2022-01-09 09:29:11 +01:00
|
|
|
module main
|
|
|
|
|
2022-01-09 21:45:48 +01:00
|
|
|
import web
|
2022-01-09 09:29:11 +01:00
|
|
|
import os
|
2022-01-09 17:44:47 +01:00
|
|
|
import log
|
2022-01-09 21:45:48 +01:00
|
|
|
import io
|
2022-01-13 21:47:14 +01:00
|
|
|
import pkg
|
|
|
|
import repo
|
2022-01-09 09:29:11 +01:00
|
|
|
|
|
|
|
const port = 8000
|
2022-01-09 22:18:04 +01:00
|
|
|
|
2022-01-11 18:19:41 +01:00
|
|
|
const buf_size = 1_000_000
|
2022-01-09 09:29:11 +01:00
|
|
|
|
2022-01-12 16:31:27 +01:00
|
|
|
const db_name = 'pieter.db'
|
2022-01-09 22:30:07 +01:00
|
|
|
|
2022-01-09 09:29:11 +01:00
|
|
|
struct App {
|
2022-01-09 21:45:48 +01:00
|
|
|
web.Context
|
2022-01-11 16:08:11 +01:00
|
|
|
pub:
|
2022-01-11 21:01:09 +01:00
|
|
|
api_key string [required; web_global]
|
2022-01-11 16:08:11 +01:00
|
|
|
pub mut:
|
2022-01-11 21:01:09 +01:00
|
|
|
repo repo.Repo [required; web_global]
|
2022-01-09 09:29:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[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) ? {
|
2022-01-09 21:45:48 +01:00
|
|
|
// Open up a file for writing to
|
|
|
|
mut file := os.create(path) ?
|
|
|
|
defer {
|
|
|
|
file.close()
|
|
|
|
}
|
2022-01-09 17:44:47 +01:00
|
|
|
|
2022-01-09 21:45:48 +01:00
|
|
|
mut buf := []byte{len: buf_size}
|
2022-01-09 22:18:04 +01:00
|
|
|
mut bytes_left := length
|
2022-01-09 17:44:47 +01:00
|
|
|
|
2022-01-09 21:45:48 +01:00
|
|
|
// Repeat as long as the stream still has data
|
2022-01-09 22:18:04 +01:00
|
|
|
for bytes_left > 0 {
|
2022-01-09 21:45:48 +01:00
|
|
|
// 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
|
2022-01-09 17:44:47 +01:00
|
|
|
|
2022-01-09 21:45:48 +01:00
|
|
|
mut to_write := bytes_read
|
2022-01-09 17:44:47 +01:00
|
|
|
|
2022-01-09 21:45:48 +01:00
|
|
|
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 }
|
2022-01-09 21:45:48 +01:00
|
|
|
|
|
|
|
to_write = to_write - bytes_written
|
2022-01-09 17:44:47 +01:00
|
|
|
}
|
2022-01-09 10:36:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 21:47:14 +01:00
|
|
|
// fn main2() {
|
|
|
|
// // Configure logger
|
|
|
|
// log_level_str := os.getenv_opt('LOG_LEVEL') or { 'WARN' }
|
|
|
|
// log_level := log.level_from_tag(log_level_str) or {
|
|
|
|
// exit_with_message(1, 'Invalid log level. The allowed values are FATAL, ERROR, WARN, INFO & DEBUG.')
|
|
|
|
// }
|
|
|
|
// log_file := os.getenv_opt('LOG_FILE') or { 'vieter.log' }
|
|
|
|
|
|
|
|
// mut logger := log.Log{
|
|
|
|
// level: log_level
|
|
|
|
// }
|
|
|
|
|
|
|
|
// logger.set_full_logpath(log_file)
|
|
|
|
// logger.log_to_console_too()
|
|
|
|
|
|
|
|
// defer {
|
|
|
|
// logger.info('Flushing log file')
|
|
|
|
// logger.flush()
|
|
|
|
// logger.close()
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // Configure web server
|
|
|
|
// key := os.getenv_opt('API_KEY') or { exit_with_message(1, 'No API key was provided.') }
|
|
|
|
// repo_dir := os.getenv_opt('REPO_DIR') or {
|
|
|
|
// exit_with_message(1, 'No repo directory was configured.')
|
|
|
|
// }
|
|
|
|
|
|
|
|
// repo := repo.Repo{
|
|
|
|
// dir: repo_dir
|
|
|
|
// name: db_name
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // We create the upload directory during startup
|
|
|
|
// if !os.is_dir(repo.pkg_dir()) {
|
|
|
|
// os.mkdir_all(repo.pkg_dir()) or {
|
|
|
|
// exit_with_message(2, "Failed to create repo directory '$repo.pkg_dir()'.")
|
|
|
|
// }
|
|
|
|
|
|
|
|
// logger.info("Created package directory '$repo.pkg_dir()'.")
|
|
|
|
// }
|
|
|
|
|
|
|
|
// web.run(&App{
|
|
|
|
// logger: logger
|
|
|
|
// api_key: key
|
|
|
|
// repo: repo
|
|
|
|
// }, port)
|
|
|
|
// }
|
2022-01-13 12:35:05 +01:00
|
|
|
|
2022-01-13 19:20:14 +01:00
|
|
|
fn main() {
|
|
|
|
// archive.list_filenames()
|
2022-01-18 22:52:14 +01:00
|
|
|
res := pkg.read_pkg('test/homebank-5.5.3-1-x86_64.pkg.tar.zst') or {
|
2022-01-13 19:20:14 +01:00
|
|
|
eprintln(err.msg)
|
|
|
|
return
|
|
|
|
}
|
2022-01-13 21:47:14 +01:00
|
|
|
// println(info)
|
2022-01-18 20:27:00 +01:00
|
|
|
print(res.info.to_desc())
|
2022-01-13 19:20:14 +01:00
|
|
|
}
|