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

vweb: simplify router (#5751)

This commit is contained in:
Louis Schmieder 2020-07-08 15:22:03 +02:00 committed by GitHub
parent b62bf59c21
commit 7ad03e9d6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -330,12 +330,12 @@ fn handle_conn<T>(conn net.Socket, mut app T) {
return return
} }
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 url_words := vals[1][1..].split('/') mut url_words := vals[1][1..].split('/')
@ -354,100 +354,82 @@ fn handle_conn<T>(conn net.Socket, mut app T) {
} }
} }
} }
mut vars := []string{cap: route_words.len} mut vars := []string{cap: route_words.len}
//println('http method = $req.method') mut action := ''
$for method in T { $for method in T {
ok = true
//println('\n\n method = $method urlwords=')
//println(url_words)
//println('attrs=$attrs')
if attrs == '' { if attrs == '' {
// No routing for this method. If it matches, call it and finish matching // No routing for this method. If it matches, call it and finish matching
// 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.
//println('no attrs for ${url_words[0]}') if (req.method == 'GET' && url_words[0] == method) || (req.method == 'POST' && url_words[0] + '_post' == method) {
if (req.method == 'GET' && url_words[0] == method) || (req.method == 'POST' && url_words[0] + '_post' == method) { println('found method $method')
println('easy match $method')
vars = []
app.$method(vars) app.$method(vars)
conn.close() or {} conn.close() or {}
return return
} }
} } else {
else {
//println('ATTR=$attrs')
route_words = attrs[1..].split('/') route_words = attrs[1..].split('/')
//println('words:') println(route_words) if url_words.len == route_words.len || (url_words.len >= route_words.len && route_words.last().ends_with('...')) {
//println('vals:') println(url_words)
vars = []string{cap: route_words.len}
if route_words.len <= url_words.len {
// match `/:user/:repo/tree` to `/vlang/v/tree` // match `/:user/:repo/tree` to `/vlang/v/tree`
for i, word in route_words { mut matching := false
if word.starts_with(':') { mut unknown := false
// remember and skip the var mut variables := []string{cap: route_words.len}
vars << url_words[i] for i in 0..route_words.len {
if url_words[i] == route_words[i] {
// no parameter
matching = true
continue continue
} } else if route_words[i].starts_with(':') {
if word != url_words[i] { // is parameter
ok = false if i < route_words.len && !route_words[i].ends_with('...') {
// normal parameter
variables << url_words[i]
} else {
// array parameter only in the end
variables << url_words[i..].join('/')
}
matching = true
unknown = true
continue
} else {
matching = false
break break
} }
} }
if ok { if matching && !unknown {
//goto end // TODO break in $for // absolute router words like `/test/site`
if !route_words[0].starts_with(':') {
// Routes without variables have higher priority, so call it right now
// e.g. `/register` matches `['/:user']`, but `['/register']` should be called first.
//println('match no var $action="$action"')
app.$method(vars)
conn.close() or {}
return
}
//println('matched method=$method')
app.$method(vars) app.$method(vars)
conn.close() or {} conn.close() or {}
return return
//action = method } else if matching && unknown {
//println('setting action to $method') // router words with paramter like `/:test/site`
action = method
vars = variables
} }
} }
} }
} }
//if action == '' { if action == '' {
// site not found
conn.send_string(http_404) or {} conn.send_string(http_404) or {}
conn.close() or {} conn.close() or {}
return return
}
send_action<T>(action, vars, mut app)
return
}
//} fn send_action<T>(action string, vars []string, mut app T) {
//end: // TODO remove this function
// No route matched, just do a simple `/home` => `action=home` $for method in T {
/* // search again for method
if action == '' { if action == method && attrs != '' {
//println('action is empty because no routes were matched...') // call action method
action = vals[1][1..].all_before('/') app.$method(vars)
if action.contains('?') {
action = action.all_before('?')
}
if action == '' {
action = 'index'
} }
} }
$if debug {
println('action=$action')
}
*/
//println('route matching took ${time.ticks() - t}ms')
//app.$action()
/*
app.$action() or {
conn.send_string(http_404) or {}
}
*/
//conn.close() or {}
////app.reset()
//return
} }
fn (mut ctx Context) parse_form(s string) { fn (mut ctx Context) parse_form(s string) {