2022-02-21 22:22:36 +01:00
|
|
|
module server
|
|
|
|
|
|
|
|
import web
|
2022-03-28 14:44:23 +02:00
|
|
|
import net.http
|
2022-04-07 11:58:05 +02:00
|
|
|
import response { new_data_response, new_response }
|
2022-05-01 22:47:00 +02:00
|
|
|
import db
|
2022-02-21 22:22:36 +01:00
|
|
|
|
2022-04-30 20:22:03 +02:00
|
|
|
// get_repos returns the current list of repos.
|
2022-02-21 22:22:36 +01:00
|
|
|
['/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
|
|
|
}
|
|
|
|
|
2022-05-01 22:47:00 +02:00
|
|
|
repos := app.db.get_git_repos()
|
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-04-30 20:22:03 +02:00
|
|
|
// get_single_repo returns the information for a single repo.
|
2022-03-28 10:19:57 +02:00
|
|
|
['/api/repos/:id'; get]
|
2022-05-01 22:47:00 +02:00
|
|
|
fn (mut app App) get_single_repo(id int) web.Result {
|
2022-03-28 10:19:57 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-05-01 22:47:00 +02:00
|
|
|
repo := app.db.get_git_repo(id) or { return app.not_found() }
|
2022-03-28 10:19:57 +02:00
|
|
|
|
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-04-30 20:22:03 +02:00
|
|
|
// post_repo creates a new repo from the provided query string.
|
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-05-01 12:44:54 +02:00
|
|
|
mut params := app.query.clone()
|
|
|
|
|
|
|
|
// If a repo is created without specifying the arch, we assume it's meant
|
|
|
|
// for the default architecture.
|
|
|
|
if 'arch' !in params {
|
|
|
|
params['arch'] = app.conf.default_arch
|
|
|
|
}
|
|
|
|
|
2022-05-01 22:47:00 +02:00
|
|
|
new_repo := db.git_repo_from_params(params) or {
|
2022-04-13 22:20:05 +02:00
|
|
|
return app.json(http.Status.bad_request, new_response(err.msg()))
|
2022-02-21 22:22:36 +01:00
|
|
|
}
|
|
|
|
|
2022-05-01 22:47:00 +02:00
|
|
|
app.db.add_git_repo(new_repo)
|
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-04-30 20:22:03 +02:00
|
|
|
// delete_repo removes a given repo from the server's list.
|
2022-03-28 10:19:57 +02:00
|
|
|
['/api/repos/:id'; delete]
|
2022-05-01 22:47:00 +02:00
|
|
|
fn (mut app App) delete_repo(id int) 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-05-03 16:16:56 +02:00
|
|
|
// repos.delete(id)
|
2022-05-01 22:47:00 +02:00
|
|
|
app.db.delete_git_repo(id)
|
2022-03-28 10:19:57 +02: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
|
|
|
}
|
|
|
|
|
2022-04-30 20:22:03 +02:00
|
|
|
// patch_repo updates a repo's data with the given query params.
|
2022-03-28 10:19:57 +02:00
|
|
|
['/api/repos/:id'; patch]
|
2022-05-02 20:59:53 +02:00
|
|
|
fn (mut app App) patch_repo(id int) web.Result {
|
2022-03-28 10:19:57 +02: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-05-02 20:59:53 +02:00
|
|
|
app.db.update_git_repo(id, app.query)
|
2022-02-21 22:40:59 +01:00
|
|
|
|
2022-05-03 16:16:56 +02:00
|
|
|
if 'arch' in app.query {
|
|
|
|
arch_objs := app.query['arch'].split(',').map(db.GitRepoArch{ value: it })
|
2022-05-02 20:59:53 +02:00
|
|
|
|
2022-05-03 16:16:56 +02:00
|
|
|
app.db.update_git_repo_archs(id, arch_objs)
|
|
|
|
}
|
2022-03-28 10:19:57 +02: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
|
|
|
}
|