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

vweb: middleware implementation (#17730)

This commit is contained in:
Casper Kuethe
2023-03-26 00:57:42 +01:00
committed by GitHub
parent 713c95fcc8
commit 1fe5aca782
11 changed files with 968 additions and 19 deletions

View File

@ -4,13 +4,14 @@ import net.urllib
import net.http
// Parsing function attributes for methods and path.
fn parse_attrs(name string, attrs []string) !([]http.Method, string) {
fn parse_attrs(name string, attrs []string) !([]http.Method, string, string) {
if attrs.len == 0 {
return [http.Method.get], '/${name}'
return [http.Method.get], '/${name}', ''
}
mut x := attrs.clone()
mut methods := []http.Method{}
mut middleware := ''
mut path := ''
for i := 0; i < x.len; {
@ -30,6 +31,11 @@ fn parse_attrs(name string, attrs []string) !([]http.Method, string) {
x.delete(i)
continue
}
if attr.starts_with('middleware:') {
middleware = attr.all_after('middleware:').trim_space()
x.delete(i)
continue
}
i++
}
if x.len > 0 {
@ -44,7 +50,7 @@ fn parse_attrs(name string, attrs []string) !([]http.Method, string) {
path = '/${name}'
}
// Make path lowercase for case-insensitive comparisons
return methods, path.to_lower()
return methods, path.to_lower(), middleware
}
fn parse_query_from_url(url urllib.URL) map[string]string {