App methods with `use` attribute is middlewares
parent
0efd083e7c
commit
0a3a31fadb
|
@ -383,17 +383,22 @@ pub fn run<T>(global_app &T, port int) {
|
||||||
|
|
||||||
// Parsing methods attributes
|
// Parsing methods attributes
|
||||||
mut routes := map[string]Route{}
|
mut routes := map[string]Route{}
|
||||||
|
mut middlewares := map[string]string{}
|
||||||
$for method in T.methods {
|
$for method in T.methods {
|
||||||
http_methods, route_path := parse_attrs(method.name, method.attrs) or {
|
http_methods, route_path := parse_attrs(method.name, method.attrs) or {
|
||||||
eprintln('error parsing method attributes: $err')
|
eprintln('error parsing method attributes: $err')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if 'use' in method.attrs.filter(it.len == 3).map(it.to_lower()) {
|
||||||
|
middlewares[method.name] = route_path
|
||||||
|
} else {
|
||||||
routes[method.name] = Route{
|
routes[method.name] = Route{
|
||||||
methods: http_methods
|
methods: http_methods
|
||||||
path: route_path
|
path: route_path
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
println('[Vweb] Running app on http://localhost:$port')
|
println('[Vweb] Running app on http://localhost:$port')
|
||||||
for {
|
for {
|
||||||
// Create a new app object for each connection, copy global data like db connections
|
// Create a new app object for each connection, copy global data like db connections
|
||||||
|
@ -414,12 +419,12 @@ pub fn run<T>(global_app &T, port int) {
|
||||||
eprintln('accept() failed with error: $err.msg')
|
eprintln('accept() failed with error: $err.msg')
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
go handle_conn<T>(mut conn, mut request_app, routes)
|
go handle_conn<T>(mut conn, mut request_app, routes, middlewares)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[manualfree]
|
[manualfree]
|
||||||
fn handle_conn<T>(mut conn net.TcpConn, mut app T, routes map[string]Route) {
|
fn handle_conn<T>(mut conn net.TcpConn, mut app T, routes map[string]Route, middlewares map[string]string) {
|
||||||
conn.set_read_timeout(30 * time.second)
|
conn.set_read_timeout(30 * time.second)
|
||||||
conn.set_write_timeout(30 * time.second)
|
conn.set_write_timeout(30 * time.second)
|
||||||
defer {
|
defer {
|
||||||
|
|
Loading…
Reference in New Issue