This commit is contained in:
ge 2024-11-03 16:55:43 +03:00
parent 493d23c2f3
commit 038d8d0859
6 changed files with 54 additions and 25 deletions

7
Dockerfile Normal file
View File

@ -0,0 +1,7 @@
FROM thevlang/vlang:latest AS builder
COPY . .
RUN v -prod -cc gcc -cflags -static -cflags -s -d habraview_version=$(git describe --tags) . -o /habraview
FROM scratch AS prod
COPY --from=builder /habraview .
ENTRYPOINT ["/habraview"]

View File

@ -1,7 +1,2 @@
all: build
build:
v -o habraview src
prod:
v -o habraview -prod -cc clang -compress src
bin:
v -prod -cflags -static -d habraview_version=$$(git describe --tags) .

View File

@ -47,5 +47,5 @@ http://localhost:8888?id=853062
Нужны компиляторы `gcc` и [v](https://vlang.io):
```
v -prod -cflags -static -cflags -s .
make
```

View File

@ -2,6 +2,9 @@ module main
import cli
import habr
import net
import net.urllib
import net.http.mime
import os
import veb
@ -17,6 +20,22 @@ struct Response {
msg string
}
const embedded = {
'style.css': $embed_file('assets/style.css')
'highlight.min.css': $embed_file('assets/highlight.min.css')
'highlight.min.js': $embed_file('assets/highlight.min.js')
'habrfixer.js': $embed_file('assets/habrfixer.js')
'favicon.ico': $embed_file('assets/favicon.ico')
}
@['/assets/:filename']
fn (a &App) assets(mut ctx Context, filename string) veb.Result {
asset := embedded[filename] or { return ctx.not_found() }
mimetype := mime.get_mime_type(os.file_ext(filename).trim_left('.'))
ctx.set_content_type(mimetype)
return ctx.text(asset.to_string())
}
@[get]
fn (a &App) index(mut ctx Context) veb.Result {
article_id := ctx.query['id'] or { habr.get_id_from_url(ctx.query['url']) or { '' } }
@ -32,35 +51,42 @@ fn (a &App) index(mut ctx Context) veb.Result {
return $veb.html()
}
fn runserver(port int) ! {
os.chdir(os.dir(@FILE))!
fn runserver(host string, port int) ! {
mut app := &App{}
app.handle_static('assets', false)!
app.serve_static('/favicon.ico', 'assets/favicon.ico')!
veb.run[App, Context](mut app, port)
mut ipversion := net.AddrFamily.ip
if host.contains(':') {
ipversion = net.AddrFamily.ip6
}
params := veb.RunParams{
host: host
port: port
family: ipversion
}
veb.run_at[App, Context](mut app, params)!
}
fn main() {
mut app := cli.Command{
name: 'habraview'
usage: '[host][:port]'
description: 'Habr.com posts viewer.'
version: $d('habraview_version', '0.0.0')
defaults: struct {
man: false
}
execute: fn (cmd cli.Command) ! {
port := cmd.flags.get_int('port') or { 8080 }
runserver(port)!
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())!
}
flags: [
cli.Flag{
flag: .int
name: 'port'
abbrev: 'p'
description: 'Listen port [default: 8888].'
default_value: ['8888']
},
]
}
app.setup()
app.parse(os.args)

View File

@ -3,6 +3,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@{article.title}</title>
<link rel="icon" type="image/x-icon" href="/assets/favicon.ico">
@css '/assets/style.css'
@css '/assets/highlight.min.css'
</head>
@ -49,7 +50,7 @@
</main>
<footer>
</footer>
@js '/assets/habr-fixer.js'
@js '/assets/habrfixer.js'
@js '/assets/highlight.min.js'
<script>hljs.highlightAll();</script>
</body>