From a52afde13b4e483e5dd96c7d1a3ace81ce400103 Mon Sep 17 00:00:00 2001 From: Eltrac Date: Thu, 27 Nov 2025 10:36:15 +0800 Subject: feat: get router working properly and implement basic methods --- api.go | 35 ++++++++++++++++++++++++++--------- context.go | 10 +--------- router.go | 21 +++++++++++++++++++-- 3 files changed, 46 insertions(+), 20 deletions(-) diff --git a/api.go b/api.go index 9cabe29..5682b99 100644 --- a/api.go +++ b/api.go @@ -1,18 +1,35 @@ package growel import ( - + "net/http" ) -type growel struct { } +type Api struct { + router *Router +} + +func New() *Api { + var a Api + a.router = NewRouter() + return &a +} + +func (api *Api) Start(port string) { + http.ListenAndServe(port, api.router) +} -func (growel) New() (*growel) { - return &growel{} +func (api *Api) GET(path string, h Handler) { + api.router.Add("GET", path, h) } -func (*growel) GET() {} -func (*growel) POST() {} -func (*growel) PUT() {} -func (*growel) DELETE() {} +func (api *Api) POST(path string, h Handler) { + api.router.Add("POST", path, h) +} -func (*growel) Start() {} +func (api *Api) PUT(path string, h Handler) { + api.router.Add("PUT", path, h) +} + +func (api *Api) DELETE(path string, h Handler) { + api.router.Add("DELETE", path, h) +} diff --git a/context.go b/context.go index fbb6a1e..628cb4f 100644 --- a/context.go +++ b/context.go @@ -6,16 +6,8 @@ import ( type Context struct { W http.ResponseWriter - R http.Request + R *http.Request Params map[string]string } type Handler func(c *Context) - -func (h Handler) ServeHTTP(w http.ResponseWriter, r http.Request) { - h(&Context{ - W: w, - R: r, - Params: make(map[string]string), - }) -} diff --git a/router.go b/router.go index 5428701..a9137be 100644 --- a/router.go +++ b/router.go @@ -1,6 +1,7 @@ package growel import ( + "net/http" "strings" ) @@ -18,12 +19,12 @@ func NewRouter() *Router { return &Router{} } -func (r *Router) Add(method, path string, h Handler) { +func (r *Router) Add(method string, path string, h Handler) { parts := strings.Split(path, "/") r.routes = append(r.routes, route{method, parts, h}) } -func (r *Router) Find(method, path string) (Handler, map[string]string) { +func (r *Router) Find(method string, path string) (Handler, map[string]string) { parts := strings.Split(path, "/") for _, rt := range r.routes { @@ -37,13 +38,16 @@ func (r *Router) Find(method, path string) (Handler, map[string]string) { continue } + // compare each segment params := make(map[string]string) matched := true for i, seg := range rt.parts { + // parse dynamic parameter if strings.HasPrefix(seg, ":") { params[seg[1:]] = parts[i] + continue } // static mismatch @@ -58,3 +62,16 @@ func (r *Router) Find(method, path string) (Handler, map[string]string) { } return nil, nil } + +func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { + handler, params := r.Find(req.Method, req.URL.Path) + if handler == nil { + http.NotFound(w, req) + return + } + handler(&Context{ + W: w, + R: req, + Params: params, + }) +} -- cgit v1.2.3