Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| aa6ba87f4e | |||
| 859d4fa458 | |||
| 894614cd54 |
@@ -27,7 +27,7 @@ func main() {
|
|||||||
r.Use(middleware1, middleware2)
|
r.Use(middleware1, middleware2)
|
||||||
|
|
||||||
// let's add a route
|
// let's add a route
|
||||||
r.Get("/hello", func(w http.ResponseWriter, r *http.Request) {
|
r.GET("/hello", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("i am route /hello"))
|
w.Write([]byte("i am route /hello"))
|
||||||
})
|
})
|
||||||
// r.Post(pattern string, h http.HandlerFunc)
|
// r.Post(pattern string, h http.HandlerFunc)
|
||||||
@@ -37,7 +37,7 @@ func main() {
|
|||||||
// you can inline middleware(s) to a route
|
// you can inline middleware(s) to a route
|
||||||
r.
|
r.
|
||||||
With(mwInline).
|
With(mwInline).
|
||||||
Get("/hello-2", func(w http.ResponseWriter, r *http.Request) {
|
GET("/hello-2", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("i am route /hello-2 with my own middleware"))
|
w.Write([]byte("i am route /hello-2 with my own middleware"))
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -64,13 +64,13 @@ func main() {
|
|||||||
// create a group of few routes with their own middlewares
|
// create a group of few routes with their own middlewares
|
||||||
r.Group(func(grp *mux.Router) {
|
r.Group(func(grp *mux.Router) {
|
||||||
grp.Use(mwGroup)
|
grp.Use(mwGroup)
|
||||||
grp.Get("/group", func(w http.ResponseWriter, r *http.Request) {
|
grp.GET("/group", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("i am route /group"))
|
w.Write([]byte("i am route /group"))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// catches all
|
// catches all
|
||||||
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
r.GET("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("hello there"))
|
w.Write([]byte("hello there"))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ func main() {
|
|||||||
r.Use(middleware1, middleware2)
|
r.Use(middleware1, middleware2)
|
||||||
|
|
||||||
// let's add a route
|
// let's add a route
|
||||||
r.Get("/hello", func(w http.ResponseWriter, r *http.Request) {
|
r.GET("/hello", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("i am route /hello"))
|
w.Write([]byte("i am route /hello"))
|
||||||
})
|
})
|
||||||
// r.Post(pattern string, h http.HandlerFunc)
|
// r.Post(pattern string, h http.HandlerFunc)
|
||||||
@@ -35,7 +35,7 @@ func main() {
|
|||||||
// you can inline middleware(s) to a route
|
// you can inline middleware(s) to a route
|
||||||
r.
|
r.
|
||||||
With(mwInline).
|
With(mwInline).
|
||||||
Get("/hello-2", func(w http.ResponseWriter, r *http.Request) {
|
GET("/hello-2", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("i am route /hello-2 with my own middleware"))
|
w.Write([]byte("i am route /hello-2 with my own middleware"))
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -62,13 +62,13 @@ func main() {
|
|||||||
// create a group of few routes with their own middlewares
|
// create a group of few routes with their own middlewares
|
||||||
r.Group(func(grp *mux.Router) {
|
r.Group(func(grp *mux.Router) {
|
||||||
grp.Use(mwGroup)
|
grp.Use(mwGroup)
|
||||||
grp.Get("/group", func(w http.ResponseWriter, r *http.Request) {
|
grp.GET("/group", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("i am route /group"))
|
w.Write([]byte("i am route /group"))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// catches all
|
// catches all
|
||||||
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
r.GET("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("hello there"))
|
w.Write([]byte("hello there"))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ type OriginValidator func(string) bool
|
|||||||
var (
|
var (
|
||||||
defaultCorsOptionStatusCode = http.StatusOK
|
defaultCorsOptionStatusCode = http.StatusOK
|
||||||
defaultCorsMethods = []string{http.MethodHead, http.MethodGet, http.MethodPost}
|
defaultCorsMethods = []string{http.MethodHead, http.MethodGet, http.MethodPost}
|
||||||
defaultCorsHeaders = []string{"Accept", "Accept-Language", "Content-Type", "Content-Language", "Origin"}
|
defaultCorsHeaders = []string{"Accept", "Accept-Language", "Content-Language", "Origin"}
|
||||||
// (WebKit/Safari v9 sends the Origin header by default in AJAX requests).
|
// (WebKit/Safari v9 sends the Origin header by default in AJAX requests).
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -17,9 +17,6 @@ func NewRouter() *Router {
|
|||||||
r := &Router{
|
r := &Router{
|
||||||
mux: http.NewServeMux(),
|
mux: http.NewServeMux(),
|
||||||
}
|
}
|
||||||
|
|
||||||
// catch all options
|
|
||||||
r.mux.Handle("OPTIONS /", optionsHandler{})
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
package mux
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
type optionsHandler struct{}
|
|
||||||
|
|
||||||
func (optionsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Content-Length", "0")
|
|
||||||
if r.ContentLength != 0 {
|
|
||||||
// Read up to 4KB of OPTIONS body (as mentioned in the
|
|
||||||
// spec as being reserved for future use), but anything
|
|
||||||
// over that is considered a waste of server resources
|
|
||||||
// (or an attack) and we abort and close the connection,
|
|
||||||
// courtesy of MaxBytesReader's EOF behavior.
|
|
||||||
mb := http.MaxBytesReader(w, r.Body, 4<<10)
|
|
||||||
io.Copy(io.Discard, mb)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -3,6 +3,7 @@ package mux
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@@ -13,6 +14,21 @@ type ServeCB func(srv *http.Server) error
|
|||||||
|
|
||||||
// Serve with graceful shutdown
|
// Serve with graceful shutdown
|
||||||
func (r *Router) Serve(cb ServeCB) {
|
func (r *Router) Serve(cb ServeCB) {
|
||||||
|
// catch all options
|
||||||
|
// lets get it thorugh all middlewares
|
||||||
|
r.OPTIONS("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Length", "0")
|
||||||
|
if r.ContentLength != 0 {
|
||||||
|
// Read up to 4KB of OPTIONS body (as mentioned in the
|
||||||
|
// spec as being reserved for future use), but anything
|
||||||
|
// over that is considered a waste of server resources
|
||||||
|
// (or an attack) and we abort and close the connection,
|
||||||
|
// courtesy of MaxBytesReader's EOF behavior.
|
||||||
|
mb := http.MaxBytesReader(w, r.Body, 4<<10)
|
||||||
|
io.Copy(io.Discard, mb)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Handler: r,
|
Handler: r,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user