1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/vweb/tests/vweb_test_server.v

120 lines
2.7 KiB
V
Raw Normal View History

2020-08-14 00:51:57 +03:00
module main
import os
import vweb
import time
const (
known_users = ['bilbo', 'kent']
)
2020-08-14 00:51:57 +03:00
struct App {
vweb.Context
port int
timeout int
global_config shared Config
}
struct Config {
max_ping int
2020-08-14 00:51:57 +03:00
}
fn exit_after_timeout(timeout_in_ms int) {
2021-02-27 20:41:06 +03:00
time.sleep(timeout_in_ms * time.millisecond)
2020-08-14 00:51:57 +03: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)
//
shared config := &Config{
max_ping: 50
}
2021-05-11 09:52:08 +03:00
app := &App{
2020-08-14 00:51:57 +03:00
port: http_port
timeout: timeout
global_config: config
2020-08-14 00:51:57 +03:00
}
eprintln('>> webserver: started on http://localhost:$app.port/ , with maximum runtime of $app.timeout milliseconds.')
vweb.run_at(app, host: 'localhost', port: http_port, family: .ip)?
2020-08-14 00:51:57 +03:00
}
// pub fn (mut app App) init_server() {
//}
pub fn (mut app App) index() vweb.Result {
rlock app.global_config {
assert app.global_config.max_ping == 50
}
return app.text('Welcome to VWeb')
2020-08-14 00:51:57 +03:00
}
pub fn (mut app App) simple() vweb.Result {
return app.text('A simple result')
2020-08-14 00:51:57 +03:00
}
pub fn (mut app App) html_page() vweb.Result {
return app.html('<h1>ok</h1>')
2020-08-14 00:51:57 +03:00
}
// the following serve custom routes
['/:user/settings']
pub fn (mut app App) settings(username string) vweb.Result {
if username !in known_users {
return app.not_found()
}
return app.html('username: $username')
2020-08-14 00:51:57 +03:00
}
['/:user/:repo/settings']
pub fn (mut app App) user_repo_settings(username string, repository string) vweb.Result {
if username !in known_users {
return app.not_found()
}
return app.html('username: $username | repository: $repository')
2020-08-14 00:51:57 +03:00
}
['/json_echo'; post]
2021-01-01 23:29:14 +03:00
pub fn (mut app App) json_echo() vweb.Result {
// eprintln('>>>>> received http request at /json_echo is: $app.req')
app.set_content_type(app.req.header.get(.content_type) or { '' })
2021-01-01 23:29:14 +03:00
return app.ok(app.req.data)
}
['/form_echo'; post]
pub fn (mut app App) form_echo() vweb.Result {
app.set_content_type(app.req.header.get(.content_type) or { '' })
return app.ok(app.form['foo'])
}
2021-01-01 23:29:14 +03:00
// Make sure [post] works without the path
[post]
pub fn (mut app App) json() vweb.Result {
// eprintln('>>>>> received http request at /json is: $app.req')
app.set_content_type(app.req.header.get(.content_type) or { '' })
return app.ok(app.req.data)
}
pub fn (mut app App) shutdown() vweb.Result {
session_key := app.get_cookie('skey') or { return app.not_found() }
if session_key != 'superman' {
return app.not_found()
}
go app.gracefull_exit()
return app.ok('good bye')
}
fn (mut app App) gracefull_exit() {
eprintln('>> webserver: gracefull_exit')
2021-02-27 20:41:06 +03:00
time.sleep(100 * time.millisecond)
exit(0)
}