mirror of
https://github.com/cubixle/tuu.git
synced 2026-04-24 23:04:43 +01:00
SAVEPOINT
This commit is contained in:
@@ -1,6 +1,10 @@
|
|||||||
package tuu
|
package tuu
|
||||||
|
|
||||||
import "net/http"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
func NewRouter() *DefaultRouter {
|
func NewRouter() *DefaultRouter {
|
||||||
return &DefaultRouter{}
|
return &DefaultRouter{}
|
||||||
@@ -9,6 +13,12 @@ func NewRouter() *DefaultRouter {
|
|||||||
type DefaultRouter struct {
|
type DefaultRouter struct {
|
||||||
Routes []*Route
|
Routes []*Route
|
||||||
StaticRoutes []*StaticRoute
|
StaticRoutes []*StaticRoute
|
||||||
|
|
||||||
|
prefix string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Prefix) Prefix(path string) {
|
||||||
|
r.prefix = path
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DefaultRouter) GET(path string, h Handler) {
|
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 {
|
func (r *DefaultRouter) GetRoutes() []*Route {
|
||||||
return r.Routes
|
return r.Routes
|
||||||
}
|
}
|
||||||
@@ -35,9 +49,11 @@ func (r *DefaultRouter) GetStaticRoutes() []*StaticRoute {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *DefaultRouter) addRoute(m, p string, h Handler) {
|
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{
|
r.Routes = append(r.Routes, &Route{
|
||||||
Method: m,
|
Method: m,
|
||||||
Path: p,
|
Path: path,
|
||||||
Handler: h,
|
Handler: h,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
2
route.go
2
route.go
@@ -1,6 +1,7 @@
|
|||||||
package tuu
|
package tuu
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/gorilla/mux"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
gcontext "github.com/gorilla/context"
|
gcontext "github.com/gorilla/context"
|
||||||
@@ -10,6 +11,7 @@ type Route struct {
|
|||||||
Method string
|
Method string
|
||||||
Path string
|
Path string
|
||||||
Handler Handler
|
Handler Handler
|
||||||
|
MuxHandler mux.Route
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Route) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
func (r *Route) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
||||||
|
|||||||
@@ -5,9 +5,11 @@ import "net/http"
|
|||||||
type Handler func(Context) error
|
type Handler func(Context) error
|
||||||
|
|
||||||
type Router interface {
|
type Router interface {
|
||||||
|
Prefix(path string)
|
||||||
GET(path string, h Handler)
|
GET(path string, h Handler)
|
||||||
POST(path string, h Handler)
|
POST(path string, h Handler)
|
||||||
Static(path string, root http.FileSystem)
|
Static(path string, root http.FileSystem)
|
||||||
|
NotFound(path string. h Handler)
|
||||||
|
|
||||||
GetRoutes() []*Route
|
GetRoutes() []*Route
|
||||||
GetStaticRoutes() []*StaticRoute
|
GetStaticRoutes() []*StaticRoute
|
||||||
|
|||||||
Reference in New Issue
Block a user