Skip to content

Instantly share code, notes, and snippets.

@vzool
Last active April 11, 2018 18:29
Show Gist options
  • Save vzool/0fa18296f18fa84543fb3f5259a38664 to your computer and use it in GitHub Desktop.
Save vzool/0fa18296f18fa84543fb3f5259a38664 to your computer and use it in GitHub Desktop.
This is an example for Pyramid Hierarchy URL routes implemented using [Bone](https://github.com/go-zoo/bone)
package main
import (
"fmt"
"net/http"
"github.com/go-zoo/bone"
)
func main() {
mux := bone.New()
// Country
mux.Get("/country", http.HandlerFunc(CountryIndex))
mux.Get("/country/create", http.HandlerFunc(CountryCreate))
mux.Post("/country", http.HandlerFunc(CountryStore))
mux.Get("/country/:country_id", http.HandlerFunc(CountryGet))
mux.Get("/country/:country_id/edit", http.HandlerFunc(CountryEdit))
mux.Put("/country/:country_id", http.HandlerFunc(CountryPut))
mux.Delete("/country/:country_id", http.HandlerFunc(CountryDelete))
// City
mux.Get("/country/:country_id/city", http.HandlerFunc(CityIndex))
mux.Get("/country/:country_id/city/create", http.HandlerFunc(CityCreate))
mux.Post("/country/:country_id/city", http.HandlerFunc(CityStore))
mux.Get("/country/:country_id/city/:city_id", http.HandlerFunc(CityGet))
mux.Get("/country/:country_id/city/:city_id/edit", http.HandlerFunc(CityEdit))
mux.Put("/country/:country_id/city/:city_id", http.HandlerFunc(CityPut))
mux.Delete("/country/:country_id/city/:city_id", http.HandlerFunc(CityDelete))
// District
mux.Get("/country/:country_id/city/:city_id/district", http.HandlerFunc(DistrictIndex))
mux.Get("/country/:country_id/city/:city_id/district/create", http.HandlerFunc(DistrictCreate))
mux.Post("/country/:country_id/city/:city_id/district", http.HandlerFunc(DistrictStore))
mux.Get("/country/:country_id/city/:city_id/district/:district_id", http.HandlerFunc(DistrictGet))
mux.Get("/country/:country_id/city/:city_id/district/:district_id/edit", http.HandlerFunc(DistrictEdit))
mux.Put("/country/:country_id/city/:city_id/district/:district_id", http.HandlerFunc(DistrictPut))
mux.Delete("/country/:country_id/city/:city_id/district/:district_id", http.HandlerFunc(DistrictDelete))
http.ListenAndServe(":8888", mux)
}
// #####################################################################
// ####################### Country Handlers ############################
// #####################################################################
// CountryIndex to something nice
func CountryIndex(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte("CountryIndex"))
}
// CountryCreate to something nice
func CountryCreate(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte("CountryCreate"))
}
// CountryStore to something nice
func CountryStore(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte("CountryStore"))
}
// CountryGet to something nice
func CountryGet(rw http.ResponseWriter, req *http.Request) {
// Get the value of the "id" parameters.
val := bone.GetValue(req, "country_id")
rw.Write([]byte(fmt.Sprintf("CountryGet: %s", val)))
}
// CountryEdit to something nice
func CountryEdit(rw http.ResponseWriter, req *http.Request) {
// Get the value of the "id" parameters.
val := bone.GetValue(req, "country_id")
rw.Write([]byte(fmt.Sprintf("CountryEdit: %s", val)))
}
// CountryPut to something nice
func CountryPut(rw http.ResponseWriter, req *http.Request) {
// Get the value of the "id" parameters.
val := bone.GetValue(req, "country_id")
rw.Write([]byte(fmt.Sprintf("CountryPut: %s", val)))
}
// CountryDelete to something nice
func CountryDelete(rw http.ResponseWriter, req *http.Request) {
// Get the value of the "id" parameters.
val := bone.GetValue(req, "country_id")
rw.Write([]byte(fmt.Sprintf("CountryDelete: %s", val)))
}
// #####################################################################
// ######################## CITY Handlers ##############################
// #####################################################################
// CityIndex to something nice
func CityIndex(rw http.ResponseWriter, req *http.Request) {
country_id := bone.GetValue(req, "country_id")
rw.Write([]byte(fmt.Sprintf("CityIndex: %s", country_id)))
}
// CityCreate to something nice
func CityCreate(rw http.ResponseWriter, req *http.Request) {
country_id := bone.GetValue(req, "country_id")
rw.Write([]byte(fmt.Sprintf("CityCreate: %s", country_id)))
}
// CityStore to something nice
func CityStore(rw http.ResponseWriter, req *http.Request) {
country_id := bone.GetValue(req, "country_id")
rw.Write([]byte(fmt.Sprintf("CityStore: %s", country_id)))
}
// CityGet to something nice
func CityGet(rw http.ResponseWriter, req *http.Request) {
// Get the value of the "id" parameters.
country_id := bone.GetValue(req, "country_id")
city_id := bone.GetValue(req, "city_id")
rw.Write([]byte(fmt.Sprintf("CityGet: %s-%s", country_id, city_id)))
}
// CityEdit to something nice
func CityEdit(rw http.ResponseWriter, req *http.Request) {
// Get the value of the "id" parameters.
country_id := bone.GetValue(req, "country_id")
city_id := bone.GetValue(req, "city_id")
rw.Write([]byte(fmt.Sprintf("CityEdit: %s-%s", country_id, city_id)))
}
// CityPut to something nice
func CityPut(rw http.ResponseWriter, req *http.Request) {
// Get the value of the "id" parameters.
country_id := bone.GetValue(req, "country_id")
city_id := bone.GetValue(req, "city_id")
rw.Write([]byte(fmt.Sprintf("CityPut: %s-%s", country_id, city_id)))
}
// CityDelete to something nice
func CityDelete(rw http.ResponseWriter, req *http.Request) {
// Get the value of the "id" parameters.
country_id := bone.GetValue(req, "country_id")
city_id := bone.GetValue(req, "city_id")
rw.Write([]byte(fmt.Sprintf("CityDelete: %s-%s", country_id, city_id)))
}
// #####################################################################
// ###################### DISTRICT Handlers ############################
// #####################################################################
// DistrictIndex to something nice
func DistrictIndex(rw http.ResponseWriter, req *http.Request) {
country_id := bone.GetValue(req, "country_id")
city_id := bone.GetValue(req, "city_id")
rw.Write([]byte(fmt.Sprintf("DistrictIndex: %s-%s", country_id, city_id)))
}
// DistrictCreate to something nice
func DistrictCreate(rw http.ResponseWriter, req *http.Request) {
country_id := bone.GetValue(req, "country_id")
city_id := bone.GetValue(req, "city_id")
rw.Write([]byte(fmt.Sprintf("DistrictCreate: %s-%s", country_id, city_id)))
}
// DistrictStore to something nice
func DistrictStore(rw http.ResponseWriter, req *http.Request) {
country_id := bone.GetValue(req, "country_id")
city_id := bone.GetValue(req, "city_id")
rw.Write([]byte(fmt.Sprintf("DistrictStore: %s-%s", country_id, city_id)))
}
// DistrictGet to something nice
func DistrictGet(rw http.ResponseWriter, req *http.Request) {
// Get the value of the "id" parameters.
country_id := bone.GetValue(req, "country_id")
city_id := bone.GetValue(req, "city_id")
district_id := bone.GetValue(req, "district_id")
rw.Write([]byte(fmt.Sprintf("DistrictGet: %s-%s-%s", country_id, city_id, district_id)))
}
// DistrictEdit to something nice
func DistrictEdit(rw http.ResponseWriter, req *http.Request) {
// Get the value of the "id" parameters.
country_id := bone.GetValue(req, "country_id")
city_id := bone.GetValue(req, "city_id")
district_id := bone.GetValue(req, "district_id")
rw.Write([]byte(fmt.Sprintf("DistrictEdit: %s-%s-%s", country_id, city_id, district_id)))
}
// DistrictPut to something nice
func DistrictPut(rw http.ResponseWriter, req *http.Request) {
// Get the value of the "id" parameters.
country_id := bone.GetValue(req, "country_id")
city_id := bone.GetValue(req, "city_id")
district_id := bone.GetValue(req, "district_id")
rw.Write([]byte(fmt.Sprintf("DistrictPut: %s-%s-%s", country_id, city_id, district_id)))
}
// DistrictDelete to something nice
func DistrictDelete(rw http.ResponseWriter, req *http.Request) {
// Get the value of the "id" parameters.
country_id := bone.GetValue(req, "country_id")
city_id := bone.GetValue(req, "city_id")
district_id := bone.GetValue(req, "district_id")
rw.Write([]byte(fmt.Sprintf("DistrictDelete: %s-%s-%s", country_id, city_id, district_id)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment