module main import vweb import json import time struct Status { status int 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(Status{401, 'OAuth token is missing'}) } scores := app.get_scores() return app.json(scores) } ['/api/v1/score/submit'; post] pub fn (mut app App) score_submit() vweb.Result { if !app.auth() { app.set_status(401, '') return app.json(Status{401, 'OAuth token is missing'}) } body := json.decode(Score, app.req.data) or { app.set_status(400, '') return app.json(Status{400, 'Bad JSON object'}) } score := app.insert_score(ScoreRes{ player: body.player score: body.score time: time.now().unix_time() }) return app.json(score) } 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 }