Added proper logging system

main
Jef Roosens 2022-01-11 14:01:13 +01:00
parent dad2b64041
commit 09d1b5458f
Signed by untrusted user: Jef Roosens
GPG Key ID: 955C0660072F691F
3 changed files with 60 additions and 26 deletions

View File

@ -8,15 +8,14 @@ import repo
const port = 8000 const port = 8000
const buf_size = 1_000_000 const buf_size = 100_000
const db_name = 'pieter.db.tar.gz' const db_name = 'pieter.db.tar.gz'
struct App { struct App {
web.Context web.Context
api_key string [required; web_global] api_key string [required; web_global]
repo_dir string [required; web_global] repo_dir string [required; web_global]
logger log.Log [required; web_global]
} }
[noreturn] [noreturn]
@ -57,45 +56,35 @@ fn (mut app App) put_package(pkg string) web.Result {
full_path := os.join_path_single(app.repo_dir, pkg) full_path := os.join_path_single(app.repo_dir, pkg)
if os.exists(full_path) { if os.exists(full_path) {
app.lwarn("Tried to upload duplicate package '$pkg'")
return app.text('File already exists.') return app.text('File already exists.')
} }
if length := app.req.header.get(.content_length) { if length := app.req.header.get(.content_length) {
reader_to_file(mut app.reader, length.int(), full_path) or { reader_to_file(mut app.reader, length.int(), full_path) or {
app.lwarn("Failed to upload package '$pkg'")
return app.text('Failed to upload file.') return app.text('Failed to upload file.')
} }
} else { } else {
app.lwarn("Tried to upload package '$pkg' without specifying a Content-Length.")
return app.text("Content-Type header isn't set.") return app.text("Content-Type header isn't set.")
} }
repo.add_package(os.join_path_single(app.repo_dir, db_name), full_path) or { repo.add_package(os.join_path_single(app.repo_dir, db_name), full_path) or {
app.linfo("Failed to add package '$pkg' to database.")
os.rm(full_path) or { println('Failed to remove $full_path') } os.rm(full_path) or { println('Failed to remove $full_path') }
return app.text('Failed to add package to repo.') return app.text('Failed to add package to repo.')
} }
app.linfo("Uploaded package '$pkg'.")
return app.text('Package added successfully.') return app.text('Package added successfully.')
} }
// ['/publish'; post]
// fn (mut app App) put_package(filename string) web.Result {
// for _, files in app.files {
// for file in files {
// filepath := os.join_path_single(app.repo_dir, file.filename)
// if os.exists(filepath) {
// return app.text('File already exists.')
// }
// os.write_file(filepath, file.data) or { return app.text('Failed to upload file.') }
// return app.text('yeet')
// }
// }
// return app.text('done')
// }
fn main() { fn main() {
// Configure logger // Configure logger
log_level_str := os.getenv_opt('LOG_LEVEL') or { 'WARN' } log_level_str := os.getenv_opt('LOG_LEVEL') or { 'WARN' }
@ -107,16 +96,18 @@ fn main() {
mut logger := log.Log{ mut logger := log.Log{
level: log_level level: log_level
} }
logger.set_full_logpath(log_file) logger.set_full_logpath(log_file)
logger.log_to_console_too() logger.log_to_console_too()
// logger.set
logger.debug('Logger set up.')
logger.flush()
defer { defer {
logger.info('Flushing log file') logger.info('Flushing log file')
logger.flush() logger.flush()
logger.close() logger.close()
} }
// logger.set
logger.info('Logger set up.')
logger.flush()
// Configure web server // Configure web server
key := os.getenv_opt('API_KEY') or { exit_with_message(1, 'No API key was provided.') } key := os.getenv_opt('API_KEY') or { exit_with_message(1, 'No API key was provided.') }

View File

@ -0,0 +1,29 @@
module web
import log
pub fn (mut ctx Context) log(msg &string, level log.Level) {
lock ctx.logger {
ctx.logger.send_output(msg, level)
}
}
pub fn (mut ctx Context) lfatal(msg &string) {
ctx.log(msg, log.Level.fatal)
}
pub fn (mut ctx Context) lerror(msg &string) {
ctx.log(msg, log.Level.error)
}
pub fn (mut ctx Context) lwarn(msg &string) {
ctx.log(msg, log.Level.warn)
}
pub fn (mut ctx Context) linfo(msg &string) {
ctx.log(msg, log.Level.info)
}
pub fn (mut ctx Context) ldebug(msg &string) {
ctx.log(msg, log.Level.debug)
}

View File

@ -10,6 +10,7 @@ import net.http
import net.urllib import net.urllib
import time import time
import json import json
import log
// A type which don't get filtered inside templates // A type which don't get filtered inside templates
pub type RawHtml = string pub type RawHtml = string
@ -169,6 +170,8 @@ pub mut:
form_error string form_error string
// Allows reading the request body // Allows reading the request body
reader io.BufferedReader reader io.BufferedReader
// Gives access to a shared logger object
logger shared log.Log
} }
struct FileData { struct FileData {
@ -424,8 +427,14 @@ pub fn run<T>(global_app &T, port int) {
fn handle_conn<T>(mut conn net.TcpConn, mut app T, routes map[string]Route) { fn handle_conn<T>(mut conn net.TcpConn, mut app T, routes map[string]Route) {
conn.set_read_timeout(30 * time.second) conn.set_read_timeout(30 * time.second)
conn.set_write_timeout(30 * time.second) conn.set_write_timeout(30 * time.second)
defer { defer {
conn.close() or {} conn.close() or {}
lock app.logger {
app.logger.flush()
}
unsafe { unsafe {
free(app) free(app)
} }
@ -447,6 +456,10 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T, routes map[string]Route) {
return return
} }
lock app.logger {
app.logger.debug('$head.method $head.url $head.version')
}
// req := http.parse_request(mut reader) or { // req := http.parse_request(mut reader) or {
// // Prevents errors from being thrown when BufferedReader is empty // // Prevents errors from being thrown when BufferedReader is empty
// if '$err' != 'none' { // if '$err' != 'none' {
@ -483,6 +496,7 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T, routes map[string]Route) {
static_files: app.static_files static_files: app.static_files
static_mime_types: app.static_mime_types static_mime_types: app.static_mime_types
reader: reader reader: reader
logger: app.logger
} }
// Calling middleware... // Calling middleware...