summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--api.go20
-rw-r--r--context.go8
-rw-r--r--router.go12
-rw-r--r--types.go25
4 files changed, 34 insertions, 31 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)
}
diff --git a/context.go b/context.go
index 98f52c7..9b00f4c 100644
--- a/context.go
+++ b/context.go
@@ -6,12 +6,6 @@ import (
"strconv"
)
-type Context struct {
- W http.ResponseWriter
- R *http.Request
- Params map[string]string
-}
-
func (c *Context) Plain(code int, data string) {
c.W.Header().Set("Content-Type", "text/plain; charset=utf-8")
c.W.WriteHeader(code)
@@ -49,5 +43,3 @@ func (c *Context) Unauthorized(msg string) {
func (c *Context) InternalError(msg string) {
c.Error(http.StatusInternalServerError, msg)
}
-
-type Handler func(c *Context)
diff --git a/router.go b/router.go
index d608784..961230a 100644
--- a/router.go
+++ b/router.go
@@ -4,16 +4,6 @@ import (
"strings"
)
-type route struct {
- method string
- parts []string
- handler Handler
-}
-
-type Router struct {
- routes []route
-}
-
func NewRouter() *Router {
return &Router{}
}
@@ -24,7 +14,7 @@ func (r *Router) splitPath(path string) []string {
func (r *Router) Add(method string, path string, h Handler) {
parts := r.splitPath(path)
- r.routes = append(r.routes, route{method, parts, h})
+ r.routes = append(r.routes, Route{method, parts, h})
}
func (r *Router) Find(method string, path string) (Handler, map[string]string) {
diff --git a/types.go b/types.go
new file mode 100644
index 0000000..e785f4c
--- /dev/null
+++ b/types.go
@@ -0,0 +1,25 @@
+package growel
+
+import "net/http"
+
+type Context struct {
+ W http.ResponseWriter
+ R *http.Request
+ Params map[string]string
+}
+
+type Handler func(c *Context)
+
+type Route struct {
+ method string
+ parts []string
+ handler Handler
+}
+
+type Router struct {
+ routes []Route
+}
+
+type Engine struct {
+ router *Router
+}