commit 23a0c77d56ccf95857d7065b20c1952e5ff3068a Author: Edgar Date: Mon Mar 7 17:57:52 2022 +0100 :tada: First commit diff --git a/load-test.js b/load-test.js new file mode 100644 index 0000000..248b133 --- /dev/null +++ b/load-test.js @@ -0,0 +1,24 @@ +import http from "k6/http"; +import { sleep } from 'k6'; + +export default function () { + for (let i = 0; i < 250; i++) { + var rand = Math.random() + if (rand > 0.5) { + http.get(`http://127.0.0.1:8080/get/${random_string(1)}`); + } else { + http.post(`http://127.0.0.1:8080/set/${random_string(1)}`, random_string(5)); + } + } +}; + + +function random_string(length) { + var result = ''; + var characters = 'abcdefghijklmnopqrstuvwxyz'; + var charactersLength = characters.length; + for (var i = 0; i < length; i++) { + result += characters.charAt(Math.floor(Math.random() * charactersLength)); + } + return result; +} \ No newline at end of file diff --git a/load-test2.js b/load-test2.js new file mode 100644 index 0000000..90055c7 --- /dev/null +++ b/load-test2.js @@ -0,0 +1,5 @@ +import http from "k6/http"; + +export default function () { + http.get(`http://localhost:8082/`); +}; \ No newline at end of file diff --git a/stupid-kv - Copy.v b/stupid-kv - Copy.v new file mode 100644 index 0000000..7d3b079 --- /dev/null +++ b/stupid-kv - Copy.v @@ -0,0 +1,41 @@ +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') + vweb.run(app, 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'; post] +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') +} diff --git a/stupid-kv.v b/stupid-kv.v new file mode 100644 index 0000000..3d63834 --- /dev/null +++ b/stupid-kv.v @@ -0,0 +1,59 @@ +module main + +import vweb +import json + +const ( + port = 8080 +) + +struct App { + vweb.Context +mut: + state shared State +} + +struct State { +mut: + kv_store string +} + +fn main() { + mut app := &App{} + println('stupid kvstore') + lock app.state { + app.state.kv_store = '{}' + } + //vweb.run(app, port) + 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 { + // data := json.decode(map[string]string, app.state.kv_store) or { + // return app.server_error(500) + // } + // if key in data { + // r := data[key] + // return app.ok(r) + // } else { + // return app.not_found() + // } + + return app.ok(app.state.kv_store) + } + return app.server_error(500) +} + +['/set/:key'; post] +fn (mut app App) kv_set(key string) vweb.Result { + lock app.state { + // mut data := json.decode(map[string]string, app.state.kv_store) or { + // return app.server_error(500) + // } + // data[key] = app.req.data + app.state.kv_store = app.req.data//json.encode(data) + } + return app.text('OK') +} diff --git a/vweb_example.v b/vweb_example.v new file mode 100644 index 0000000..7f60086 --- /dev/null +++ b/vweb_example.v @@ -0,0 +1,17 @@ +module main + +import vweb + +struct App { + vweb.Context +} + + +fn main() { + mut app := &App{} + vweb.run_at(app, family: .ip, host: 'localhost', port: 8082) ? +} + +pub fn (mut app App) index() vweb.Result { + return app.text('World') +} \ No newline at end of file