2020-08-14 00:51:57 +03:00
|
|
|
module main
|
|
|
|
|
|
|
|
import os
|
|
|
|
import vweb
|
|
|
|
import time
|
|
|
|
|
2020-08-14 02:43:57 +03:00
|
|
|
const (
|
|
|
|
known_users = ['bilbo', 'kent']
|
|
|
|
)
|
|
|
|
|
2020-08-14 00:51:57 +03:00
|
|
|
struct App {
|
2020-12-31 19:22:47 +03:00
|
|
|
vweb.Context
|
2021-06-16 20:33:30 +03:00
|
|
|
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)
|
2022-11-15 16:53:13 +03:00
|
|
|
println('>> webserver: pid: ${os.getpid()}, exiting ...')
|
2020-08-14 00:51:57 +03:00
|
|
|
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
|
2022-11-05 10:46:40 +03:00
|
|
|
spawn exit_after_timeout(timeout)
|
2020-08-14 00:51:57 +03:00
|
|
|
//
|
2021-06-16 20:33:30 +03:00
|
|
|
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
|
2021-06-16 20:33:30 +03:00
|
|
|
global_config: config
|
2020-08-14 00:51:57 +03:00
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('>> webserver: pid: ${os.getpid()}, started on http://localhost:${app.port}/ , with maximum runtime of ${app.timeout} milliseconds.')
|
2022-11-13 11:57:35 +03:00
|
|
|
vweb.run_at(app, host: 'localhost', port: http_port, family: .ip)!
|
2020-08-14 00:51:57 +03:00
|
|
|
}
|
|
|
|
|
2021-05-11 09:30:01 +03:00
|
|
|
// pub fn (mut app App) init_server() {
|
|
|
|
//}
|
|
|
|
|
2021-01-05 03:30:27 +03:00
|
|
|
pub fn (mut app App) index() vweb.Result {
|
2022-04-07 18:05:11 +03:00
|
|
|
rlock app.global_config {
|
|
|
|
assert app.global_config.max_ping == 50
|
|
|
|
}
|
2021-01-05 03:30:27 +03:00
|
|
|
return app.text('Welcome to VWeb')
|
2020-08-14 00:51:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut app App) simple() vweb.Result {
|
2021-01-05 03:30:27 +03:00
|
|
|
return app.text('A simple result')
|
2020-08-14 00:51:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut app App) html_page() vweb.Result {
|
2021-01-08 06:57:02 +03:00
|
|
|
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 {
|
2021-01-25 14:08:43 +03:00
|
|
|
if username !in known_users {
|
2020-12-31 19:22:47 +03:00
|
|
|
return app.not_found()
|
2020-08-14 02:43:57 +03:00
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
return app.html('username: ${username}')
|
2020-08-14 00:51:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
['/:user/:repo/settings']
|
2020-10-17 18:27:06 +03:00
|
|
|
pub fn (mut app App) user_repo_settings(username string, repository string) vweb.Result {
|
2021-01-25 14:08:43 +03:00
|
|
|
if username !in known_users {
|
2020-12-31 19:22:47 +03:00
|
|
|
return app.not_found()
|
2020-08-14 02:43:57 +03:00
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
return app.html('username: ${username} | repository: ${repository}')
|
2020-08-14 00:51:57 +03:00
|
|
|
}
|
2020-08-14 02:43:57 +03:00
|
|
|
|
2021-03-05 08:17:57 +03:00
|
|
|
['/json_echo'; post]
|
2021-01-01 23:29:14 +03:00
|
|
|
pub fn (mut app App) json_echo() vweb.Result {
|
2021-03-03 10:56:22 +03:00
|
|
|
// eprintln('>>>>> received http request at /json_echo is: $app.req')
|
2021-04-09 19:17:33 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-07-14 16:07:52 +03:00
|
|
|
['/login'; post]
|
|
|
|
pub fn (mut app App) login_form(username string, password string) vweb.Result {
|
|
|
|
return app.html('username: x${username}x | password: x${password}x')
|
|
|
|
}
|
|
|
|
|
2021-04-16 08:46:06 +03:00
|
|
|
['/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'])
|
|
|
|
}
|
|
|
|
|
2023-04-14 08:07:48 +03:00
|
|
|
['/file_echo'; post]
|
|
|
|
pub fn (mut app App) file_echo() vweb.Result {
|
|
|
|
if 'file' !in app.files {
|
|
|
|
app.set_status(500, '')
|
|
|
|
return app.text('no file')
|
|
|
|
}
|
|
|
|
|
|
|
|
return app.text(app.files['file'][0].data)
|
|
|
|
}
|
|
|
|
|
2021-01-01 23:29:14 +03:00
|
|
|
// Make sure [post] works without the path
|
|
|
|
[post]
|
2020-08-14 02:43:57 +03:00
|
|
|
pub fn (mut app App) json() vweb.Result {
|
2021-03-03 10:56:22 +03:00
|
|
|
// eprintln('>>>>> received http request at /json is: $app.req')
|
2021-04-09 19:17:33 +03:00
|
|
|
app.set_content_type(app.req.header.get(.content_type) or { '' })
|
2020-12-31 19:22:47 +03:00
|
|
|
return app.ok(app.req.data)
|
2020-08-14 02:43:57 +03:00
|
|
|
}
|
|
|
|
|
2023-04-12 00:50:03 +03:00
|
|
|
// Custom 404 page
|
|
|
|
pub fn (mut app App) not_found() vweb.Result {
|
|
|
|
app.set_status(404, 'Not Found')
|
|
|
|
return app.html('404 on "${app.req.url}"')
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:22:23 +03:00
|
|
|
[host: 'example.com']
|
|
|
|
['/with_host']
|
|
|
|
pub fn (mut app App) with_host() vweb.Result {
|
|
|
|
return app.ok('')
|
|
|
|
}
|
|
|
|
|
2020-08-14 02:43:57 +03:00
|
|
|
pub fn (mut app App) shutdown() vweb.Result {
|
2020-12-31 19:22:47 +03:00
|
|
|
session_key := app.get_cookie('skey') or { return app.not_found() }
|
2020-08-14 02:43:57 +03:00
|
|
|
if session_key != 'superman' {
|
2020-12-31 19:22:47 +03:00
|
|
|
return app.not_found()
|
2020-08-14 02:43:57 +03:00
|
|
|
}
|
2022-11-05 10:46:40 +03:00
|
|
|
spawn app.gracefull_exit()
|
2020-12-31 19:22:47 +03:00
|
|
|
return app.ok('good bye')
|
2020-08-14 02:43:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut app App) gracefull_exit() {
|
|
|
|
eprintln('>> webserver: gracefull_exit')
|
2021-02-27 20:41:06 +03:00
|
|
|
time.sleep(100 * time.millisecond)
|
2020-08-14 02:43:57 +03:00
|
|
|
exit(0)
|
|
|
|
}
|