4 Commits

Author SHA1 Message Date
aa6ba87f4e default header reset 2024-11-04 11:26:10 +05:30
859d4fa458 option handler 2024-11-04 11:06:49 +05:30
894614cd54 default options handler 2024-11-04 11:00:02 +05:30
c34f5b7d0d route catch optiong route 2024-11-04 10:40:29 +05:30
4 changed files with 44 additions and 29 deletions

View File

@@ -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"))
}) })

View File

@@ -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"))
}) })

View File

@@ -14,9 +14,10 @@ type Router struct {
} }
func NewRouter() *Router { func NewRouter() *Router {
return &Router{ r := &Router{
mux: http.NewServeMux(), mux: http.NewServeMux(),
} }
return r
} }
// Use will register middleware(s) with router stack // Use will register middleware(s) with router stack
@@ -27,48 +28,46 @@ func (r *Router) Use(h ...func(http.Handler) http.Handler) {
r.middlewares = append(r.middlewares, h...) r.middlewares = append(r.middlewares, h...)
} }
// Get method route // GET method route
func (r *Router) Get(pattern string, h http.HandlerFunc) { func (r *Router) GET(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodGet, pattern, h) r.handlerFunc(http.MethodGet, pattern, h)
} }
// Head method route // HEAD method route
func (r *Router) Head(pattern string, h http.HandlerFunc) { func (r *Router) HEAD(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodHead, pattern, h) r.handlerFunc(http.MethodHead, pattern, h)
} }
// Post method route // POST method route
func (r *Router) Post(pattern string, h http.HandlerFunc) { func (r *Router) POST(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodPost, pattern, h) r.handlerFunc(http.MethodPost, pattern, h)
} }
// Put method route // PUT method route
func (r *Router) Put(pattern string, h http.HandlerFunc) { func (r *Router) PUT(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodPut, pattern, h) r.handlerFunc(http.MethodPut, pattern, h)
} }
// Patch method route // PATCH method route
func (r *Router) Patch(pattern string, h http.HandlerFunc) { func (r *Router) PATCH(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodPatch, pattern, h) r.handlerFunc(http.MethodPatch, pattern, h)
} }
// Delete method route // DELETE method route
func (r *Router) Delete(pattern string, h http.HandlerFunc) { func (r *Router) DELETE(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodDelete, pattern, h) r.handlerFunc(http.MethodDelete, pattern, h)
} }
// Connect method route // CONNECT method route
func (r *Router) Connect(pattern string, h http.HandlerFunc) { func (r *Router) CONNECT(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodConnect, pattern, h) r.handlerFunc(http.MethodConnect, pattern, h)
} } // OPTIONS method route
func (r *Router) OPTIONS(pattern string, h http.HandlerFunc) {
// Options method route
func (r *Router) Options(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodOptions, pattern, h) r.handlerFunc(http.MethodOptions, pattern, h)
} }
// Trace method route // TRACE method route
func (r *Router) Trace(pattern string, h http.HandlerFunc) { func (r *Router) TRACE(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodTrace, pattern, h) r.handlerFunc(http.MethodTrace, pattern, h)
} }

View File

@@ -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,
} }