Formatting, Simplified status, Return inserted score
This commit is contained in:
parent
20fecb22da
commit
318b1d3dc8
4 changed files with 24 additions and 52 deletions
14
src/config.v
14
src/config.v
|
@ -3,7 +3,7 @@ module main
|
||||||
import toml
|
import toml
|
||||||
|
|
||||||
const (
|
const (
|
||||||
path = "./config.toml"
|
path = './config.toml'
|
||||||
)
|
)
|
||||||
|
|
||||||
struct Config {
|
struct Config {
|
||||||
|
@ -23,11 +23,11 @@ fn load_config() Config {
|
||||||
config := toml.parse_file(path) or { panic(err) }
|
config := toml.parse_file(path) or { panic(err) }
|
||||||
|
|
||||||
return Config {
|
return Config {
|
||||||
host: config.value("host").string()
|
host: config.value('host').string()
|
||||||
port: config.value("port").int()
|
port: config.value('port').int()
|
||||||
token: config.value("token").string()
|
token: config.value('token').string()
|
||||||
redirect: config.value("redirect").bool()
|
redirect: config.value('redirect').bool()
|
||||||
redirect_url: config.value("redirect_url").string()
|
redirect_url: config.value('redirect_url').string()
|
||||||
db_path: config.value("db_path").string()
|
db_path: config.value('db_path').string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,22 +10,23 @@ pub mut:
|
||||||
|
|
||||||
fn (mut app App) create_tables() int {
|
fn (mut app App) create_tables() int {
|
||||||
return app.db.exec_none(
|
return app.db.exec_none(
|
||||||
"CREATE TABLE IF NOT EXISTS ScoreRes (
|
'CREATE TABLE IF NOT EXISTS ScoreRes (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
player TEXT NOT NULL,
|
player TEXT NOT NULL,
|
||||||
score INTEGER NOT NULL,
|
score INTEGER NOT NULL,
|
||||||
time SQLITE_INT64_TYPE 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 }
|
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 {
|
fn (mut app App) get_scores() []ScoreRes {
|
||||||
scores := sql app.db { select from ScoreRes order by score desc }
|
return sql app.db { select from ScoreRes order by score desc }
|
||||||
return scores
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut app App) delete_score(score_id int) {
|
fn (mut app App) delete_score(score_id int) {
|
||||||
|
|
|
@ -23,7 +23,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !os.exists(app_config.db_path) {
|
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) }
|
mut file := os.create(app_config.db_path) or { panic(err) }
|
||||||
file.close()
|
file.close()
|
||||||
}
|
}
|
||||||
|
@ -58,8 +58,8 @@ fn main() {
|
||||||
|
|
||||||
fn (mut app App) shutdown(sig os.Signal) {
|
fn (mut app App) shutdown(sig os.Signal) {
|
||||||
app.db.close() or { panic(err) }
|
app.db.close() or { panic(err) }
|
||||||
println("Shut down database gracefully")
|
println('Shut down database gracefully')
|
||||||
println("Exiting...")
|
println('Exiting...')
|
||||||
time.sleep(1e+9) // Sleep one second
|
time.sleep(1e+9) // Sleep one second
|
||||||
exit(0)
|
exit(0)
|
||||||
}
|
}
|
||||||
|
|
45
src/web.v
45
src/web.v
|
@ -4,17 +4,11 @@ import vweb
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
|
|
||||||
struct ResultStatus {
|
struct Status {
|
||||||
status int
|
status int
|
||||||
message string
|
message string
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ErrorStatus {
|
|
||||||
status int
|
|
||||||
error string
|
|
||||||
message string
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn (mut app App) index() vweb.Result {
|
pub fn (mut app App) index() vweb.Result {
|
||||||
rlock app.config {
|
rlock app.config {
|
||||||
if app.config.redirect {
|
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']
|
['/api/v1/score/list']
|
||||||
pub fn (mut app App) score_list() vweb.Result {
|
pub fn (mut app App) score_list() vweb.Result {
|
||||||
if !app.auth() {
|
if !app.auth() {
|
||||||
app.set_status(401, '')
|
app.set_status(401, '')
|
||||||
return app.json(
|
return app.json(Status{401, 'OAuth token is missing'})
|
||||||
ErrorStatus {
|
|
||||||
401,
|
|
||||||
"Unauthorized",
|
|
||||||
"OAuth token is missing"
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
scores := app.get_scores()
|
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 {
|
pub fn (mut app App) score_submit() vweb.Result {
|
||||||
if !app.auth() {
|
if !app.auth() {
|
||||||
app.set_status(401, '')
|
app.set_status(401, '')
|
||||||
return app.json(
|
return app.json(Status {401, 'OAuth token is missing'})
|
||||||
ErrorStatus {
|
|
||||||
401,
|
|
||||||
"Unauthorized",
|
|
||||||
"OAuth token is missing"
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
body := json.decode(Score, app.req.data) or {
|
body := json.decode(Score, app.req.data) or {
|
||||||
app.set_status(400, '')
|
app.set_status(400, '')
|
||||||
return app.json(
|
return app.json(Status{400, 'Bad JSON object'})
|
||||||
ErrorStatus {
|
|
||||||
400,
|
|
||||||
"Bad Request",
|
|
||||||
"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(
|
return app.json(score)
|
||||||
ResultStatus {
|
|
||||||
200,
|
|
||||||
"Success"
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut app App) auth() bool {
|
fn (mut app App) auth() bool {
|
||||||
auth_header := app.get_header('Authorization')
|
auth_header := app.get_header('Authorization')
|
||||||
token := auth_header.after('Bearer ')
|
token := auth_header.after('Bearer ')
|
||||||
|
|
||||||
mut config_token := ""
|
mut config_token := ''
|
||||||
|
|
||||||
rlock app.config {
|
rlock app.config {
|
||||||
config_token = app.config.token
|
config_token = app.config.token
|
||||||
|
|
Loading…
Reference in a new issue