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

vweb: make thread safe; checker: $if T is Interface {

This commit is contained in:
Alexander Medvednikov
2021-05-11 09:30:01 +03:00
parent e310513a5f
commit dc034d9b16
17 changed files with 348 additions and 178 deletions

View File

@@ -30,16 +30,17 @@ fn main() {
assert timeout > 0
go exit_after_timeout(timeout)
//
mut app := App{
mut app := &App{
port: http_port
timeout: timeout
}
vweb.run_app<App>(mut app, http_port)
eprintln('>> webserver: started on http://127.0.0.1:$app.port/ , with maximum runtime of $app.timeout milliseconds.')
// vweb.run<App>(mut app, http_port)
vweb.run(mut app, http_port)
}
pub fn (mut app App) init_server() {
eprintln('>> webserver: started on http://127.0.0.1:$app.port/ , with maximum runtime of $app.timeout milliseconds.')
}
// pub fn (mut app App) init_server() {
//}
pub fn (mut app App) index() vweb.Result {
return app.text('Welcome to VWeb')

View File

@@ -284,27 +284,49 @@ pub fn (ctx &Context) get_header(key string) string {
return ctx.req.header.get_custom(key) or { '' }
}
/*
pub fn run<T>(port int) {
mut app := T{}
run_app<T>(mut app, port)
mut x := &T{}
run_app(mut x, port)
}
*/
interface DbInterface {
db voidptr
}
pub fn run_app<T>(mut app T, port int) {
// run_app
pub fn run<T>(global_app &T, port int) {
// x := global_app.clone()
// mut global_app := &T{}
// mut app := &T{}
// run_app<T>(mut app, port)
mut l := net.listen_tcp(port) or { panic('failed to listen') }
println('[Vweb] Running app on http://localhost:$port')
app.Context = Context{
conn: 0
}
app.init_server()
$for method in T.methods {
$if method.return_type is Result {
// check routes for validity
}
}
// app.Context = Context{
// conn: 0
//}
// app.init_server()
// global_app.init_server()
//$for method in T.methods {
//$if method.return_type is Result {
// check routes for validity
//}
//}
for {
// Create a new app object for each connection, copy global data like db connections
mut request_app := T{}
$if T is DbInterface {
request_app.db = global_app.db
} $else {
// println('vweb no db')
}
// request_app.Context = Context{
// conn: 0
//}
mut conn := l.accept() or { panic('accept() failed') }
// TODO: running handle_conn concurrently results in a race-condition
handle_conn<T>(mut conn, mut app)
handle_conn<T>(mut conn, mut request_app)
}
}