diff options
| author | Eltrac <hi@guhub.cn> | 2025-11-27 11:45:00 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-11-27 11:45:00 +0800 |
| commit | c41a8f36676bca96b3a3c4af7ca6ab96f2ad9fc1 (patch) | |
| tree | 143b7388a766c82a45890e37f1c256dbb0ff67f9 /engine.go | |
| parent | bb58f6cd1d9cce5b12e2f9a30b1c9a6aaefdd54c (diff) | |
refactor: use standard Go tests
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) +} |
