vieter/src/server/repo.v

97 lines
3.1 KiB
Coq
Raw Normal View History

2022-02-21 20:51:41 +01:00
module server
2022-01-11 14:24:29 +01:00
import web
import os
import repo
import time
2022-01-19 21:24:46 +01:00
import rand
2022-02-21 20:51:41 +01:00
import util
import web.response { new_data_response, new_response }
2022-02-02 13:34:28 +01:00
// healthcheck just returns a string, but can be used to quickly check if the
// server is still responsive.
2022-12-22 23:10:10 +01:00
['/health'; get; markused]
pub fn (mut app App) healthcheck() web.Result {
return app.json(.ok, new_response('Healthy.'))
}
// get_repo_file handles all Pacman-related routes. It returns both the
// repository's archives, but also package archives or the contents of a
// package's desc file.
2022-12-22 23:10:10 +01:00
['/:repo/:arch/:filename'; get; head; markused]
2023-02-15 20:07:07 +01:00
fn (mut app App) get_repo_file(repo_ string, arch string, filename string) web.Result {
mut full_path := ''
db_exts := ['.db', '.files', '.db.tar.gz', '.files.tar.gz']
// There's no point in having the ability to serve db archives with wrong
// filenames
2023-02-15 20:07:07 +01:00
if db_exts.any(filename == '${repo_}${it}') {
full_path = os.join_path(app.repo.repos_dir, repo_, arch, filename)
// repo-add does this using symlinks, but we just change the requested
// path
if !full_path.ends_with('.tar.gz') {
full_path += '.tar.gz'
}
} else if filename.contains('.pkg') {
2023-02-15 20:07:07 +01:00
full_path = os.join_path(app.repo.pkg_dir, repo_, arch, filename)
}
// Default behavior is to return the desc file for the package, if present.
// This can then also be used by the build system to properly check whether
// a package is present in an arch-repo.
else {
2023-02-15 20:07:07 +01:00
full_path = os.join_path(app.repo.repos_dir, repo_, arch, filename, 'desc')
}
return app.file(full_path)
}
// put_package handles publishing a package to a repository.
2022-12-22 23:10:10 +01:00
['/:repo/publish'; auth; markused; post]
2023-02-15 20:07:07 +01:00
fn (mut app App) put_package(repo_ string) web.Result {
// api is a reserved keyword for api routes & should never be allowed to be
// a repository.
2023-02-15 20:07:07 +01:00
if repo_.to_lower() == 'api' {
return app.json(.bad_request, new_response("'api' is a reserved keyword & cannot be used as a repository name."))
}
2022-01-19 21:24:46 +01:00
mut pkg_path := ''
2022-01-11 14:24:29 +01:00
2022-01-19 21:24:46 +01:00
if length := app.req.header.get(.content_length) {
// Generate a random filename for the temp file
pkg_path = os.join_path_single(app.repo.pkg_dir, rand.uuid_v4())
2022-01-11 14:24:29 +01:00
2023-02-08 11:00:17 +01:00
app.ldebug("Uploading ${length} bytes (${util.pretty_bytes(length.int())}) to '${pkg_path}'.")
2022-01-19 21:24:46 +01:00
// This is used to time how long it takes to upload a file
mut sw := time.new_stopwatch(time.StopWatchOptions{ auto_start: true })
2022-02-21 20:51:41 +01:00
util.reader_to_file(mut app.reader, length.int(), pkg_path) or {
2023-02-08 11:00:17 +01:00
app.lwarn("Failed to upload '${pkg_path}'")
2022-01-11 14:24:29 +01:00
return app.status(.internal_server_error)
2022-01-19 21:24:46 +01:00
}
2022-01-19 21:24:46 +01:00
sw.stop()
2023-02-08 11:00:17 +01:00
app.ldebug("Upload of '${pkg_path}' completed in ${sw.elapsed().seconds():.3}s.")
2022-01-19 21:24:46 +01:00
} else {
app.lwarn('Tried to upload package without specifying a Content-Length.')
2022-03-28 14:24:26 +02:00
// length required
return app.status(.length_required)
2022-01-19 21:24:46 +01:00
}
2022-01-11 14:24:29 +01:00
2023-02-15 20:07:07 +01:00
res := app.repo.add_pkg_from_path(repo_, pkg_path) or {
2023-02-08 11:00:17 +01:00
app.lerror('Error while adding package: ${err.msg()}')
2022-01-11 14:24:29 +01:00
2023-02-08 11:00:17 +01:00
os.rm(pkg_path) or { app.lerror("Failed to remove download '${pkg_path}': ${err.msg()}") }
return app.status(.internal_server_error)
2022-01-19 21:24:46 +01:00
}
2022-03-27 18:22:30 +02:00
2023-02-15 20:07:07 +01:00
app.linfo("Added '${res.name}-${res.version}' to '${repo_} (${res.archs.join(',')})'.")
2022-01-11 14:24:29 +01:00
return app.json(.ok, new_data_response(res))
2022-01-19 21:24:46 +01:00
}