2022-02-21 22:22:36 +01:00
|
|
|
module server
|
|
|
|
|
|
|
|
import web
|
2022-09-11 20:34:02 +02:00
|
|
|
import web.response { new_data_response, new_response }
|
2022-06-14 22:25:40 +02:00
|
|
|
import models { Target, TargetArch, TargetFilter }
|
2022-02-21 22:22:36 +01:00
|
|
|
|
2022-06-14 22:25:40 +02:00
|
|
|
// v1_get_targets returns the current list of targets.
|
2022-12-22 23:10:10 +01:00
|
|
|
['/api/v1/targets'; auth; get; markused]
|
2022-06-14 20:31:00 +02:00
|
|
|
fn (mut app App) v1_get_targets() web.Result {
|
2023-02-08 11:00:17 +01:00
|
|
|
filter := models.from_params[TargetFilter](app.query) or {
|
2022-12-15 10:46:58 +01:00
|
|
|
return app.json(.bad_request, new_response('Invalid query parameters.'))
|
2022-05-17 13:50:46 +02:00
|
|
|
}
|
2022-12-31 16:10:47 +01:00
|
|
|
mut iter := app.db.targets(filter)
|
2022-02-21 22:22:36 +01:00
|
|
|
|
2022-12-31 16:10:47 +01:00
|
|
|
return app.json(.ok, new_data_response(iter.collect()))
|
2022-02-21 22:22:36 +01:00
|
|
|
}
|
|
|
|
|
2022-06-14 22:25:40 +02:00
|
|
|
// v1_get_single_target returns the information for a single target.
|
2022-12-22 23:10:10 +01:00
|
|
|
['/api/v1/targets/:id'; auth; get; markused]
|
2022-06-14 20:31:00 +02:00
|
|
|
fn (mut app App) v1_get_single_target(id int) web.Result {
|
2022-12-15 10:46:58 +01:00
|
|
|
target := app.db.get_target(id) or { return app.status(.not_found) }
|
2022-03-28 10:19:57 +02:00
|
|
|
|
2022-12-13 13:58:51 +01:00
|
|
|
return app.json(.ok, new_data_response(target))
|
2022-03-28 10:19:57 +02:00
|
|
|
}
|
|
|
|
|
2022-06-14 22:25:40 +02:00
|
|
|
// v1_post_target creates a new target from the provided query string.
|
2022-12-22 23:10:10 +01:00
|
|
|
['/api/v1/targets'; auth; markused; post]
|
2022-06-14 20:31:00 +02:00
|
|
|
fn (mut app App) v1_post_target() web.Result {
|
2022-05-01 12:44:54 +02:00
|
|
|
mut params := app.query.clone()
|
|
|
|
|
2022-12-13 13:58:51 +01:00
|
|
|
// If a target is created without specifying the arch, we assume it's meant
|
2022-05-01 12:44:54 +02:00
|
|
|
// for the default architecture.
|
2022-11-07 21:13:40 +01:00
|
|
|
if 'arch' !in params || params['arch'] == '' {
|
2022-05-01 12:44:54 +02:00
|
|
|
params['arch'] = app.conf.default_arch
|
|
|
|
}
|
|
|
|
|
2023-02-08 11:00:17 +01:00
|
|
|
mut new_target := models.from_params[Target](params) or {
|
2022-12-15 10:46:58 +01:00
|
|
|
return app.json(.bad_request, new_response(err.msg()))
|
2022-02-21 22:22:36 +01:00
|
|
|
}
|
|
|
|
|
2022-06-17 13:56:38 +02:00
|
|
|
// Ensure someone doesn't submit an invalid kind
|
2022-12-13 13:58:51 +01:00
|
|
|
if new_target.kind !in models.valid_kinds {
|
2022-12-15 10:46:58 +01:00
|
|
|
return app.json(.bad_request, new_response('Invalid kind.'))
|
2022-06-17 13:56:38 +02:00
|
|
|
}
|
|
|
|
|
2022-12-13 13:58:51 +01:00
|
|
|
id := app.db.add_target(new_target)
|
|
|
|
new_target.id = id
|
|
|
|
|
|
|
|
// Add the target to the job queue
|
|
|
|
// TODO return better error here if it's the cron schedule that's incorrect
|
|
|
|
app.job_queue.insert_all(new_target) or { return app.status(.internal_server_error) }
|
2022-02-21 22:22:36 +01:00
|
|
|
|
2022-11-07 21:35:49 +01:00
|
|
|
return app.json(.ok, new_data_response(id))
|
2022-02-21 22:22:36 +01:00
|
|
|
}
|
2022-02-21 22:40:59 +01:00
|
|
|
|
2022-06-14 22:25:40 +02:00
|
|
|
// v1_delete_target removes a given target from the server's list.
|
2022-12-22 23:10:10 +01:00
|
|
|
['/api/v1/targets/:id'; auth; delete; markused]
|
2022-06-14 20:31:00 +02:00
|
|
|
fn (mut app App) v1_delete_target(id int) web.Result {
|
2022-06-14 22:25:40 +02:00
|
|
|
app.db.delete_target(id)
|
2022-12-13 13:58:51 +01:00
|
|
|
app.job_queue.invalidate(id)
|
2022-03-28 10:19:57 +02:00
|
|
|
|
2022-12-15 10:46:58 +01:00
|
|
|
return app.status(.ok)
|
2022-03-28 10:19:57 +02:00
|
|
|
}
|
|
|
|
|
2022-06-14 22:25:40 +02:00
|
|
|
// v1_patch_target updates a target's data with the given query params.
|
2022-12-22 23:10:10 +01:00
|
|
|
['/api/v1/targets/:id'; auth; markused; patch]
|
2022-06-14 20:31:00 +02:00
|
|
|
fn (mut app App) v1_patch_target(id int) web.Result {
|
2022-06-14 22:25:40 +02:00
|
|
|
app.db.update_target(id, app.query)
|
2022-02-21 22:40:59 +01:00
|
|
|
|
2022-05-03 16:16:56 +02:00
|
|
|
if 'arch' in app.query {
|
2022-06-14 22:25:40 +02:00
|
|
|
arch_objs := app.query['arch'].split(',').map(TargetArch{ value: it })
|
2022-05-02 20:59:53 +02:00
|
|
|
|
2022-06-14 22:25:40 +02:00
|
|
|
app.db.update_target_archs(id, arch_objs)
|
2022-05-03 16:16:56 +02:00
|
|
|
}
|
2022-03-28 10:19:57 +02:00
|
|
|
|
2022-12-13 13:58:51 +01:00
|
|
|
target := app.db.get_target(id) or { return app.status(.internal_server_error) }
|
|
|
|
|
|
|
|
app.job_queue.invalidate(id)
|
|
|
|
app.job_queue.insert_all(target) or { return app.status(.internal_server_error) }
|
2022-11-07 21:35:49 +01:00
|
|
|
|
2022-12-13 13:58:51 +01:00
|
|
|
return app.json(.ok, new_data_response(target))
|
2022-02-21 22:40:59 +01:00
|
|
|
}
|