From a6450e8e9822d3d30a84942d537de43f82421524 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 7 Jul 2020 12:35:45 +0200 Subject: [PATCH] vweb: fix and simplify routing --- vlib/v/gen/comptime.v | 5 ++++- vlib/vweb/vweb.v | 46 ++++++++++++++++++++++++++----------------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/vlib/v/gen/comptime.v b/vlib/v/gen/comptime.v index 18fde1a3c4..7ba9e222f3 100644 --- a/vlib/v/gen/comptime.v +++ b/vlib/v/gen/comptime.v @@ -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')) mut i := 0 // 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 { // continue // } diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index af072e3f78..812a183108 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -329,11 +329,19 @@ fn handle_conn(conn net.Socket, mut app T) { app.init() // Call the right action println('route matching...') - t := time.ticks() + //t := time.ticks() mut action := '' mut route_words := []string{} mut ok := true url_words := vals[1][1..].split('/') + + + if url_words.len == 0 { + app.index() + conn.close() or {} + return + } + mut vars := []string{cap: route_words.len} $for method in T { @@ -346,13 +354,6 @@ fn handle_conn(conn net.Socket, mut app T) { // since such methods have a priority. // For example URL `/register` matches route `/:user`, but `fn register()` // should be called first. - - if url_words.len == 0 { - app.index() - conn.close() or {} - return - } - println('no attrs for ${url_words[0]}') if url_words[0] == method { println('easy match $method') @@ -386,22 +387,30 @@ fn handle_conn(conn net.Socket, mut app T) { 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('OK not var $action="$action"') + //println('match no var $action="$action"') app.$method(vars) conn.close() or {} return } - action = method - println('setting action to $method') + //println('matched method=$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.close() or {} + return + + //} //end: // No route matched, just do a simple `/home` => `action=home` + /* if action == '' { //println('action is empty because no routes were matched...') action = vals[1][1..].all_before('/') @@ -415,17 +424,18 @@ fn handle_conn(conn net.Socket, mut app T) { $if debug { println('action=$action') } + */ - println('route matching took ${time.ticks() - t}ms') - app.$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 + //conn.close() or {} + ////app.reset() + //return } fn (mut ctx Context) parse_form(s string) {