App methods with `use` attribute is middlewares

pull/12916/head
Anton Zavodchikov 2021-12-21 07:10:57 +05:00
parent 0efd083e7c
commit 0a3a31fadb
1 changed files with 10 additions and 5 deletions

View File

@ -383,15 +383,20 @@ 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
} }
routes[method.name] = Route{ if 'use' in method.attrs.filter(it.len == 3).map(it.to_lower()) {
methods: http_methods middlewares[method.name] = route_path
path: route_path } else {
routes[method.name] = Route{
methods: http_methods
path: route_path
}
} }
} }
println('[Vweb] Running app on http://localhost:$port') println('[Vweb] Running app on http://localhost:$port')
@ -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 {