diff options
| author | Eltrac <hi@guhub.cn> | 2025-11-27 11:31:35 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-11-27 11:31:35 +0800 |
| commit | bb58f6cd1d9cce5b12e2f9a30b1c9a6aaefdd54c (patch) | |
| tree | 8a668165b0f0f57c4fc10a462cf22f2cdbdf5954 /api.go | |
| parent | a13c9a7abe43e9e3f21799e8be161422c7fad5a4 (diff) | |
refactor: move type definitions to types.go also, rename Api to Engine.
Diffstat (limited to 'api.go')
| -rw-r--r-- | api.go | 20 |
1 files changed, 8 insertions, 12 deletions
@@ -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) } |
