93 lines
1.4 KiB
V
93 lines
1.4 KiB
V
module main
|
|
|
|
import vweb
|
|
import json
|
|
import time
|
|
|
|
struct ResultStatus {
|
|
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 {
|
|
app.redirect(app.config.redirect_url)
|
|
}
|
|
}
|
|
|
|
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"
|
|
}
|
|
)
|
|
}
|
|
|
|
scores := app.get_scores()
|
|
|
|
return app.json(scores)
|
|
}
|
|
|
|
[post]
|
|
['/api/v1/score/submit']
|
|
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"
|
|
}
|
|
)
|
|
}
|
|
|
|
body := json.decode(Score, app.req.data) or {
|
|
app.set_status(400, '')
|
|
return app.json(
|
|
ErrorStatus {
|
|
400,
|
|
"Bad Request",
|
|
"Bad JSON object"
|
|
}
|
|
)
|
|
}
|
|
|
|
app.insert_score(ScoreRes{player:body.player, score:body.score, time:time.now().unix_time()})
|
|
|
|
return app.json(
|
|
ResultStatus {
|
|
200,
|
|
"Success"
|
|
}
|
|
)
|
|
}
|
|
|
|
fn (mut app App) auth() bool {
|
|
auth_header := app.get_header('Authorization')
|
|
token := auth_header.after('Bearer ')
|
|
|
|
mut config_token := ""
|
|
|
|
rlock app.config {
|
|
config_token = app.config.token
|
|
}
|
|
|
|
return config_token.len != 0 && token == config_token
|
|
}
|