From b67b854b88b4c7d2879c8fdfaa35df2bb281f156 Mon Sep 17 00:00:00 2001 From: ge Date: Sun, 3 Nov 2024 18:00:57 +0300 Subject: [PATCH] upd --- README.md | 8 +++++++- docker-compose.yaml | 8 ++++++++ habraview.v | 44 +++++++++++++++++++------------------------- 3 files changed, 34 insertions(+), 26 deletions(-) create mode 100644 docker-compose.yaml diff --git a/README.md b/README.md index 8da14d8..bf66349 100644 --- a/README.md +++ b/README.md @@ -56,4 +56,10 @@ make ``` docker build -t habraview:latest . docker run --rm -p 8888:8888 habraview:latest -``` \ No newline at end of file +``` + +или: + +``` +docker compose up -d +``` diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..ed130a1 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,8 @@ +services: + habraview: + image: habraview:latest + build: . + container_name: habraview + restart: always + ports: + - '8888:8888' diff --git a/habraview.v b/habraview.v index 70cac27..1699066 100644 --- a/habraview.v +++ b/habraview.v @@ -12,13 +12,7 @@ pub struct Context { veb.Context } -pub struct App { - veb.StaticHandler -} - -struct Response { - msg string -} +pub struct App {} const embedded = { 'style.css': $embed_file('assets/style.css') @@ -40,18 +34,16 @@ fn (a &App) assets(mut ctx Context, filename string) veb.Result { fn (a &App) index(mut ctx Context) veb.Result { article_id := ctx.query['id'] or { habr.get_id_from_url(ctx.query['url']) or { '' } } client := habr.Habr.new() - raw_article := client.get_article(article_id.int()) or { - return ctx.json(Response{ msg: err.str() }) - } + raw_article := client.get_article(article_id.int()) or { return ctx.server_error(err.str()) } raw_comments := client.get_article_comments(article_id.int()) or { - return ctx.json(Response{ msg: err.str() }) + return ctx.server_error(err.str()) } article := habr.Article.parse(raw_article) comments := habr.Comments.parse(raw_comments) return $veb.html() } -fn runserver(host string, port int) ! { +fn serve(host string, port int) ! { mut app := &App{} mut ipversion := net.AddrFamily.ip if host.contains(':') { @@ -65,6 +57,20 @@ fn runserver(host string, port int) ! { veb.run_at[App, Context](mut app, params)! } +fn run_server(cmd cli.Command) ! { + mut host, mut port := '0.0.0.0', '8888' + if cmd.args.len == 1 { + host, port = urllib.split_host_port(cmd.args[0]) + if host.is_blank() { + host = '0.0.0.0' + } + if port.is_blank() { + port = '8888' + } + } + serve(host, port.int())! +} + fn main() { mut app := cli.Command{ name: 'habraview' @@ -74,19 +80,7 @@ fn main() { defaults: struct { man: false } - execute: fn (cmd cli.Command) ! { - mut host, mut port := '0.0.0.0', '8888' - if cmd.args.len == 1 { - host, port = urllib.split_host_port(cmd.args[0]) - if host.is_blank() { - host = '0.0.0.0' - } - if port.is_blank() { - port = '8888' - } - } - runserver(host, port.int())! - } + execute: run_server } app.setup() app.parse(os.args)