feat: add hono.route method for easy group requests

This commit is contained in:
yangshangbo
2025-04-18 19:02:04 +08:00
parent 7734b328b2
commit a76ad033b0
3 changed files with 77 additions and 33 deletions
+6
View File
@@ -70,6 +70,12 @@ fn main() {
status_code: 200
}
})
mut book_app := &hono.Hono{}
book_app.get('/api/book', fn (req hono.Request) http.Response {
println('Received get request: ${req}')
return hono.Response.text('books')
})
hono_app.route('/api', mut book_app)
port := '0.0.0.0:8585'
hono_app.listen(port)
println('Listening on ${port}')
+21 -13
View File
@@ -1,14 +1,22 @@
import regex
// import regex
import pcre
fn main(){
path := "/order/**"
mut replaced_path := path.replace('**','[\\w+//?]+')
println("replaced_path: ${replaced_path}")
replaced_path = replaced_path.replace('*','\\w+')
println("replaced_path: ${replaced_path}")
replaced_path = replaced_path + '$'
println("replaced_path: ${replaced_path}")
mut reg := regex.regex_opt(replaced_path) or { panic(err) }
result := reg.matches_string("/order/33")
println("reg match result ${result}")
}
fn main() {
// path := '/order/**'
// mut replaced_path := path.replace('**','[\\w+//?]+')
// println("replaced_path: ${replaced_path}")
// replaced_path = replaced_path.replace('*','\\w+')
// println("replaced_path: ${replaced_path}")
// replaced_path = replaced_path + '$'
// println("replaced_path: ${replaced_path}")
// mut reg := regex.regex_opt(replaced_path) or { panic(err) }
// result := reg.matches_string("/order/33")
mut reg := pcre.new_regex(r'/(?:order)/[\w+/]+', 0) or { panic(err) }
result := reg.match_str('555/order/33//?88/', 0, 0) or { panic(err) }
mut count := 0
for count < result.group_size {
println('group ${count} ${result.get(count) or { 'not found' }}')
count++
}
println('reg match result ${result}')
}