Compare commits

..

No commits in common. "master" and "v0.0.1" have entirely different histories.

8 changed files with 36 additions and 65 deletions

2
.gitignore vendored
View File

@ -18,5 +18,3 @@ bin/
# ENV
.env
*.todo

View File

@ -1,17 +1,6 @@
FROM debian:bookworm AS vlang
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends ca-certificates gcc clang make git binutils libssl-dev libatomic1 && \
apt clean && rm -rf /var/cache/apt/archives/* && \
rm -rf /var/lib/apt/lists/*
RUN git clone --depth=1 https://github.com/vlang/v /opt/v && \
cd /opt/v && \
make && \
/opt/v/v symlink && \
v version
FROM vlang AS builder
FROM thevlang/vlang:latest AS builder
COPY . .
RUN v -prod -cflags -static -cflags -s -d hv_version=$(git describe --tags) . -o /habraview
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 .

View File

@ -1,2 +1,2 @@
bin:
v -prod -cflags -static -d hv_version=$$(git describe --tags) .
v -prod -cflags -static -d habraview_version=$$(git describe --tags) .

View File

@ -16,10 +16,9 @@
* Отображает все комментарии (отрисовка дерева не удалась, но и так сойдёт).
* Решает проблему с заблюренными изображениями.
Работает только с `article`, то есть новостные посты и статьи из `sandbox`
работать не будут.
Работает только с `article`, то есть новостные посты и статьи из `sandbox`.
## Как пользоваться
# Как пользоваться
`habraview` это веб-приложение. Просто запускаем файл:
@ -28,13 +27,13 @@
```
Приложение будет по умолчанию будет слушать на 8888 порту. Чтобы получить
страницу, открываем в браузере:
страницу, открываем в брайзере:
```
http://localhost:8888?url=https://habr.com/ru/articles/853062/
```
Адрес статьи на Хабре можно передать целиком как значение query-параметра `url`
Адрес статьи на Хабре можно передать целиком как значение quey-параметра `url`
или как `id`:
```
@ -43,23 +42,10 @@ http://localhost:8888?id=853062
Теперь на эту страницу можно натравить архиватор веб-страниц.
## Компиляция
# Компиляция
Нужны компиляторы `gcc` и [v](https://vlang.io):
```
make
```
## Docker
```
docker build -t habraview:latest .
docker run --rm -p 8888:8888 habraview:latest
```
или:
```
docker compose up -d
```

View File

@ -1,8 +0,0 @@
services:
habraview:
image: habraview:latest
build: .
container_name: habraview
restart: always
ports:
- '8888:8888'

View File

@ -3,7 +3,7 @@ module habr
import net.http
pub struct Habr {
baseurl string = $d('hv_habr_baseurl', 'https://habr.com')
baseurl string = $d('habr_baseurl', 'https://habr.com')
}
pub fn Habr.new() Habr {

View File

@ -12,7 +12,13 @@ pub struct Context {
veb.Context
}
pub struct App {}
pub struct App {
veb.StaticHandler
}
struct Response {
msg string
}
const embedded = {
'style.css': $embed_file('assets/style.css')
@ -34,16 +40,18 @@ 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.server_error(err.str()) }
raw_article := client.get_article(article_id.int()) or {
return ctx.json(Response{ msg: err.str() })
}
raw_comments := client.get_article_comments(article_id.int()) or {
return ctx.server_error(err.str())
return ctx.json(Response{ msg: err.str() })
}
article := habr.Article.parse(raw_article)
comments := habr.Comments.parse(raw_comments)
return $veb.html()
}
fn serve(host string, port int) ! {
fn runserver(host string, port int) ! {
mut app := &App{}
mut ipversion := net.AddrFamily.ip
if host.contains(':') {
@ -57,30 +65,28 @@ fn serve(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'
usage: '[host][:port]'
description: 'Habr.com posts viewer.'
version: $d('hv_version', '0.0.0')
version: $d('habraview_version', '0.0.0')
defaults: struct {
man: false
}
execute: run_server
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())!
}
}
app.setup()
app.parse(os.args)

2
v.mod
View File

@ -3,5 +3,5 @@ Module {
description: 'Habr.com posts viewer'
version: '0.0.1'
license: 'Unlicense'
dependencies: []
dependencies: ['whisker']
}