From bb58f6cd1d9cce5b12e2f9a30b1c9a6aaefdd54c Mon Sep 17 00:00:00 2001 From: Eltrac Date: Thu, 27 Nov 2025 11:31:35 +0800 Subject: refactor: move type definitions to types.go also, rename Api to Engine. --- api.go | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'api.go') diff --git a/api.go b/api.go index cc35c57..e3c8ca8 100644 --- a/api.go +++ b/api.go @@ -4,39 +4,35 @@ import ( "net/http" ) -type Api struct { - router *Router -} - // create API engine -func New() *Api { - var a Api +func New() *Engine { + var a Engine a.router = NewRouter() return &a } // add routes -func (api *Api) GET(path string, h Handler) { +func (api *Engine) GET(path string, h Handler) { api.router.Add("GET", path, h) } -func (api *Api) POST(path string, h Handler) { +func (api *Engine) POST(path string, h Handler) { api.router.Add("POST", path, h) } -func (api *Api) PUT(path string, h Handler) { +func (api *Engine) PUT(path string, h Handler) { api.router.Add("PUT", path, h) } -func (api *Api) DELETE(path string, h Handler) { +func (api *Engine) DELETE(path string, h Handler) { api.router.Add("DELETE", path, h) } // serve and start -func (a *Api) ServeHTTP(w http.ResponseWriter, req *http.Request) { +func (a *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) { handler, params := a.router.Find(req.Method, req.URL.Path) if handler == nil { http.NotFound(w, req) @@ -49,6 +45,6 @@ func (a *Api) ServeHTTP(w http.ResponseWriter, req *http.Request) { }) } -func (api *Api) Start(port string) { +func (api *Engine) Start(port string) { http.ListenAndServe(port, api) } -- cgit v1.2.3