stupid-kv/stupid-kv.v
2025-01-21 10:21:18 +01:00

43 lines
685 B
V

module main
import vweb
const port = 8082
struct App {
vweb.Context
mut:
state shared State
}
struct State {
mut:
kv_store map[string]string = {}
}
fn main() {
mut app := &App{}
println('stupid kvstore')
lock app.state {
app.state.kv_store = {}
}
vweb.run_at(app, family: .ip, host: 'localhost', port: port)!
}
@['/get/:key']
pub fn (mut app App) kv_get(key string) vweb.Result {
lock app.state {
res := app.state.kv_store[key] or { return app.not_found() }
return app.ok(res)
}
return app.not_found()
}
@['/set/:key'; put]
fn (mut app App) kv_set(key string) vweb.Result {
lock app.state {
app.state.kv_store[key] = app.req.data
}
return app.text('OK')
}