diff options
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) } |
