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

vweb: add controllers (#17840)

This commit is contained in:
Casper Kuethe
2023-04-02 15:46:43 +02:00
committed by GitHub
parent c7237b1c58
commit b2735bf937
5 changed files with 474 additions and 29 deletions

View File

@@ -0,0 +1,41 @@
module main
import vweb
import time
import os
struct App {
vweb.Context
vweb.Controller
}
struct Admin {
vweb.Context
}
['/admin/duplicate']
fn (mut app App) duplicate() vweb.Result {
return app.text('duplicate')
}
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()
mut app_dup := &App{
controllers: [
vweb.controller('/admin', &Admin{}),
]
}
vweb.run_at(app_dup, host: 'localhost', port: http_port, family: .ip) or { panic(err) }
}