2020-08-13 23:51:57 +02:00
|
|
|
module main
|
|
|
|
|
|
|
|
import os
|
|
|
|
import vweb
|
|
|
|
import time
|
|
|
|
|
2020-08-14 01:43:57 +02:00
|
|
|
const (
|
|
|
|
known_users = ['bilbo', 'kent']
|
|
|
|
)
|
|
|
|
|
2020-08-13 23:51:57 +02:00
|
|
|
struct App {
|
2020-12-31 17:22:47 +01:00
|
|
|
vweb.Context
|
2020-08-13 23:51:57 +02:00
|
|
|
port int
|
|
|
|
timeout int
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exit_after_timeout(timeout_in_ms int) {
|
2021-02-27 18:41:06 +01:00
|
|
|
time.sleep(timeout_in_ms * time.millisecond)
|
2020-08-13 23:51:57 +02:00
|
|
|
// eprintln('webserver is exiting ...')
|
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
if os.args.len != 3 {
|
|
|
|
panic('Usage: `vweb_test_server.exe PORT TIMEOUT_IN_MILLISECONDS`')
|
|
|
|
}
|
|
|
|
http_port := os.args[1].int()
|
|
|
|
assert http_port > 0
|
|
|
|
timeout := os.args[2].int()
|
|
|
|
assert timeout > 0
|
|
|
|
go exit_after_timeout(timeout)
|
|
|
|
//
|
|
|
|
mut app := App{
|
|
|
|
port: http_port
|
|
|
|
timeout: timeout
|
|
|
|
}
|
|
|
|
vweb.run_app<App>(mut app, http_port)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut app App) init() {
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut app App) init_once() {
|
2020-08-14 01:43:57 +02:00
|
|
|
eprintln('>> webserver: started on http://127.0.0.1:$app.port/ , with maximum runtime of $app.timeout milliseconds.')
|
2020-08-13 23:51:57 +02:00
|
|
|
}
|
|
|
|
|
2021-01-05 01:30:27 +01:00
|
|
|
pub fn (mut app App) index() vweb.Result {
|
|
|
|
return app.text('Welcome to VWeb')
|
2020-08-13 23:51:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut app App) simple() vweb.Result {
|
2021-01-05 01:30:27 +01:00
|
|
|
return app.text('A simple result')
|
2020-08-13 23:51:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut app App) html_page() vweb.Result {
|
2021-01-08 04:57:02 +01:00
|
|
|
return app.html('<h1>ok</h1>')
|
2020-08-13 23:51:57 +02:00
|
|
|
}
|
|
|
|
|
2021-01-18 13:04:21 +01:00
|
|
|
pub fn (mut app App) chunk() vweb.Result {
|
|
|
|
app.enable_chunked_transfer(20)
|
|
|
|
return app.html('Lorem ipsum dolor sit amet, consetetur sadipscing')
|
|
|
|
}
|
|
|
|
|
2020-08-13 23:51:57 +02:00
|
|
|
// the following serve custom routes
|
|
|
|
['/:user/settings']
|
|
|
|
pub fn (mut app App) settings(username string) vweb.Result {
|
2021-01-25 12:08:43 +01:00
|
|
|
if username !in known_users {
|
2020-12-31 17:22:47 +01:00
|
|
|
return app.not_found()
|
2020-08-14 01:43:57 +02:00
|
|
|
}
|
2021-01-08 04:57:02 +01:00
|
|
|
return app.html('username: $username')
|
2020-08-13 23:51:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
['/:user/:repo/settings']
|
2020-10-17 17:27:06 +02:00
|
|
|
pub fn (mut app App) user_repo_settings(username string, repository string) vweb.Result {
|
2021-01-25 12:08:43 +01:00
|
|
|
if username !in known_users {
|
2020-12-31 17:22:47 +01:00
|
|
|
return app.not_found()
|
2020-08-14 01:43:57 +02:00
|
|
|
}
|
2021-01-08 04:57:02 +01:00
|
|
|
return app.html('username: $username | repository: $repository')
|
2020-08-13 23:51:57 +02:00
|
|
|
}
|
2020-08-14 01:43:57 +02:00
|
|
|
|
|
|
|
[post]
|
|
|
|
['/json_echo']
|
2021-01-01 21:29:14 +01:00
|
|
|
pub fn (mut app App) json_echo() vweb.Result {
|
2021-01-02 12:21:30 +01:00
|
|
|
eprintln('>>>>> received http request at /json_echo is: $app.req')
|
2021-01-01 21:29:14 +01:00
|
|
|
app.set_content_type(app.req.headers['Content-Type'])
|
|
|
|
return app.ok(app.req.data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure [post] works without the path
|
|
|
|
[post]
|
2020-08-14 01:43:57 +02:00
|
|
|
pub fn (mut app App) json() vweb.Result {
|
2021-01-02 12:21:30 +01:00
|
|
|
eprintln('>>>>> received http request at /json is: $app.req')
|
2020-12-31 17:22:47 +01:00
|
|
|
app.set_content_type(app.req.headers['Content-Type'])
|
|
|
|
return app.ok(app.req.data)
|
2020-08-14 01:43:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut app App) shutdown() vweb.Result {
|
2020-12-31 17:22:47 +01:00
|
|
|
session_key := app.get_cookie('skey') or { return app.not_found() }
|
2020-08-14 01:43:57 +02:00
|
|
|
if session_key != 'superman' {
|
2020-12-31 17:22:47 +01:00
|
|
|
return app.not_found()
|
2020-08-14 01:43:57 +02:00
|
|
|
}
|
|
|
|
go app.gracefull_exit()
|
2020-12-31 17:22:47 +01:00
|
|
|
return app.ok('good bye')
|
2020-08-14 01:43:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut app App) gracefull_exit() {
|
|
|
|
eprintln('>> webserver: gracefull_exit')
|
2021-02-27 18:41:06 +01:00
|
|
|
time.sleep(100 * time.millisecond)
|
2020-08-14 01:43:57 +02:00
|
|
|
exit(0)
|
|
|
|
}
|