204 lines
5.1 KiB
V
204 lines
5.1 KiB
V
module hono
|
|
|
|
import net.urllib
|
|
import net.http
|
|
|
|
struct Router {
|
|
mut:
|
|
handlers struct {
|
|
mut:
|
|
get []IRequestHandler
|
|
post []IRequestHandler
|
|
put []IRequestHandler
|
|
delete []IRequestHandler
|
|
patch []IRequestHandler
|
|
head []IRequestHandler
|
|
options []IRequestHandler
|
|
}
|
|
}
|
|
|
|
pub struct Hono {
|
|
mut:
|
|
server http.Server = http.Server{}
|
|
router Router = Router{}
|
|
routes map[string] Hono = {}
|
|
base_path string
|
|
pub mut:
|
|
hybrid_router HybridRouter
|
|
trie_router TrieRouter
|
|
middlewares []Middleware
|
|
}
|
|
|
|
pub fn (mut app Hono) get(path string, handler fn (Request) http.Response) {
|
|
h := RequestHandler{
|
|
path: path
|
|
handler: handler
|
|
}
|
|
app.hybrid_router.add_route('GET', h, '')
|
|
app.trie_router.add_route('GET', path, h)
|
|
}
|
|
|
|
pub fn (mut app Hono) post(path string, handler fn (Request) http.Response) {
|
|
h := RequestHandler{
|
|
path: path
|
|
handler: handler
|
|
}
|
|
app.hybrid_router.add_route('POST', h, '')
|
|
app.trie_router.add_route('POST', path, h)
|
|
}
|
|
|
|
pub fn (mut app Hono) put(path string, handler fn (Request) http.Response) {
|
|
handlers := RequestHandler{path, handler}
|
|
app.router.handlers.put << handlers
|
|
app.hybrid_router.add_route('PUT', handlers, app.base_path)
|
|
}
|
|
|
|
pub fn (mut app Hono) delete(path string, handler fn (Request) http.Response) {
|
|
handlers := RequestHandler{path, handler}
|
|
app.router.handlers.delete << handlers
|
|
app.hybrid_router.add_route('DELETE', handlers, app.base_path)
|
|
}
|
|
|
|
pub fn (mut app Hono) patch(path string, handler fn (Request) http.Response) {
|
|
handlers := RequestHandler{path, handler}
|
|
app.router.handlers.patch << handlers
|
|
app.hybrid_router.add_route('PATCH', handlers, app.base_path)
|
|
}
|
|
|
|
pub fn (mut app Hono) head(path string, handler fn (Request) http.Response) {
|
|
handlers := RequestHandler{path, handler}
|
|
app.router.handlers.head << handlers
|
|
app.hybrid_router.add_route('HEAD', handlers, app.base_path)
|
|
}
|
|
|
|
pub fn (mut app Hono) options(path string, handler fn (Request) http.Response) {
|
|
handlers := RequestHandler{path, handler}
|
|
app.router.handlers.options << handlers
|
|
app.hybrid_router.add_route('OPTIONS', handlers, app.base_path)
|
|
}
|
|
|
|
struct ServerHanler {
|
|
mut:
|
|
app Hono
|
|
}
|
|
|
|
fn server_hanler_new(app Hono) ServerHanler {
|
|
return ServerHanler{
|
|
app: app
|
|
}
|
|
}
|
|
|
|
type Middleware = fn (mut req Request, next fn (mut Request) http.Response) http.Response
|
|
|
|
pub fn (mut app Hono) use(mw Middleware) {
|
|
app.middlewares << mw
|
|
}
|
|
|
|
fn (mut s ServerHanler) handle(req http.Request) http.Response {
|
|
mut res := http.Response{
|
|
status_code: 404
|
|
body: 'Not Found'
|
|
}
|
|
url := urllib.parse(req.url) or {
|
|
urllib.URL{
|
|
path: '/'
|
|
}
|
|
}
|
|
if route_match := s.app.hybrid_router.match_route(req.method.str(), url.path) {
|
|
// 解析 query
|
|
raw_query := url.query().to_map()
|
|
mut query_map := map[string]string{}
|
|
for key, values in raw_query {
|
|
if values.len > 0 {
|
|
query_map[key] = values[0]
|
|
}
|
|
}
|
|
// param 由路由匹配结果提供
|
|
param_map := route_match.params.clone()
|
|
// body
|
|
body := req.data
|
|
// 构造 hono.Request
|
|
mut hreq := Request{
|
|
url: url.path
|
|
param: param_map
|
|
query: query_map
|
|
body: body
|
|
}
|
|
// 洋葱模型递归执行中间件
|
|
return s.exec_middlewares(0, mut hreq, fn [route_match] (req Request) http.Response {
|
|
return route_match.handler.handle(req)
|
|
})
|
|
}
|
|
return res
|
|
}
|
|
|
|
fn (mut s ServerHanler) exec_middlewares(idx int, mut req Request, handler fn (Request) http.Response) http.Response {
|
|
if idx < s.app.middlewares.len {
|
|
mw := s.app.middlewares[idx]
|
|
return mw(mut req, fn [mut s, idx, handler] (mut r Request) http.Response {
|
|
return s.exec_middlewares(idx+1, mut r, handler)
|
|
})
|
|
} else {
|
|
return handler(req)
|
|
}
|
|
}
|
|
|
|
pub fn (mut app Hono) listen(port string) {
|
|
app.server.addr = port
|
|
app.server.handler = server_hanler_new(app)
|
|
app.server.listen_and_serve()
|
|
}
|
|
|
|
pub fn (mut app Hono) route(prefix string, mut subapp Hono) {
|
|
// 简化路由合并,直接添加所有处理器
|
|
app.routes[prefix] = subapp
|
|
|
|
// 将子应用的路由添加到主应用
|
|
for handler in subapp.router.handlers.get {
|
|
app.router.handlers.get << handler
|
|
}
|
|
for handler in subapp.router.handlers.post {
|
|
app.router.handlers.post << handler
|
|
}
|
|
for handler in subapp.router.handlers.put {
|
|
app.router.handlers.put << handler
|
|
}
|
|
for handler in subapp.router.handlers.delete {
|
|
app.router.handlers.delete << handler
|
|
}
|
|
for handler in subapp.router.handlers.patch {
|
|
app.router.handlers.patch << handler
|
|
}
|
|
for handler in subapp.router.handlers.head {
|
|
app.router.handlers.head << handler
|
|
}
|
|
for handler in subapp.router.handlers.options {
|
|
app.router.handlers.options << handler
|
|
}
|
|
}
|
|
|
|
pub fn (mut app Hono) set_base_path(base_path string) {
|
|
app.base_path = base_path
|
|
}
|
|
|
|
pub fn (app Hono) get_router_stats() (int, int, int, int) {
|
|
static_paths, dynamic_paths := app.hybrid_router.get_all_routes()
|
|
cache_size, cache_capacity := app.hybrid_router.get_cache_stats()
|
|
return static_paths.len, dynamic_paths.len, cache_size, cache_capacity
|
|
}
|
|
|
|
pub fn (mut app Hono) clear_cache() {
|
|
app.hybrid_router.clear_cache()
|
|
}
|
|
|
|
pub fn new_hono() Hono {
|
|
return Hono{
|
|
server: http.Server{}
|
|
router: Router{}
|
|
routes: map[string]Hono{}
|
|
base_path: ''
|
|
hybrid_router: new_hybrid_router()
|
|
trie_router: new_trie_router()
|
|
}
|
|
}
|