2022-02-21 22:22:36 +01:00
|
|
|
module server
|
|
|
|
|
|
|
|
import web
|
2022-04-06 16:52:31 +02:00
|
|
|
import git
|
2022-03-28 14:44:23 +02:00
|
|
|
import net.http
|
2022-04-06 21:03:52 +02:00
|
|
|
import rand
|
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
|
|
|
|
|
|
|
const repos_file = 'repos.json'
|
|
|
|
|
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()
|
|
|
|
// repos := rlock app.git_mutex {
|
|
|
|
// git.read_repos(app.conf.repos_file) or {
|
|
|
|
// app.lerror('Failed to read repos file: $err.msg()')
|
2022-02-21 22:22:36 +01:00
|
|
|
|
2022-05-01 22:47:00 +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-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
|
|
|
// repos := rlock app.git_mutex {
|
|
|
|
// git.read_repos(app.conf.repos_file) or {
|
|
|
|
// app.lerror('Failed to read repos file.')
|
2022-03-28 10:19:57 +02:00
|
|
|
|
2022-05-01 22:47:00 +02:00
|
|
|
// return app.status(http.Status.internal_server_error)
|
|
|
|
// }
|
|
|
|
//}
|
2022-03-28 10:19:57 +02:00
|
|
|
|
2022-05-01 22:47:00 +02:00
|
|
|
// if id !in repos {
|
|
|
|
// return app.not_found()
|
|
|
|
//}
|
2022-03-28 10:19:57 +02:00
|
|
|
|
2022-05-01 22:47:00 +02:00
|
|
|
// repo := repos[id]
|
|
|
|
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-05-01 22:47:00 +02:00
|
|
|
// id := rand.uuid_v4()
|
2022-02-21 22:22:36 +01:00
|
|
|
|
2022-05-01 22:47:00 +02:00
|
|
|
// mut repos := rlock app.git_mutex {
|
|
|
|
// git.read_repos(app.conf.repos_file) or {
|
|
|
|
// app.lerror('Failed to read repos file.')
|
2022-02-21 22:22:36 +01:00
|
|
|
|
2022-05-01 22:47:00 +02:00
|
|
|
// return app.status(http.Status.internal_server_error)
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
// repos := app.db.get_git_repos()
|
2022-02-21 22:22:36 +01:00
|
|
|
|
2022-05-01 22:47:00 +02:00
|
|
|
//// We need to check for duplicates
|
|
|
|
// for _, repo in repos {
|
|
|
|
// if repo == new_repo {
|
|
|
|
// return app.json(http.Status.bad_request, new_response('Duplicate repository.'))
|
|
|
|
// }
|
|
|
|
//}
|
2022-02-21 22:22:36 +01:00
|
|
|
|
2022-05-01 22:47:00 +02:00
|
|
|
// repos[id] = new_repo
|
|
|
|
|
|
|
|
// lock app.git_mutex {
|
|
|
|
// git.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-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-01 22:47:00 +02:00
|
|
|
/* mut repos := rlock app.git_mutex { */
|
|
|
|
/* git.read_repos(app.conf.repos_file) or { */
|
|
|
|
/* app.lerror('Failed to read repos file.') */
|
2022-03-28 10:19:57 +02:00
|
|
|
|
2022-05-01 22:47:00 +02:00
|
|
|
/* return app.status(http.Status.internal_server_error) */
|
|
|
|
/* } */
|
|
|
|
/* } */
|
2022-03-28 10:19:57 +02:00
|
|
|
|
2022-05-01 22:47:00 +02:00
|
|
|
/* if id !in repos { */
|
|
|
|
/* return app.not_found() */
|
|
|
|
/* } */
|
2022-03-28 10:19:57 +02:00
|
|
|
|
2022-05-01 22:47:00 +02:00
|
|
|
/* repos.delete(id) */
|
|
|
|
app.db.delete_git_repo(id)
|
2022-03-28 10:19:57 +02:00
|
|
|
|
2022-05-01 22:47:00 +02:00
|
|
|
/* lock app.git_mutex { */
|
|
|
|
/* git.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
|
|
|
}
|
|
|
|
|
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-02 20:59:53 +02:00
|
|
|
if 'arch' in app.query {
|
|
|
|
arch_objs := app.query['arch'].split(',').map(db.GitRepoArch{value: it})
|
|
|
|
|
|
|
|
app.db.update_git_repo_archs(id, arch_objs)
|
2022-02-21 22:40:59 +01:00
|
|
|
}
|
2022-03-28 10:19:57 +02:00
|
|
|
|
2022-05-02 20:59:53 +02:00
|
|
|
/* mut repos := rlock app.git_mutex { */
|
|
|
|
/* git.read_repos(app.conf.repos_file) or { */
|
|
|
|
/* app.lerror('Failed to read repos file.') */
|
2022-03-28 10:19:57 +02:00
|
|
|
|
2022-05-02 20:59:53 +02:00
|
|
|
/* return app.status(http.Status.internal_server_error) */
|
|
|
|
/* } */
|
|
|
|
/* } */
|
2022-02-21 22:40:59 +01:00
|
|
|
|
2022-05-02 20:59:53 +02:00
|
|
|
/* if id !in repos { */
|
|
|
|
/* return app.not_found() */
|
|
|
|
/* } */
|
|
|
|
|
|
|
|
/* repos[id].patch_from_params(app.query) */
|
|
|
|
|
|
|
|
/* lock app.git_mutex { */
|
|
|
|
/* git.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
|
|
|
}
|