diff options
Diffstat (limited to 'engine.go')
| -rw-r--r-- | engine.go | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/engine.go b/engine.go new file mode 100644 index 0000000..e3c8ca8 --- /dev/null +++ b/engine.go @@ -0,0 +1,50 @@ +package growel + +import ( + "net/http" +) + +// create API engine + +func New() *Engine { + var a Engine + a.router = NewRouter() + return &a +} + +// add routes + +func (api *Engine) GET(path string, h Handler) { + api.router.Add("GET", path, h) +} + +func (api *Engine) POST(path string, h Handler) { + api.router.Add("POST", path, h) +} + +func (api *Engine) PUT(path string, h Handler) { + api.router.Add("PUT", path, h) +} + +func (api *Engine) DELETE(path string, h Handler) { + api.router.Add("DELETE", path, h) +} + +// serve and start + +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) + return + } + handler(&Context{ + W: w, + R: req, + Params: params, + }) +} + +func (api *Engine) Start(port string) { + http.ListenAndServe(port, api) +} |
