Compare commits
No commits in common. "7749b7c159c93624ef5e8a3663846f6790516468" and "ba7938dcf433fdee36f8be08a2026d506035e22f" have entirely different histories.
7749b7c159
...
ba7938dcf4
8 changed files with 19 additions and 92 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,4 +1,3 @@
|
||||||
/config.toml
|
/config.toml
|
||||||
/compose.yml
|
|
||||||
/highscore-server
|
/highscore-server
|
||||||
/db
|
/db
|
||||||
|
|
|
@ -2,12 +2,10 @@ FROM thevlang/vlang:alpine AS build
|
||||||
|
|
||||||
WORKDIR /tmp/app
|
WORKDIR /tmp/app
|
||||||
COPY . .
|
COPY . .
|
||||||
RUN v -prod -o highscore-server .
|
RUN v -prod .
|
||||||
|
|
||||||
FROM alpine:latest
|
FROM alpine:latest
|
||||||
|
|
||||||
RUN apk --no-cache add sqlite-dev
|
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY --from=build /tmp/app/highscore-server .
|
COPY --from=build /tmp/app/highscore-server .
|
||||||
COPY --from=build /tmp/app/config.example.toml ./config.toml
|
COPY --from=build /tmp/app/config.example.toml ./config.toml
|
||||||
|
|
|
@ -1,15 +0,0 @@
|
||||||
services:
|
|
||||||
highscore-server:
|
|
||||||
build:
|
|
||||||
context: .
|
|
||||||
dockerfile: Dockerfile
|
|
||||||
image: git.snrd.eu/sunred/highscore-server:latest
|
|
||||||
container_name: highscore-server
|
|
||||||
network_mode: bridge
|
|
||||||
volumes:
|
|
||||||
- ./config.toml:/app/config.toml:ro
|
|
||||||
- ./db:/app/db:rw
|
|
||||||
ports:
|
|
||||||
- "127.0.0.1:8080:8080/tcp"
|
|
||||||
- "[::1]:8080:8080/tcp"
|
|
||||||
restart: unless-stopped
|
|
|
@ -1,17 +0,0 @@
|
||||||
variable "REG" {
|
|
||||||
default = "git.snrd.eu"
|
|
||||||
}
|
|
||||||
variable "REPO" {
|
|
||||||
default = "sunred/higscore-server"
|
|
||||||
}
|
|
||||||
variable "TAG" {
|
|
||||||
default = "latest"
|
|
||||||
}
|
|
||||||
|
|
||||||
group "default" {
|
|
||||||
targets = ["production"]
|
|
||||||
}
|
|
||||||
target "production" {
|
|
||||||
output = ["type=docker"]
|
|
||||||
tags = ["${REG}/${REPO}:latest", "${REG}/${REPO}:${TAG}"]
|
|
||||||
}
|
|
|
@ -15,10 +15,6 @@ struct Config {
|
||||||
db_path string
|
db_path string
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (config Config) copy() Config {
|
|
||||||
return config
|
|
||||||
}
|
|
||||||
|
|
||||||
fn load_config() Config {
|
fn load_config() Config {
|
||||||
config := toml.parse_file(path) or { panic(err) }
|
config := toml.parse_file(path) or { panic(err) }
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ pub mut:
|
||||||
time i64
|
time i64
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut app App) create_tables() int {
|
fn (mut app App) create_tables() bool {
|
||||||
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,
|
||||||
|
@ -16,7 +16,7 @@ fn (mut app App) create_tables() int {
|
||||||
score INTEGER NOT NULL,
|
score INTEGER NOT NULL,
|
||||||
time SQLITE_INT64_TYPE NOT NULL
|
time SQLITE_INT64_TYPE NOT NULL
|
||||||
)"
|
)"
|
||||||
)
|
) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut app App) insert_score(score ScoreRes) {
|
fn (mut app App) insert_score(score ScoreRes) {
|
||||||
|
|
44
src/main.v
44
src/main.v
|
@ -2,64 +2,36 @@ module main
|
||||||
|
|
||||||
import vweb
|
import vweb
|
||||||
import sqlite
|
import sqlite
|
||||||
import time
|
|
||||||
import os
|
|
||||||
|
|
||||||
struct App {
|
struct App {
|
||||||
vweb.Context
|
vweb.Context
|
||||||
pub mut:
|
pub mut:
|
||||||
db sqlite.DB
|
db sqlite.DB
|
||||||
config shared Config
|
config Config
|
||||||
is_admin bool
|
is_admin bool
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
mut app := &App{}
|
mut app := &App{}
|
||||||
mut app_config := Config{}
|
|
||||||
|
|
||||||
lock app.config {
|
|
||||||
app.config = load_config()
|
app.config = load_config()
|
||||||
app_config = app.config.copy()
|
|
||||||
}
|
|
||||||
|
|
||||||
if !os.exists(app_config.db_path) {
|
app.db = sqlite.connect(app.config.db_path) or {
|
||||||
println("Creating database file at " + app_config.db_path)
|
println('Database Error!')
|
||||||
mut file := os.create(app_config.db_path) or { panic(err) }
|
|
||||||
file.close()
|
|
||||||
}
|
|
||||||
|
|
||||||
app.db = sqlite.connect(app_config.db_path) or {
|
|
||||||
println('Database error!')
|
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
sql_code := app.create_tables()
|
app.create_tables()
|
||||||
|
|
||||||
if sqlite.is_error(sql_code) {
|
|
||||||
println('Could not create tables!')
|
|
||||||
panic('Error code ' + sql_code.str())
|
|
||||||
}
|
|
||||||
|
|
||||||
mut host := '::'
|
mut host := '::'
|
||||||
if app_config.host != "" {
|
|
||||||
host = app_config.host
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle graceful shutdown for Docker
|
if app.config.host != "" {
|
||||||
os.signal_opt(os.Signal.int, app.shutdown) or { panic(err) }
|
host = app.config.host
|
||||||
os.signal_opt(os.Signal.term, app.shutdown) or { panic(err) }
|
}
|
||||||
|
|
||||||
vweb.run_at(app, vweb.RunParams{
|
vweb.run_at(app, vweb.RunParams{
|
||||||
host: host
|
host: host
|
||||||
port: app_config.port
|
port: app.config.port
|
||||||
family: .ip6
|
family: .ip6
|
||||||
}) or { panic(err) }
|
}) or { panic(err) }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut app App) shutdown(sig os.Signal) {
|
|
||||||
app.db.close() or { panic(err) }
|
|
||||||
println("Shut down database gracefully")
|
|
||||||
println("Exiting...")
|
|
||||||
time.sleep(1e+9) // Sleep one second
|
|
||||||
exit(0)
|
|
||||||
}
|
|
||||||
|
|
16
src/web.v
16
src/web.v
|
@ -4,7 +4,7 @@ import vweb
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
|
|
||||||
struct ResultStatus {
|
struct Status {
|
||||||
status int
|
status int
|
||||||
message string
|
message string
|
||||||
}
|
}
|
||||||
|
@ -16,11 +16,9 @@ struct ErrorStatus {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut app App) index() vweb.Result {
|
pub fn (mut app App) index() vweb.Result {
|
||||||
rlock app.config {
|
|
||||||
if app.config.redirect {
|
if app.config.redirect {
|
||||||
app.redirect(app.config.redirect_url)
|
app.redirect(app.config.redirect_url)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return app.text("Hello :)")
|
return app.text("Hello :)")
|
||||||
}
|
}
|
||||||
|
@ -71,7 +69,7 @@ pub fn (mut app App) score_submit() vweb.Result {
|
||||||
app.insert_score(ScoreRes{player:body.player, score:body.score, time:time.now().unix_time()})
|
app.insert_score(ScoreRes{player:body.player, score:body.score, time:time.now().unix_time()})
|
||||||
|
|
||||||
return app.json(
|
return app.json(
|
||||||
ResultStatus {
|
Status {
|
||||||
200,
|
200,
|
||||||
"Success"
|
"Success"
|
||||||
}
|
}
|
||||||
|
@ -79,14 +77,10 @@ pub fn (mut app App) score_submit() vweb.Result {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut app App) auth() bool {
|
fn (mut app App) auth() bool {
|
||||||
|
app.config = load_config()
|
||||||
|
|
||||||
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 := ""
|
return token == app.config.token
|
||||||
|
|
||||||
rlock app.config {
|
|
||||||
config_token = app.config.token
|
|
||||||
}
|
|
||||||
|
|
||||||
return config_token.len != 0 && token == config_token
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue