2022-02-21 20:51:41 +01:00
|
|
|
module server
|
2022-01-11 14:24:29 +01:00
|
|
|
|
|
|
|
import web
|
|
|
|
import os
|
|
|
|
import repo
|
2022-01-11 18:19:41 +01:00
|
|
|
import time
|
2022-01-19 21:24:46 +01:00
|
|
|
import rand
|
2022-02-21 20:51:41 +01:00
|
|
|
import util
|
2022-02-25 21:47:28 +01:00
|
|
|
import net.http
|
2022-04-07 11:58:05 +02:00
|
|
|
import response { new_response }
|
2022-01-11 18:19:41 +01:00
|
|
|
|
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-01-31 11:11:45 +01:00
|
|
|
['/health'; get]
|
|
|
|
pub fn (mut app App) healthcheck() web.Result {
|
2022-03-28 14:44:23 +02:00
|
|
|
return app.json(http.Status.ok, new_response('Healthy.'))
|
2022-01-31 11:11:45 +01:00
|
|
|
}
|
|
|
|
|
2022-03-27 17:00:11 +02:00
|
|
|
['/:repo/:arch/:filename'; get; head]
|
|
|
|
fn (mut app App) get_repo_file(repo string, arch string, filename string) web.Result {
|
2022-01-11 21:01:09 +01:00
|
|
|
mut full_path := ''
|
|
|
|
|
2022-03-27 17:00:11 +02:00
|
|
|
db_exts := ['.db', '.files', '.db.tar.gz', '.files.tar.gz']
|
|
|
|
|
2022-04-09 12:23:54 +02:00
|
|
|
// There's no point in having the ability to serve db archives with wrong
|
|
|
|
// filenames
|
|
|
|
if db_exts.any(filename == '$repo$it') {
|
2022-04-07 15:21:27 +02:00
|
|
|
full_path = os.join_path(app.repo.repos_dir, repo, arch, filename)
|
2022-03-27 17:00:11 +02:00
|
|
|
|
|
|
|
// repo-add does this using symlinks, but we just change the requested
|
|
|
|
// path
|
|
|
|
if !full_path.ends_with('.tar.gz') {
|
|
|
|
full_path += '.tar.gz'
|
|
|
|
}
|
2022-04-09 12:23:54 +02:00
|
|
|
} else if filename.contains('.pkg') {
|
2022-04-09 21:08:54 +02: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 {
|
2022-04-09 12:23:54 +02:00
|
|
|
full_path = os.join_path(app.repo.repos_dir, repo, arch, filename, 'desc')
|
2022-01-11 21:01:09 +01:00
|
|
|
}
|
|
|
|
|
2022-02-25 21:47:28 +01:00
|
|
|
// Scuffed way to respond to HEAD requests
|
|
|
|
if app.req.method == http.Method.head {
|
|
|
|
if os.exists(full_path) {
|
2022-03-28 14:44:23 +02:00
|
|
|
return app.status(http.Status.ok)
|
2022-02-25 21:47:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return app.not_found()
|
|
|
|
}
|
|
|
|
|
2022-01-11 21:01:09 +01:00
|
|
|
return app.file(full_path)
|
|
|
|
}
|
|
|
|
|
2022-03-27 17:00:11 +02:00
|
|
|
['/:repo/publish'; post]
|
|
|
|
fn (mut app App) put_package(repo string) web.Result {
|
2022-01-19 21:24:46 +01:00
|
|
|
if !app.is_authorized() {
|
2022-03-28 14:44:23 +02:00
|
|
|
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
2022-01-19 21:24:46 +01:00
|
|
|
}
|
2022-01-11 21:01:09 +01:00
|
|
|
|
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
|
2022-02-21 20:40:13 +01:00
|
|
|
pkg_path = os.join_path_single(app.conf.download_dir, rand.uuid_v4())
|
2022-01-11 14:24:29 +01:00
|
|
|
|
2022-02-21 22:22:36 +01:00
|
|
|
app.ldebug("Uploading $length bytes (${util.pretty_bytes(length.int())}) to '$pkg_path'.")
|
2022-01-11 18:19:41 +01:00
|
|
|
|
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-01-11 18:19:41 +01:00
|
|
|
|
2022-02-21 20:51:41 +01:00
|
|
|
util.reader_to_file(mut app.reader, length.int(), pkg_path) or {
|
2022-01-19 21:24:46 +01:00
|
|
|
app.lwarn("Failed to upload '$pkg_path'")
|
2022-01-11 14:24:29 +01:00
|
|
|
|
2022-03-28 14:44:23 +02:00
|
|
|
return app.json(http.Status.internal_server_error, new_response('Failed to upload file.'))
|
2022-01-19 21:24:46 +01:00
|
|
|
}
|
2022-01-11 18:19:41 +01:00
|
|
|
|
2022-01-19 21:24:46 +01:00
|
|
|
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.')
|
2022-03-28 14:24:26 +02:00
|
|
|
|
|
|
|
// length required
|
2022-03-28 14:44:23 +02:00
|
|
|
return app.status(http.Status.length_required)
|
2022-01-19 21:24:46 +01:00
|
|
|
}
|
2022-01-11 14:24:29 +01:00
|
|
|
|
2022-03-27 17:00:11 +02:00
|
|
|
res := app.repo.add_pkg_from_path(repo, pkg_path) or {
|
2022-01-20 19:39:38 +01:00
|
|
|
app.lerror('Error while adding package: $err.msg')
|
2022-01-11 14:24:29 +01:00
|
|
|
|
2022-02-02 13:24:31 +01:00
|
|
|
os.rm(pkg_path) or { app.lerror("Failed to remove download '$pkg_path': $err.msg") }
|
2022-01-30 14:13:37 +01:00
|
|
|
|
2022-03-28 14:44:23 +02:00
|
|
|
return app.json(http.Status.internal_server_error, new_response('Failed to add package.'))
|
2022-01-19 21:24:46 +01:00
|
|
|
}
|
2022-03-27 18:22:30 +02:00
|
|
|
|
2022-02-02 13:24:31 +01:00
|
|
|
if !res.added {
|
|
|
|
os.rm(pkg_path) or { app.lerror("Failed to remove download '$pkg_path': $err.msg") }
|
2022-01-30 14:13:37 +01:00
|
|
|
|
2022-03-27 18:22:30 +02:00
|
|
|
app.lwarn("Duplicate package '$res.pkg.full_name()' in repo '$repo ($res.pkg.info.arch)'.")
|
2022-01-11 14:40:25 +01:00
|
|
|
|
2022-03-28 14:44:23 +02:00
|
|
|
return app.json(http.Status.bad_request, new_response('File already exists.'))
|
2022-01-19 21:24:46 +01:00
|
|
|
}
|
2022-01-13 21:47:14 +01:00
|
|
|
|
2022-03-27 18:22:30 +02:00
|
|
|
app.linfo("Added '$res.pkg.full_name()' to repo '$repo ($res.pkg.info.arch)'.")
|
2022-01-11 14:24:29 +01:00
|
|
|
|
2022-03-28 14:44:23 +02:00
|
|
|
return app.json(http.Status.ok, new_response('Package added successfully.'))
|
2022-01-19 21:24:46 +01:00
|
|
|
}
|