SAVEPOINT

This commit is contained in:
luke.rodham
2017-11-13 15:12:13 +00:00
parent 866e0da047
commit 2f157585d6
3 changed files with 22 additions and 2 deletions

View File

@@ -1,6 +1,10 @@
package tuu
import "net/http"
import (
"fmt"
"net/http"
"strings"
)
func NewRouter() *DefaultRouter {
return &DefaultRouter{}
@@ -9,6 +13,12 @@ func NewRouter() *DefaultRouter {
type DefaultRouter struct {
Routes []*Route
StaticRoutes []*StaticRoute
prefix string
}
func (r *Prefix) Prefix(path string) {
r.prefix = path
}
func (r *DefaultRouter) GET(path string, h Handler) {
@@ -26,6 +36,10 @@ func (r *DefaultRouter) Static(path string, root http.FileSystem) {
})
}
func (r *DefaultRouter) NotFound(path string, h Handler) {
}
func (r *DefaultRouter) GetRoutes() []*Route {
return r.Routes
}
@@ -35,9 +49,11 @@ func (r *DefaultRouter) GetStaticRoutes() []*StaticRoute {
}
func (r *DefaultRouter) addRoute(m, p string, h Handler) {
path := fmt.Sprintf("/%s/%s", strings.TrimPrefix(r.prefix, "/"), strings.TrimSuffix(p, "/"))
r.Routes = append(r.Routes, &Route{
Method: m,
Path: p,
Path: path,
Handler: h,
})
}

View File

@@ -1,6 +1,7 @@
package tuu
import (
"github.com/gorilla/mux"
"net/http"
gcontext "github.com/gorilla/context"
@@ -10,6 +11,7 @@ type Route struct {
Method string
Path string
Handler Handler
MuxHandler mux.Route
}
func (r *Route) ServeHTTP(res http.ResponseWriter, req *http.Request) {

View File

@@ -5,9 +5,11 @@ import "net/http"
type Handler func(Context) error
type Router interface {
Prefix(path string)
GET(path string, h Handler)
POST(path string, h Handler)
Static(path string, root http.FileSystem)
NotFound(path string. h Handler)
GetRoutes() []*Route
GetStaticRoutes() []*StaticRoute