make it more readable
This commit is contained in:
parent
67667dd2ce
commit
82e92101d2
34
README.md
34
README.md
@ -15,15 +15,20 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// init mux router
|
// create a new router
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
|
|
||||||
// here is how can add middlewares, these will apply to all routes after it
|
// you can use any middleware that is: "func(http.Handler) http.Handler"
|
||||||
|
// so you can use any of it
|
||||||
|
// - https://github.com/gorilla/handlers
|
||||||
|
// - https://github.com/go-chi/chi/tree/master/middleware
|
||||||
|
|
||||||
|
// add some root level middlewares, these will apply to all routes after it
|
||||||
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("hello1"))
|
w.Write([]byte("i am route /hello"))
|
||||||
})
|
})
|
||||||
// r.Post(pattern string, h http.HandlerFunc)
|
// r.Post(pattern string, h http.HandlerFunc)
|
||||||
// r.Put(pattern string, h http.HandlerFunc)
|
// r.Put(pattern string, h http.HandlerFunc)
|
||||||
@ -32,13 +37,13 @@ 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("/test", func(w http.ResponseWriter, r *http.Request) {
|
Get("/hello-2", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("middle ware 3 test"))
|
w.Write([]byte("i am route /hello-2 with my own middleware"))
|
||||||
})
|
})
|
||||||
|
|
||||||
// you define a resource
|
// define a resource
|
||||||
r.Resource("/photos", func(resource *mux.Resource) {
|
r.Resource("/photos", func(resource *mux.Resource) {
|
||||||
// rails style routes
|
// rails style resource routes
|
||||||
// GET /photos
|
// GET /photos
|
||||||
// GET /photos/new
|
// GET /photos/new
|
||||||
// POST /photos
|
// POST /photos
|
||||||
@ -48,28 +53,28 @@ func main() {
|
|||||||
// PATCH /photos/:id
|
// PATCH /photos/:id
|
||||||
// DELETE /photos/:id
|
// DELETE /photos/:id
|
||||||
resource.Index(func(w http.ResponseWriter, r *http.Request) {
|
resource.Index(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("user index"))
|
w.Write([]byte("all photos"))
|
||||||
})
|
})
|
||||||
|
|
||||||
resource.New(func(w http.ResponseWriter, r *http.Request) {
|
resource.New(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("create new user"))
|
w.Write([]byte("upload a new pohoto"))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// you can create 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 group 1"))
|
w.Write([]byte("i am route /group"))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// catche 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"))
|
||||||
})
|
})
|
||||||
|
|
||||||
// Serve allows graceful shutdown
|
// Serve allows graceful shutdown, you can use it
|
||||||
r.Serve(func(srv *http.Server) error {
|
r.Serve(func(srv *http.Server) error {
|
||||||
srv.Addr = ":3001"
|
srv.Addr = ":3001"
|
||||||
// srv.ReadTimeout = time.Minute
|
// srv.ReadTimeout = time.Minute
|
||||||
@ -80,9 +85,6 @@ func main() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// example middlewares
|
|
||||||
//
|
|
||||||
func middleware1(h http.Handler) http.Handler {
|
func middleware1(h http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
slog.Info("i am middleware 1")
|
slog.Info("i am middleware 1")
|
||||||
|
@ -7,25 +7,21 @@ import (
|
|||||||
"gitserver.in/patialtech/mux"
|
"gitserver.in/patialtech/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log/slog"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"gitserver.in/patialtech/mux"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// init mux router
|
// create a new router
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
|
|
||||||
// here is how can add middlewares, these will apply to all routes after it
|
// you can use any middleware that is: "func(http.Handler) http.Handler"
|
||||||
|
// so you can use any of it
|
||||||
|
// - https://github.com/gorilla/handlers
|
||||||
|
// - https://github.com/go-chi/chi/tree/master/middleware
|
||||||
|
|
||||||
|
// add some root level middlewares, these will apply to all routes after it
|
||||||
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("hello1"))
|
w.Write([]byte("i am route /hello"))
|
||||||
})
|
})
|
||||||
// r.Post(pattern string, h http.HandlerFunc)
|
// r.Post(pattern string, h http.HandlerFunc)
|
||||||
// r.Put(pattern string, h http.HandlerFunc)
|
// r.Put(pattern string, h http.HandlerFunc)
|
||||||
@ -34,13 +30,13 @@ 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("/test", func(w http.ResponseWriter, r *http.Request) {
|
Get("/hello-2", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("middle ware 3 test"))
|
w.Write([]byte("i am route /hello-2 with my own middleware"))
|
||||||
})
|
})
|
||||||
|
|
||||||
// you define a resource
|
// define a resource
|
||||||
r.Resource("/photos", func(resource *mux.Resource) {
|
r.Resource("/photos", func(resource *mux.Resource) {
|
||||||
// rails style routes
|
// rails style resource routes
|
||||||
// GET /photos
|
// GET /photos
|
||||||
// GET /photos/new
|
// GET /photos/new
|
||||||
// POST /photos
|
// POST /photos
|
||||||
@ -50,18 +46,19 @@ func main() {
|
|||||||
// PATCH /photos/:id
|
// PATCH /photos/:id
|
||||||
// DELETE /photos/:id
|
// DELETE /photos/:id
|
||||||
resource.Index(func(w http.ResponseWriter, r *http.Request) {
|
resource.Index(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("user index"))
|
w.Write([]byte("all photos"))
|
||||||
})
|
})
|
||||||
|
|
||||||
resource.New(func(w http.ResponseWriter, r *http.Request) {
|
resource.New(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("create new user"))
|
w.Write([]byte("upload a new pohoto"))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// 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 group 1"))
|
w.Write([]byte("i am route /group"))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -70,6 +67,7 @@ func main() {
|
|||||||
w.Write([]byte("hello there"))
|
w.Write([]byte("hello there"))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Serve allows graceful shutdown, you can use it
|
||||||
r.Serve(func(srv *http.Server) error {
|
r.Serve(func(srv *http.Server) error {
|
||||||
srv.Addr = ":3001"
|
srv.Addr = ":3001"
|
||||||
// srv.ReadTimeout = time.Minute
|
// srv.ReadTimeout = time.Minute
|
||||||
|
Loading…
x
Reference in New Issue
Block a user