make it more readable

This commit is contained in:
Ankit Patial 2024-10-12 19:40:54 +05:30
parent 67667dd2ce
commit 82e92101d2
2 changed files with 37 additions and 37 deletions

View File

@ -15,15 +15,20 @@ import (
)
func main() {
// init mux router
// create a new router
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)
// let's add a route
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.Put(pattern string, h http.HandlerFunc)
@ -32,44 +37,44 @@ func main() {
// you can inline middleware(s) to a route
r.
With(mwInline).
Get("/test", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("middle ware 3 test"))
Get("/hello-2", func(w http.ResponseWriter, r *http.Request) {
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) {
// rails style routes
// rails style resource routes
// GET /photos
// GET /photos/new
// POST /photos
// GET /photos/:id
// GET /photos/:id/edit
// PUT /photos/:id
// PATCH /photos/:id
// PUT /photos/:id
// PATCH /photos/:id
// DELETE /photos/:id
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) {
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) {
grp.Use(mwGroup)
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) {
w.Write([]byte("hello there"))
})
// Serve allows graceful shutdown
// Serve allows graceful shutdown, you can use it
r.Serve(func(srv *http.Server) error {
srv.Addr = ":3001"
// srv.ReadTimeout = time.Minute
@ -80,9 +85,6 @@ func main() {
})
}
//
// example middlewares
//
func middleware1(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
slog.Info("i am middleware 1")

View File

@ -7,25 +7,21 @@ import (
"gitserver.in/patialtech/mux"
)
package main
import (
"log/slog"
"net/http"
"gitserver.in/patialtech/mux"
)
func main() {
// init mux router
// create a new router
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)
// let's add a route
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.Put(pattern string, h http.HandlerFunc)
@ -34,13 +30,13 @@ func main() {
// you can inline middleware(s) to a route
r.
With(mwInline).
Get("/test", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("middle ware 3 test"))
Get("/hello-2", func(w http.ResponseWriter, r *http.Request) {
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) {
// rails style routes
// rails style resource routes
// GET /photos
// GET /photos/new
// POST /photos
@ -50,18 +46,19 @@ func main() {
// PATCH /photos/:id
// DELETE /photos/:id
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) {
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) {
grp.Use(mwGroup)
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"))
})
// Serve allows graceful shutdown, you can use it
r.Serve(func(srv *http.Server) error {
srv.Addr = ":3001"
// srv.ReadTimeout = time.Minute