mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vweb: fix and simplify routing
This commit is contained in:
parent
1307cfc97c
commit
a6450e8e98
@ -115,7 +115,10 @@ fn (mut g Gen) comp_for(node ast.CompFor) {
|
|||||||
vweb_result_type := table.new_type(g.table.find_type_idx('vweb.Result'))
|
vweb_result_type := table.new_type(g.table.find_type_idx('vweb.Result'))
|
||||||
mut i := 0
|
mut i := 0
|
||||||
// g.writeln('string method = tos_lit("");')
|
// g.writeln('string method = tos_lit("");')
|
||||||
for method in sym.methods {
|
mut methods := sym.methods.filter(it.attrs.len == 0) // methods without attrs first
|
||||||
|
methods_with_attrs := sym.methods.filter(it.attrs.len > 0) // methods without attrs first
|
||||||
|
methods << methods_with_attrs
|
||||||
|
for method in methods { // sym.methods {
|
||||||
// if method.attrs.len == 0 {
|
// if method.attrs.len == 0 {
|
||||||
// continue
|
// continue
|
||||||
// }
|
// }
|
||||||
|
@ -329,11 +329,19 @@ fn handle_conn<T>(conn net.Socket, mut app T) {
|
|||||||
app.init()
|
app.init()
|
||||||
// Call the right action
|
// Call the right action
|
||||||
println('route matching...')
|
println('route matching...')
|
||||||
t := time.ticks()
|
//t := time.ticks()
|
||||||
mut action := ''
|
mut action := ''
|
||||||
mut route_words := []string{}
|
mut route_words := []string{}
|
||||||
mut ok := true
|
mut ok := true
|
||||||
url_words := vals[1][1..].split('/')
|
url_words := vals[1][1..].split('/')
|
||||||
|
|
||||||
|
|
||||||
|
if url_words.len == 0 {
|
||||||
|
app.index()
|
||||||
|
conn.close() or {}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
mut vars := []string{cap: route_words.len}
|
mut vars := []string{cap: route_words.len}
|
||||||
|
|
||||||
$for method in T {
|
$for method in T {
|
||||||
@ -346,13 +354,6 @@ fn handle_conn<T>(conn net.Socket, mut app T) {
|
|||||||
// since such methods have a priority.
|
// since such methods have a priority.
|
||||||
// For example URL `/register` matches route `/:user`, but `fn register()`
|
// For example URL `/register` matches route `/:user`, but `fn register()`
|
||||||
// should be called first.
|
// should be called first.
|
||||||
|
|
||||||
if url_words.len == 0 {
|
|
||||||
app.index()
|
|
||||||
conn.close() or {}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
println('no attrs for ${url_words[0]}')
|
println('no attrs for ${url_words[0]}')
|
||||||
if url_words[0] == method {
|
if url_words[0] == method {
|
||||||
println('easy match $method')
|
println('easy match $method')
|
||||||
@ -386,22 +387,30 @@ fn handle_conn<T>(conn net.Socket, mut app T) {
|
|||||||
if !route_words[0].starts_with(':') {
|
if !route_words[0].starts_with(':') {
|
||||||
// Routes without variables have higher priority, so call it right now
|
// Routes without variables have higher priority, so call it right now
|
||||||
// e.g. `/register` matches `['/:user']`, but `['/register']` should be called first.
|
// e.g. `/register` matches `['/:user']`, but `['/register']` should be called first.
|
||||||
//println('OK not var $action="$action"')
|
//println('match no var $action="$action"')
|
||||||
app.$method(vars)
|
app.$method(vars)
|
||||||
conn.close() or {}
|
conn.close() or {}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
action = method
|
//println('matched method=$method')
|
||||||
println('setting action to $method')
|
app.$method(vars)
|
||||||
|
conn.close() or {}
|
||||||
|
return
|
||||||
|
//action = method
|
||||||
|
//println('setting action to $method')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if action == '' {
|
//if action == '' {
|
||||||
conn.send_string(http_404) or {}
|
conn.send_string(http_404) or {}
|
||||||
}
|
conn.close() or {}
|
||||||
|
return
|
||||||
|
|
||||||
|
//}
|
||||||
//end:
|
//end:
|
||||||
// No route matched, just do a simple `/home` => `action=home`
|
// No route matched, just do a simple `/home` => `action=home`
|
||||||
|
/*
|
||||||
if action == '' {
|
if action == '' {
|
||||||
//println('action is empty because no routes were matched...')
|
//println('action is empty because no routes were matched...')
|
||||||
action = vals[1][1..].all_before('/')
|
action = vals[1][1..].all_before('/')
|
||||||
@ -415,17 +424,18 @@ fn handle_conn<T>(conn net.Socket, mut app T) {
|
|||||||
$if debug {
|
$if debug {
|
||||||
println('action=$action')
|
println('action=$action')
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
println('route matching took ${time.ticks() - t}ms')
|
//println('route matching took ${time.ticks() - t}ms')
|
||||||
app.$action()
|
//app.$action()
|
||||||
/*
|
/*
|
||||||
app.$action() or {
|
app.$action() or {
|
||||||
conn.send_string(http_404) or {}
|
conn.send_string(http_404) or {}
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
conn.close() or {}
|
//conn.close() or {}
|
||||||
//app.reset()
|
////app.reset()
|
||||||
return
|
//return
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut ctx Context) parse_form(s string) {
|
fn (mut ctx Context) parse_form(s string) {
|
||||||
|
Loading…
Reference in New Issue
Block a user