Files
masonry/examples/custom-js-functions/templates/router.tmpl

29 lines
1.3 KiB
Cheetah

package main
import (
"net/http"
"github.com/gorilla/mux"
"your-app/controllers"
)
{{formatComment "Router setup with all entity routes\nGenerated using custom JavaScript template functions"}}
func SetupRouter() *mux.Router {
router := mux.NewRouter()
{{range $entity := .AST.Definitions}}
{{- if $entity.Entity}}
{{formatComment (printf "Routes for %s entity" $entity.Entity.Name)}}
{{$entity.Entity.Name | lower}}Controller := controllers.New{{$entity.Entity.Name | title}}Controller()
router.HandleFunc("/{{pluralize ($entity.Entity.Name | lower)}}", {{$entity.Entity.Name | lower}}Controller.List{{pluralize ($entity.Entity.Name | title)}}).Methods("GET")
router.HandleFunc("/{{pluralize ($entity.Entity.Name | lower)}}/:id", {{$entity.Entity.Name | lower}}Controller.Get{{$entity.Entity.Name | title}}).Methods("GET")
router.HandleFunc("/{{pluralize ($entity.Entity.Name | lower)}}", {{$entity.Entity.Name | lower}}Controller.Create{{$entity.Entity.Name | title}}).Methods("POST")
router.HandleFunc("/{{pluralize ($entity.Entity.Name | lower)}}/:id", {{$entity.Entity.Name | lower}}Controller.Update{{$entity.Entity.Name | title}}).Methods("PUT")
router.HandleFunc("/{{pluralize ($entity.Entity.Name | lower)}}/:id", {{$entity.Entity.Name | lower}}Controller.Delete{{$entity.Entity.Name | title}}).Methods("DELETE")
{{end}}
{{- end}}
return router
}