2022-02-21 22:22:36 +01:00
|
|
|
module server
|
|
|
|
|
|
|
|
import web
|
|
|
|
import os
|
|
|
|
import json
|
2022-03-28 10:19:57 +02:00
|
|
|
import rand
|
2022-03-28 14:44:23 +02:00
|
|
|
import net.http
|
2022-02-21 22:22:36 +01:00
|
|
|
|
|
|
|
pub struct GitRepo {
|
2022-03-28 10:19:57 +02:00
|
|
|
pub mut:
|
|
|
|
// URL of the Git repository
|
2022-04-01 21:34:58 +02:00
|
|
|
url string
|
2022-03-28 10:19:57 +02:00
|
|
|
// Branch of the Git repository to use
|
|
|
|
branch string
|
|
|
|
// On which architectures the package is allowed to be built. In reality,
|
|
|
|
// this controls which builders will periodically build the image.
|
2022-04-01 21:34:58 +02:00
|
|
|
arch []string
|
2022-02-21 22:22:36 +01:00
|
|
|
}
|
|
|
|
|
2022-03-28 13:34:22 +02:00
|
|
|
fn (mut r GitRepo) patch_from_params(params map[string]string) {
|
2022-03-28 10:19:57 +02:00
|
|
|
$for field in GitRepo.fields {
|
|
|
|
if field.name in params {
|
|
|
|
$if field.typ is string {
|
|
|
|
r.$(field.name) = params[field.name]
|
2022-04-01 21:34:58 +02:00
|
|
|
// This specific type check is needed for the compiler to ensure
|
|
|
|
// our types are correct
|
2022-03-28 10:19:57 +02:00
|
|
|
} $else $if field.typ is []string {
|
|
|
|
r.$(field.name) = params[field.name].split(',')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:34:22 +02:00
|
|
|
fn repo_from_params(params map[string]string) ?GitRepo {
|
2022-03-28 10:19:57 +02:00
|
|
|
mut repo := GitRepo{}
|
|
|
|
|
2022-03-28 13:34:22 +02:00
|
|
|
// If we're creating a new GitRepo, we want all fields to be present before
|
|
|
|
// "patching".
|
|
|
|
$for field in GitRepo.fields {
|
|
|
|
if field.name !in params {
|
|
|
|
return error('Missing parameter: ${field.name}.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
repo.patch_from_params(params)
|
2022-03-28 10:19:57 +02:00
|
|
|
|
|
|
|
return repo
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_repos(path string) ?map[string]GitRepo {
|
2022-02-21 22:22:36 +01:00
|
|
|
if !os.exists(path) {
|
|
|
|
mut f := os.create(path) ?
|
|
|
|
|
|
|
|
defer {
|
|
|
|
f.close()
|
|
|
|
}
|
|
|
|
|
2022-03-28 10:19:57 +02:00
|
|
|
f.write_string('{}') ?
|
2022-02-21 22:22:36 +01:00
|
|
|
|
2022-03-28 10:19:57 +02:00
|
|
|
return {}
|
2022-02-21 22:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
content := os.read_file(path) ?
|
2022-03-28 10:19:57 +02:00
|
|
|
res := json.decode(map[string]GitRepo, content) ?
|
2022-02-21 22:32:54 +01:00
|
|
|
return res
|
2022-02-21 22:22:36 +01:00
|
|
|
}
|
|
|
|
|
2022-03-28 10:19:57 +02:00
|
|
|
fn write_repos(path string, repos &map[string]GitRepo) ? {
|
2022-02-21 22:22:36 +01:00
|
|
|
mut f := os.create(path) ?
|
|
|
|
|
|
|
|
defer {
|
|
|
|
f.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
value := json.encode(repos)
|
|
|
|
f.write_string(value) ?
|
|
|
|
}
|
|
|
|
|
|
|
|
['/api/repos'; get]
|
2022-02-22 08:14:20 +01:00
|
|
|
fn (mut app App) get_repos() web.Result {
|
2022-02-21 22:22:36 +01:00
|
|
|
if !app.is_authorized() {
|
2022-03-28 14:44:23 +02:00
|
|
|
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
2022-02-21 22:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
repos := rlock app.git_mutex {
|
|
|
|
read_repos(app.conf.repos_file) or {
|
|
|
|
app.lerror('Failed to read repos file.')
|
|
|
|
|
2022-03-28 14:44:23 +02:00
|
|
|
return app.status(http.Status.internal_server_error)
|
2022-02-21 22:22:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 14:44:23 +02:00
|
|
|
return app.json(http.Status.ok, new_data_response(repos))
|
2022-02-21 22:22:36 +01:00
|
|
|
}
|
|
|
|
|
2022-03-28 10:19:57 +02:00
|
|
|
['/api/repos/:id'; get]
|
|
|
|
fn (mut app App) get_single_repo(id string) web.Result {
|
|
|
|
if !app.is_authorized() {
|
2022-03-28 14:44:23 +02:00
|
|
|
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
2022-03-28 10:19:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
repos := rlock app.git_mutex {
|
|
|
|
read_repos(app.conf.repos_file) or {
|
|
|
|
app.lerror('Failed to read repos file.')
|
|
|
|
|
2022-03-28 14:44:23 +02:00
|
|
|
return app.status(http.Status.internal_server_error)
|
2022-03-28 10:19:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if id !in repos {
|
|
|
|
return app.not_found()
|
|
|
|
}
|
|
|
|
|
|
|
|
repo := repos[id]
|
|
|
|
|
2022-03-28 14:44:23 +02:00
|
|
|
return app.json(http.Status.ok, new_data_response(repo))
|
2022-03-28 10:19:57 +02:00
|
|
|
}
|
|
|
|
|
2022-02-21 22:22:36 +01:00
|
|
|
['/api/repos'; post]
|
2022-02-22 08:14:20 +01:00
|
|
|
fn (mut app App) post_repo() web.Result {
|
2022-02-21 22:22:36 +01:00
|
|
|
if !app.is_authorized() {
|
2022-03-28 14:44:23 +02:00
|
|
|
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
2022-02-21 22:22:36 +01:00
|
|
|
}
|
|
|
|
|
2022-03-28 13:34:22 +02:00
|
|
|
new_repo := repo_from_params(app.query) or {
|
2022-03-28 14:44:23 +02:00
|
|
|
return app.json(http.Status.bad_request, new_response(err.msg))
|
2022-02-21 22:22:36 +01:00
|
|
|
}
|
|
|
|
|
2022-03-28 10:19:57 +02:00
|
|
|
id := rand.uuid_v4()
|
2022-02-21 22:22:36 +01:00
|
|
|
|
|
|
|
mut repos := rlock app.git_mutex {
|
|
|
|
read_repos(app.conf.repos_file) or {
|
|
|
|
app.lerror('Failed to read repos file.')
|
|
|
|
|
2022-03-28 14:44:23 +02:00
|
|
|
return app.status(http.Status.internal_server_error)
|
2022-02-21 22:22:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to check for duplicates
|
2022-03-28 10:19:57 +02:00
|
|
|
for _, repo in repos {
|
|
|
|
if repo == new_repo {
|
2022-03-28 14:44:23 +02:00
|
|
|
return app.json(http.Status.bad_request, new_response('Duplicate repository.'))
|
2022-02-21 22:22:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 10:19:57 +02:00
|
|
|
repos[id] = new_repo
|
2022-02-21 22:22:36 +01:00
|
|
|
|
|
|
|
lock app.git_mutex {
|
2022-04-01 21:34:58 +02:00
|
|
|
write_repos(app.conf.repos_file, &repos) or {
|
|
|
|
return app.status(http.Status.internal_server_error)
|
|
|
|
}
|
2022-02-21 22:22:36 +01:00
|
|
|
}
|
|
|
|
|
2022-03-28 14:44:23 +02:00
|
|
|
return app.json(http.Status.ok, new_response('Repo added successfully.'))
|
2022-02-21 22:22:36 +01:00
|
|
|
}
|
2022-02-21 22:40:59 +01:00
|
|
|
|
2022-03-28 10:19:57 +02:00
|
|
|
['/api/repos/:id'; delete]
|
|
|
|
fn (mut app App) delete_repo(id string) web.Result {
|
2022-02-21 22:40:59 +01:00
|
|
|
if !app.is_authorized() {
|
2022-03-28 14:44:23 +02:00
|
|
|
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
2022-02-21 22:40:59 +01:00
|
|
|
}
|
|
|
|
|
2022-03-28 10:19:57 +02:00
|
|
|
mut repos := rlock app.git_mutex {
|
|
|
|
read_repos(app.conf.repos_file) or {
|
|
|
|
app.lerror('Failed to read repos file.')
|
|
|
|
|
2022-03-28 14:44:23 +02:00
|
|
|
return app.status(http.Status.internal_server_error)
|
2022-03-28 10:19:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if id !in repos {
|
|
|
|
return app.not_found()
|
|
|
|
}
|
|
|
|
|
|
|
|
repos.delete(id)
|
|
|
|
|
|
|
|
lock app.git_mutex {
|
|
|
|
write_repos(app.conf.repos_file, &repos) or { return app.server_error(500) }
|
2022-02-21 22:40:59 +01:00
|
|
|
}
|
|
|
|
|
2022-03-28 14:44:23 +02:00
|
|
|
return app.json(http.Status.ok, new_response('Repo removed successfully.'))
|
2022-03-28 10:19:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
['/api/repos/:id'; patch]
|
|
|
|
fn (mut app App) patch_repo(id string) web.Result {
|
|
|
|
if !app.is_authorized() {
|
2022-03-28 14:44:23 +02:00
|
|
|
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
2022-02-21 22:40:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
mut repos := rlock app.git_mutex {
|
|
|
|
read_repos(app.conf.repos_file) or {
|
|
|
|
app.lerror('Failed to read repos file.')
|
|
|
|
|
2022-03-28 14:44:23 +02:00
|
|
|
return app.status(http.Status.internal_server_error)
|
2022-02-21 22:40:59 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-28 10:19:57 +02:00
|
|
|
|
|
|
|
if id !in repos {
|
|
|
|
return app.not_found()
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:34:22 +02:00
|
|
|
repos[id].patch_from_params(app.query)
|
2022-02-21 22:40:59 +01:00
|
|
|
|
|
|
|
lock app.git_mutex {
|
2022-03-28 10:19:57 +02:00
|
|
|
write_repos(app.conf.repos_file, &repos) or { return app.server_error(500) }
|
2022-02-21 22:40:59 +01:00
|
|
|
}
|
|
|
|
|
2022-03-28 14:44:23 +02:00
|
|
|
return app.json(http.Status.ok, new_response('Repo updated successfully.'))
|
2022-02-21 22:40:59 +01:00
|
|
|
}
|