summaryrefslogtreecommitdiff
path: root/api.go
diff options
context:
space:
mode:
authorEltrac <hi@guhub.cn>2025-11-27 11:31:35 +0800
committerEltrac <hi@guhub.cn>2025-11-27 11:31:35 +0800
commitbb58f6cd1d9cce5b12e2f9a30b1c9a6aaefdd54c (patch)
tree8a668165b0f0f57c4fc10a462cf22f2cdbdf5954 /api.go
parenta13c9a7abe43e9e3f21799e8be161422c7fad5a4 (diff)
refactor: move type definitions to types.go also, rename Api to Engine.
Diffstat (limited to 'api.go')
-rw-r--r--api.go20
1 files changed, 8 insertions, 12 deletions
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)
}