diff --git a/src/config.v b/src/config.v index 01cd7cd..0ed7e5a 100644 --- a/src/config.v +++ b/src/config.v @@ -3,7 +3,7 @@ module main import toml const ( - path = "./config.toml" + path = './config.toml' ) struct Config { @@ -23,11 +23,11 @@ fn load_config() Config { config := toml.parse_file(path) or { panic(err) } return Config { - host: config.value("host").string() - port: config.value("port").int() - token: config.value("token").string() - redirect: config.value("redirect").bool() - redirect_url: config.value("redirect_url").string() - db_path: config.value("db_path").string() + host: config.value('host').string() + port: config.value('port').int() + token: config.value('token').string() + redirect: config.value('redirect').bool() + redirect_url: config.value('redirect_url').string() + db_path: config.value('db_path').string() } } diff --git a/src/database.v b/src/database.v index 2704a93..6fe454d 100644 --- a/src/database.v +++ b/src/database.v @@ -10,22 +10,23 @@ pub mut: fn (mut app App) create_tables() int { return app.db.exec_none( - "CREATE TABLE IF NOT EXISTS ScoreRes ( + 'CREATE TABLE IF NOT EXISTS ScoreRes ( id INTEGER PRIMARY KEY, player TEXT NOT NULL, score INTEGER NOT NULL, time SQLITE_INT64_TYPE NOT NULL - )" + )' ) } -fn (mut app App) insert_score(score ScoreRes) { +fn (mut app App) insert_score(score ScoreRes) ScoreRes { sql app.db { insert score into ScoreRes } + last_row_id := app.db.last_insert_rowid() + return sql app.db { select from ScoreRes where id == last_row_id } } fn (mut app App) get_scores() []ScoreRes { - scores := sql app.db { select from ScoreRes order by score desc } - return scores + return sql app.db { select from ScoreRes order by score desc } } fn (mut app App) delete_score(score_id int) { diff --git a/src/main.v b/src/main.v index fed1520..6679625 100644 --- a/src/main.v +++ b/src/main.v @@ -23,7 +23,7 @@ fn main() { } if !os.exists(app_config.db_path) { - println("Creating database file at " + app_config.db_path) + println('Creating database file at ' + app_config.db_path) mut file := os.create(app_config.db_path) or { panic(err) } file.close() } @@ -58,8 +58,8 @@ fn main() { fn (mut app App) shutdown(sig os.Signal) { app.db.close() or { panic(err) } - println("Shut down database gracefully") - println("Exiting...") + println('Shut down database gracefully') + println('Exiting...') time.sleep(1e+9) // Sleep one second exit(0) } diff --git a/src/web.v b/src/web.v index 8760f92..a1810e2 100644 --- a/src/web.v +++ b/src/web.v @@ -4,17 +4,11 @@ import vweb import json import time -struct ResultStatus { +struct Status { status int message string } -struct ErrorStatus { - status int - error string - message string -} - pub fn (mut app App) index() vweb.Result { rlock app.config { if app.config.redirect { @@ -22,20 +16,14 @@ pub fn (mut app App) index() vweb.Result { } } - return app.text("Hello :)") + return app.text('Hello :)') } ['/api/v1/score/list'] pub fn (mut app App) score_list() vweb.Result { if !app.auth() { app.set_status(401, '') - return app.json( - ErrorStatus { - 401, - "Unauthorized", - "OAuth token is missing" - } - ) + return app.json(Status{401, 'OAuth token is missing'}) } scores := app.get_scores() @@ -48,41 +36,24 @@ pub fn (mut app App) score_list() vweb.Result { pub fn (mut app App) score_submit() vweb.Result { if !app.auth() { app.set_status(401, '') - return app.json( - ErrorStatus { - 401, - "Unauthorized", - "OAuth token is missing" - } - ) + return app.json(Status {401, 'OAuth token is missing'}) } body := json.decode(Score, app.req.data) or { app.set_status(400, '') - return app.json( - ErrorStatus { - 400, - "Bad Request", - "Bad JSON object" - } - ) + return app.json(Status{400, 'Bad JSON object'}) } - app.insert_score(ScoreRes{player:body.player, score:body.score, time:time.now().unix_time()}) + score := app.insert_score(ScoreRes{player:body.player, score:body.score, time:time.now().unix_time()}) - return app.json( - ResultStatus { - 200, - "Success" - } - ) + return app.json(score) } fn (mut app App) auth() bool { auth_header := app.get_header('Authorization') token := auth_header.after('Bearer ') - mut config_token := "" + mut config_token := '' rlock app.config { config_token = app.config.token