Split code in respective files.
Resource method name change. Route list func
This commit is contained in:
@@ -10,8 +10,8 @@ import (
|
||||
|
||||
func main() {
|
||||
// create a new router
|
||||
r := mux.NewRouter()
|
||||
r.Use(middleware.CORS(middleware.CORSOption{
|
||||
m := mux.New()
|
||||
m.Use(middleware.CORS(middleware.CORSOption{
|
||||
AllowedOrigins: []string{"*"},
|
||||
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
||||
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-AccessToken", "X-Real-IP"},
|
||||
@@ -20,7 +20,7 @@ func main() {
|
||||
MaxAge: 300,
|
||||
}))
|
||||
|
||||
r.Use(middleware.Helmet(middleware.HelmetOption{
|
||||
m.Use(middleware.Helmet(middleware.HelmetOption{
|
||||
StrictTransportSecurity: &middleware.TransportSecurity{
|
||||
MaxAge: 31536000,
|
||||
IncludeSubDomains: true,
|
||||
@@ -36,10 +36,10 @@ func main() {
|
||||
// - 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)
|
||||
m.Use(middleware1, middleware2)
|
||||
|
||||
// let's add a route
|
||||
r.GET("/hello", func(w http.ResponseWriter, r *http.Request) {
|
||||
m.GET("/hello", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("i am route /hello"))
|
||||
})
|
||||
// r.Post(pattern string, h http.HandlerFunc)
|
||||
@@ -47,34 +47,45 @@ func main() {
|
||||
// ...
|
||||
|
||||
// you can inline middleware(s) to a route
|
||||
r.
|
||||
m.
|
||||
With(mwInline).
|
||||
GET("/hello-2", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("i am route /hello-2 with my own middleware"))
|
||||
})
|
||||
|
||||
// define a resource
|
||||
r.Resource("/photos", func(resource *mux.Resource) {
|
||||
// Rails style resource routes
|
||||
// GET /photos
|
||||
// GET /photos/new
|
||||
// POST /photos
|
||||
// GET /photos/:id
|
||||
// GET /photos/:id/edit
|
||||
// PUT /photos/:id
|
||||
// PATCH /photos/:id
|
||||
// DELETE /photos/:id
|
||||
resource.Index(func(w http.ResponseWriter, r *http.Request) {
|
||||
m.Resource("/photos", func(res *mux.Resource) {
|
||||
res.Index(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("all photos"))
|
||||
})
|
||||
|
||||
resource.New(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("upload a new pohoto"))
|
||||
res.CreateView(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("new photo view"))
|
||||
})
|
||||
|
||||
res.Create(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("new photo"))
|
||||
})
|
||||
|
||||
res.View(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("view photo detail"))
|
||||
})
|
||||
|
||||
res.Update(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("update photos"))
|
||||
})
|
||||
|
||||
res.UpdatePartial(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("update few of photo fields"))
|
||||
})
|
||||
|
||||
res.Delete(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("removed a phot"))
|
||||
})
|
||||
})
|
||||
|
||||
// create a group of few routes with their own middlewares
|
||||
r.Group(func(grp *mux.Router) {
|
||||
m.Group(func(grp *mux.Mux) {
|
||||
grp.Use(mwGroup)
|
||||
grp.GET("/group", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("i am route /group"))
|
||||
@@ -82,12 +93,16 @@ func main() {
|
||||
})
|
||||
|
||||
// catches all
|
||||
r.GET("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
m.GET("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("hello there"))
|
||||
})
|
||||
|
||||
m.GET("/routes", func(w http.ResponseWriter, r *http.Request) {
|
||||
m.PrintRoutes(w)
|
||||
})
|
||||
|
||||
// Serve allows graceful shutdown, you can use it
|
||||
r.Serve(func(srv *http.Server) error {
|
||||
m.Serve(func(srv *http.Server) error {
|
||||
srv.Addr = ":3001"
|
||||
// srv.ReadTimeout = time.Minute
|
||||
// srv.WriteTimeout = time.Minute
|
||||
|
Reference in New Issue
Block a user