From 139142fcec4f66ad08e95ed4dcf6c3c30acc423e Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sat, 7 May 2022 15:41:49 +0200 Subject: [PATCH] feat(server): added endpoint for content of build log --- src/db/logs.v | 2 ++ src/server/logs.v | 27 ++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/db/logs.v b/src/db/logs.v index 9a2405b4..589304e7 100644 --- a/src/db/logs.v +++ b/src/db/logs.v @@ -3,10 +3,12 @@ module db import time pub struct BuildLog { +pub: id int [primary; sql: serial] repo_id int [nonull] start_time time.Time [nonull] end_time time.Time [nonull] + arch string [nonull] exit_code int [nonull] } diff --git a/src/server/logs.v b/src/server/logs.v index 8b0f297f..9bddc210 100644 --- a/src/server/logs.v +++ b/src/server/logs.v @@ -24,6 +24,30 @@ fn (mut app App) get_logs() web.Result { return app.json(http.Status.ok, new_data_response(logs)) } +['/api/logs/:id'; get] +fn (mut app App) get_single_log(id int) web.Result { + if !app.is_authorized() { + return app.json(http.Status.unauthorized, new_response('Unauthorized.')) + } + + log := app.db.get_build_log(id) or { return app.not_found() } + + return app.json(http.Status.ok, new_data_response(log)) +} + +['/api/logs/:id/content'; get] +fn (mut app App) get_log_contents(id int) web.Result { + if !app.is_authorized() { + return app.json(http.Status.unauthorized, new_response('Unauthorized.')) + } + + log := app.db.get_build_log(id) or { return app.not_found() } + file_name := log.start_time.custom_format('YYYY-MM-DD_HH-mm-ss') + full_path := os.join_path(app.conf.data_dir, server.logs_dir_name, log.repo_id.str(), log.arch, file_name) + + return app.file(full_path) +} + // parse_query_time unescapes an HTTP query parameter & tries to parse it as a // time.Time struct. fn parse_query_time(query string) ?time.Time { @@ -44,7 +68,7 @@ fn (mut app App) post_log() web.Result { return app.json(http.Status.bad_request, new_response('Invalid or missing start time.')) } - end_time := time.parse(app.query['endTime'].replace('_', ' ')) or { + end_time := parse_query_time(app.query['endTime']) or { return app.json(http.Status.bad_request, new_response('Invalid or missing end time.')) } @@ -71,6 +95,7 @@ fn (mut app App) post_log() web.Result { repo_id: repo start_time: start_time end_time: end_time + arch: arch exit_code: exit_code }