Finally enabled web server again
ci/woodpecker/push/builder unknown status Details
ci/woodpecker/push/publish Pipeline was successful Details
ci/woodpecker/push/lint Pipeline was successful Details
ci/woodpecker/push/build Pipeline failed Details

pull/48/head
Jef Roosens 2022-01-19 21:24:46 +01:00
parent 57fe767a70
commit d04ba06ad3
Signed by: Jef Roosens
GPG Key ID: 955C0660072F691F
5 changed files with 103 additions and 99 deletions

View File

@ -38,7 +38,7 @@ c:
# Run the server in the default 'data' directory
.PHONY: run
run: vieter
API_KEY=test REPO_DIR=data LOG_LEVEL=DEBUG ./vieter
API_KEY=test DOWNLOAD_DIR=data/downloads REPO_DIR=data/repo PKG_DIR=data/pkgs LOG_LEVEL=DEBUG ./vieter
.PHONY: run-prod
run-prod: prod

View File

@ -16,6 +16,7 @@ struct App {
web.Context
pub:
api_key string [required; web_global]
dl_dir string [required; web_global]
pub mut:
repo repo.Repo [required; web_global]
}
@ -53,65 +54,67 @@ fn reader_to_file(mut reader io.BufferedReader, length int, path string) ? {
}
}
// 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)
// }
fn main() {
r := repo.new('data/repo', 'data/pkgs') or { return }
print(r.add_from_path('test/homebank-5.5.1-1-x86_64.pkg.tar.zst') or { panic('you fialed') })
// 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' }
// archive.list_filenames()
// res := pkg.read_pkg('test/jjr-joplin-desktop-2.6.10-4-x86_64.pkg.tar.zst') or {
// eprintln(err.msg)
// return
// }
// println(info)
// println('hey')
// print(res.to_desc())
// print(res.to_files())
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.')
}
pkg_dir := os.getenv_opt('PKG_DIR') or {
exit_with_message(1, 'No package directory was configured.')
}
dl_dir := os.getenv_opt('DOWNLOAD_DIR') or {
exit_with_message(1, 'No download directory was configured.')
}
// This also creates the directories if needed
repo := repo.new(repo_dir, pkg_dir) or {
exit_with_message(1, 'Failed to create required directories.')
}
os.mkdir_all(dl_dir) or { exit_with_message(1, 'Failed to create download directory.') }
web.run(&App{
logger: logger
api_key: key
dl_dir: dl_dir
repo: repo
}, port)
}
// fn main() {
// r := repo.new('data/repo', 'data/pkgs') or { return }
// print(r.add_from_path('test/jjr-joplin-desktop-2.6.10-4-x86_64.pkg.tar.zst') or {
// panic('you fialed')
// })
// // archive.list_filenames()
// // res := pkg.read_pkg('test/jjr-joplin-desktop-2.6.10-4-x86_64.pkg.tar.zst') or {
// // eprintln(err.msg)
// // return
// // }
// // println(info)
// // println('hey')
// // print(res.to_desc())
// // print(res.to_files())
// }

View File

@ -1,6 +1,5 @@
module package
import time
import os
import util
@ -93,6 +92,7 @@ pub mut:
checkdepends []string
}
// checksum calculates the md5 & sha256 hash of the package
pub fn (p &Pkg) checksum() ?(string, string) {
return util.hash_file(p.path)
}
@ -214,6 +214,7 @@ fn format_entry(key string, value string) string {
return '\n%$key%\n$value\n'
}
// filename returns the correct filename of the package file
pub fn (pkg &Pkg) filename() string {
p := pkg.info

View File

@ -26,6 +26,7 @@ pub:
pkg_dir string [required]
}
// new creates a new Repo & creates the directories as needed
pub fn new(repo_dir string, pkg_dir string) ?Repo {
if !os.is_dir(repo_dir) {
os.mkdir_all(repo_dir) or { return error('Failed to create repo directory.') }

View File

@ -4,6 +4,7 @@ import web
import os
import repo
import time
import rand
const prefixes = ['B', 'KB', 'MB', 'GB']
@ -38,57 +39,55 @@ fn (mut app App) get_root(filename string) web.Result {
return app.file(full_path)
}
// ['/pkgs/:pkg'; put]
// fn (mut app App) put_package(pkg string) web.Result {
// if !app.is_authorized() {
// return app.text('Unauthorized.')
// }
['/publish'; post]
fn (mut app App) put_package() web.Result {
if !app.is_authorized() {
return app.text('Unauthorized.')
}
// if !is_pkg_name(pkg) {
// app.lwarn("Invalid package name '$pkg'.")
mut pkg_path := ''
// return app.text('Invalid filename.')
// }
if length := app.req.header.get(.content_length) {
// Generate a random filename for the temp file
pkg_path = os.join_path_single(app.dl_dir, rand.uuid_v4())
// if app.repo.exists(pkg) {
// app.lwarn("Duplicate package '$pkg'")
for os.exists(pkg_path) {
pkg_path = os.join_path_single(app.dl_dir, rand.uuid_v4())
}
// return app.text('File already exists.')
// }
app.ldebug("Uploading $length (${pretty_bytes(length.int())}) bytes to '$pkg_path'.")
// pkg_path := app.repo.pkg_path(pkg)
// This is used to time how long it takes to upload a file
mut sw := time.new_stopwatch(time.StopWatchOptions{ auto_start: true })
// if length := app.req.header.get(.content_length) {
// app.ldebug("Uploading $length (${pretty_bytes(length.int())}) bytes to package '$pkg'.")
reader_to_file(mut app.reader, length.int(), pkg_path) or {
app.lwarn("Failed to upload '$pkg_path'")
// // This is used to time how long it takes to upload a file
// mut sw := time.new_stopwatch(time.StopWatchOptions{ auto_start: true })
return app.text('Failed to upload file.')
}
// reader_to_file(mut app.reader, length.int(), pkg_path) or {
// app.lwarn("Failed to upload package '$pkg'")
sw.stop()
app.ldebug("Upload of '$pkg_path' completed in ${sw.elapsed().seconds():.3}s.")
} else {
app.lwarn('Tried to upload package without specifying a Content-Length.')
return app.text("Content-Type header isn't set.")
}
// return app.text('Failed to upload file.')
// }
added := app.repo.add_from_path(pkg_path) or {
app.lerror('Error while adding package.')
// sw.stop()
// app.ldebug("Upload of package '$pkg' completed in ${sw.elapsed().seconds():.3}s.")
// } else {
// app.lwarn("Tried to upload package '$pkg' without specifying a Content-Length.")
// return app.text("Content-Type header isn't set.")
// }
return app.text('Failed to add package.')
}
if !added {
app.lwarn('Duplicate package.')
// app.repo.add_package(pkg_path) or {
// app.lwarn("Failed to add package '$pkg' to database.")
return app.text('File already exists.')
}
// os.rm(pkg_path) or { println('Failed to remove $pkg_path') }
app.linfo("Added '$pkg_path' to repository.")
// return app.text('Failed to add package to repo.')
// }
// app.linfo("Added '$pkg' to repository.")
// return app.text('Package added successfully.')
// }
return app.text('Package added successfully.')
}
// add_package PUT a new package to the server
['/add'; put]