-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller_test_server.v
More file actions
118 lines (96 loc) · 2.31 KB
/
controller_test_server.v
File metadata and controls
118 lines (96 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
module main
import vweb
import time
import os
struct App {
vweb.Context
vweb.Controller
timeout int
}
struct Admin {
vweb.Context
}
struct Other {
vweb.Context
}
struct OtherHidedByOther {
vweb.Context
}
fn exit_after_timeout(timeout_in_ms int) {
time.sleep(timeout_in_ms * time.millisecond)
println('>> webserver: pid: ${os.getpid()}, exiting ...')
exit(0)
}
fn main() {
if os.args.len != 3 {
panic('Usage: `controller_test_server.exe PORT TIMEOUT_IN_MILLISECONDS`')
}
http_port := os.args[1].int()
assert http_port > 0
timeout := os.args[2].int()
spawn exit_after_timeout(timeout)
mut app := &App{
timeout: timeout
controllers: [
vweb.controller('/admin', &Admin{}),
vweb.controller('/other', &Other{}),
vweb.controller('/other/hide', &OtherHidedByOther{}),
]
}
eprintln('>> webserver: pid: ${os.getpid()}, started on http://localhost:${http_port}/ , with maximum runtime of ${app.timeout} milliseconds.')
vweb.run_at(app, host: 'localhost', port: http_port, family: .ip)!
}
['/']
pub fn (mut app App) home() vweb.Result {
return app.text('App')
}
['/path']
pub fn (mut app App) app_path() vweb.Result {
return app.text('App path')
}
pub fn (mut app App) not_found() vweb.Result {
app.set_status(404, 'Not Found')
return app.text('404 From App')
}
['/']
pub fn (mut app Admin) admin_home() vweb.Result {
return app.text('Admin')
}
['/path']
pub fn (mut app Admin) admin_path() vweb.Result {
return app.text('Admin path')
}
pub fn (mut app Admin) not_found() vweb.Result {
app.set_status(404, 'Not Found')
return app.text('404 From Admin')
}
['/']
pub fn (mut app Other) other_home() vweb.Result {
return app.text('Other')
}
['/path']
pub fn (mut app Other) other_path() vweb.Result {
return app.text('Other path')
}
['/']
pub fn (mut app OtherHidedByOther) other_home() vweb.Result {
return app.text('Other')
}
['/path']
pub fn (mut app OtherHidedByOther) other_path() vweb.Result {
return app.text('Other path')
}
// utility functions:
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()
}
spawn app.gracefull_exit()
return app.ok('good bye')
}
fn (mut app App) gracefull_exit() {
eprintln('>> webserver: gracefull_exit')
time.sleep(100 * time.millisecond)
exit(0)
}