Documentation ¶
Overview ¶
Package marmoset ,reinventing the wheel
Index ¶
- func LoadViews(p string) *template.Template
- func RenderJSON(w http.ResponseWriter, status int, data interface{}) error
- func UseTemplate(tpl *template.Template)
- type ContextFilter
- type Filter
- type Filterable
- type HTTPMethod
- type P
- type Renderer
- type RequestContextMap
- type Resolver
- type Router
- func (router *Router) Apply(filters ...Filterable) error
- func (router *Router) FindHandler(req *http.Request) (http.HandlerFunc, bool)
- func (router *Router) GET(path string, handler http.HandlerFunc) *Router
- func (router *Router) Handle(path string, handler http.Handler) *Router
- func (router *Router) NotFound(fun http.HandlerFunc)
- func (router *Router) POST(path string, handler http.HandlerFunc) *Router
- func (router *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (router *Router) Static(p, dir string) *Router
- func (router *Router) StaticRelative(p string, relativeDir string) *Router
- func (router *Router) Subrouter(child *Router) *Router
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RenderJSON ¶
func RenderJSON(w http.ResponseWriter, status int, data interface{}) error
RenderJSON ...
func UseTemplate ¶ added in v0.5.0
Types ¶
type ContextFilter ¶
type ContextFilter struct {
Filter
}
ContextFilter ... Only this `ContextFilter` can access `shared` itself. Add this filter for the last of your filter chain.
func (*ContextFilter) ServeHTTP ¶
func (f *ContextFilter) ServeHTTP(w http.ResponseWriter, r *http.Request)
type Filter ¶
Filter ... Remember "Last added, First called"
Example ¶
// Define routings. router := NewRouter() router.GET("/test", SampleController) // Add Filters. // If you want to use `Context`, `ContextFilter` must be added for the last. // Remember "Last added, First called" router.Apply(new(AuthFilter)) // Use `http.ListenAndServe` in real case, instead of httptest. server := httptest.NewServer(router) req, _ := http.NewRequest("GET", server.URL+"/test", nil) req.Header.Add("X-User", `{"id":10,"name":"otiai10"}`) res, _ := http.DefaultClient.Do(req) fmt.Println("StatusCode:", res.StatusCode) req, _ = http.NewRequest("GET", server.URL+"/test", nil) req.Header.Add("X-User", `{"id":20,"name":"otiai20"}`) res, _ = http.DefaultClient.Do(req) fmt.Println("StatusCode:", res.StatusCode)
Output: StatusCode: 200 StatusCode: 403
type Filterable ¶
Filterable represents struct which can be a filter
type HTTPMethod ¶
type HTTPMethod string
HTTPMethod ...
const ( // MethodGet ... MethodGet HTTPMethod = http.MethodGet // MethodPost ... MethodPost HTTPMethod = http.MethodPost // MethodAny represents any method type MethodAny HTTPMethod = "*" )
type P ¶
type P map[string]interface{}
P is just a short hand of `map[string]interface{}` I don't want to write `map[string]interface{}` so repeatedly... :(
type RequestContextMap ¶
type RequestContextMap struct {
// contains filtered or unexported fields
}
RequestContextMap ...
func (*RequestContextMap) Flush ¶
func (rctxmap *RequestContextMap) Flush(req *http.Request)
Flush ...
type Resolver ¶
type Resolver interface {
FindHandler(*http.Request) (http.HandlerFunc, bool)
}
Resolver only resolves
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router ...
Example (WithPathParams) ¶
router := NewRouter() var userID, filename string router.GET("/users/(?P<user_id>[a-zA-Z0-9]+)", func(w http.ResponseWriter, r *http.Request) { userID = r.FormValue("user_id") w.WriteHeader(http.StatusOK) }) router.GET("/uploads/(?P<filename>.+)", func(w http.ResponseWriter, r *http.Request) { filename = r.FormValue("filename") w.WriteHeader(http.StatusOK) }) server := httptest.NewServer(router) http.Get(server.URL + "/users/otiai10") http.Get(server.URL + "/uploads/some_image.png") fmt.Println("USER ID:", userID) fmt.Println("FILENAME:", filename)
Output: USER ID: otiai10 FILENAME: some_image.png
func (*Router) Apply ¶
func (router *Router) Apply(filters ...Filterable) error
Apply applies Filters
func (*Router) FindHandler ¶
FindHandler ...
func (*Router) GET ¶
func (router *Router) GET(path string, handler http.HandlerFunc) *Router
GET ...
func (*Router) NotFound ¶ added in v0.6.0
func (router *Router) NotFound(fun http.HandlerFunc)
func (*Router) POST ¶
func (router *Router) POST(path string, handler http.HandlerFunc) *Router
POST ...
func (*Router) ServeHTTP ¶
func (router *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
func (*Router) StaticRelative ¶
StaticRelative ...
Source Files ¶
Click to show internal directories.
Click to hide internal directories.