vieter/src/main.v

66 lines
1.2 KiB
Coq
Raw Normal View History

module main
import web
import os
import io
2022-01-13 21:47:14 +01:00
import repo
const port = 8000
2022-01-09 22:18:04 +01:00
const buf_size = 1_000_000
2022-01-12 16:31:27 +01:00
const db_name = 'pieter.db'
2022-01-09 22:30:07 +01:00
struct App {
web.Context
pub:
api_key string [required; web_global]
2022-01-19 21:24:46 +01:00
dl_dir string [required; web_global]
pub mut:
repo repo.Repo [required; web_global]
}
[noreturn]
fn exit_with_message(code int, msg string) {
eprintln(msg)
exit(code)
}
2022-01-09 22:18:04 +01:00
fn reader_to_file(mut reader io.BufferedReader, length int, path string) ? {
mut file := os.create(path) ?
defer {
file.close()
}
mut buf := []byte{len: buf_size}
2022-01-09 22:18:04 +01:00
mut bytes_left := length
// Repeat as long as the stream still has data
2022-01-09 22:18:04 +01:00
for bytes_left > 0 {
// TODO check if just breaking here is safe
2022-01-09 22:18:04 +01:00
bytes_read := reader.read(mut buf) or { break }
bytes_left -= bytes_read
mut to_write := bytes_read
for to_write > 0 {
// TODO don't just loop infinitely here
2022-01-09 22:18:04 +01:00
bytes_written := file.write(buf[bytes_read - to_write..bytes_read]) or { continue }
to_write = to_write - bytes_written
}
2022-01-09 10:36:02 +01:00
}
}
fn main() {
if os.args.len == 1 {
exit_with_message(1, 'No action provided.')
}
match os.args[1] {
'server' { server() }
'build' { build() }
else { exit_with_message(1, 'Unknown action: ${os.args[1]}') }
}
}