vweb: route to index dynamically (#7782)

pull/7877/head
Daniel Däschle 2021-01-05 01:30:27 +01:00 committed by GitHub
parent a7a8e659f6
commit 9f74be4cf6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 28 deletions

View File

@ -60,7 +60,7 @@ fn assert_common_headers(received string) {
} }
fn test_a_simple_tcp_client_can_connect_to_the_vweb_server() { fn test_a_simple_tcp_client_can_connect_to_the_vweb_server() {
received := simple_tcp_client({}) or { received := simple_tcp_client(path: '/') or {
assert err == '' assert err == ''
return return
} }

View File

@ -44,13 +44,12 @@ pub fn (mut app App) init_once() {
eprintln('>> webserver: started on http://127.0.0.1:$app.port/ , with maximum runtime of $app.timeout milliseconds.') eprintln('>> webserver: started on http://127.0.0.1:$app.port/ , with maximum runtime of $app.timeout milliseconds.')
} }
pub fn (mut app App) index() { pub fn (mut app App) index() vweb.Result {
app.text('Welcome to VWeb') return app.text('Welcome to VWeb')
} }
pub fn (mut app App) simple() vweb.Result { pub fn (mut app App) simple() vweb.Result {
app.text('A simple result') return app.text('A simple result')
return vweb.Result{}
} }
pub fn (mut app App) html_page() vweb.Result { pub fn (mut app App) html_page() vweb.Result {

View File

@ -367,19 +367,14 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
// mut url_words := vals[1][1..].split('/').filter(it != '') // mut url_words := vals[1][1..].split('/').filter(it != '')
x := vals[1][1..].split('/') x := vals[1][1..].split('/')
mut url_words := x.filter(it != '') mut url_words := x.filter(it != '')
if url_words.len == 0 { // Parse URL query
app.index() if url_words.len > 0 && url_words.last().contains('?') {
return words := url_words.last().after('?').split('&')
} else { tmp_query := words.map(it.split('='))
// Parse URL query url_words[url_words.len - 1] = url_words.last().all_before('?')
if url_words.last().contains('?') { for data in tmp_query {
words := url_words.last().after('?').split('&') if data.len == 2 {
tmp_query := words.map(it.split('=')) app.query[data[0]] = data[1]
url_words[url_words.len - 1] = url_words.last().all_before('?')
for data in tmp_query {
if data.len == 2 {
app.query[data[0]] = data[1]
}
} }
} }
} }
@ -420,15 +415,24 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
route_words_a = attrs.filter(it.to_lower() != 'get').map(it[1..].split('/')) route_words_a = attrs.filter(it.to_lower() != 'get').map(it[1..].split('/'))
} }
if attrs.len == 0 || (attrs.len == 1 && route_words_a.len == 0) { if attrs.len == 0 || (attrs.len == 1 && route_words_a.len == 0) {
// No routing for this method. If it matches, call it and finish matching if url_words.len > 0 {
// since such methods have a priority. // No routing for this method. If it matches, call it and finish matching
// For example URL `/register` matches route `/:user`, but `fn register()` // since such methods have a priority.
// should be called first. // For example URL `/register` matches route `/:user`, but `fn register()`
if (req_method_str == '' && // should be called first.
url_words[0] == method.name && url_words.len == 1) || if (req_method_str == '' &&
(req_method_str == req.method.str() && url_words[0] == method.name && url_words.len == 1) { url_words[0] == method.name && url_words.len == 1) ||
(req_method_str == req.method.str() && url_words[0] == method.name && url_words.len == 1) {
$if debug {
println('easy match method=$method.name')
}
app.$method(vars)
return
}
} else if method.name == 'index' {
// handle / to .index()
$if debug { $if debug {
println('easy match method=$method.name') println('route to .index()')
} }
app.$method(vars) app.$method(vars)
return return
@ -436,8 +440,10 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
} else { } else {
mut req_method := []string{} mut req_method := []string{}
if route_words_a.len > 0 { if route_words_a.len > 0 {
for route_words in route_words_a { for route_words_ in route_words_a {
if route_words[0] in methods_without_first && route_words.len == 1 { // cannot move to line initialize line because of C error with map(it.filter(it != ''))
route_words := route_words_.filter(it != '')
if route_words.len == 1 && route_words[0] in methods_without_first {
req_method << route_words[0] req_method << route_words[0]
} }
if url_words.len == route_words.len || if url_words.len == route_words.len ||
@ -451,6 +457,10 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
mut matching := false mut matching := false
mut unknown := false mut unknown := false
mut variables := []string{cap: route_words.len} mut variables := []string{cap: route_words.len}
if route_words.len == 0 && url_words.len == 0 {
// index route
matching = true
}
for i in 0 .. route_words.len { for i in 0 .. route_words.len {
if url_words.len == i { if url_words.len == i {
variables << '' variables << ''