Skip to content

Instantly share code, notes, and snippets.

@jchannon
Forked from philcleveland/routes.go
Last active November 2, 2015 17:41
Show Gist options
  • Save jchannon/64d70d49edb4cad854c5 to your computer and use it in GitHub Desktop.
Save jchannon/64d70d49edb4cad854c5 to your computer and use it in GitHub Desktop.
Currently how I define my routes for api's in Go. Up for discussion
type Recipe struct{}
func NewRecipe(*router router) *router {
router.HandleFunc("/recipe/{id}", GetById)
}
func GetById(writer w, *request r) *handler {
//do something
}
r := mux.NewRouter()
r.StrictSlash(true)
apiV1 := r.PathPrefix("/api/v1").Subrouter()
recipeRouter := Recipe.NewRecipe(r.PathPrefix("/api").SubRouter())
//recipes
apiV1.HandleFunc("/{userID}/recipes/", ws.GetAllRecipes).Methods("GET")
apiV1.HandleFunc("/{userID}/recipes/{recipeID}", ws.GetRecipeByID).Methods("GET")
apiV1.HandleFunc("/{userID}/recipes/", ws.CreateRecipe).Methods("POST")
apiV1.HandleFunc("/{userID}/recipes/{recipeID}", ws.EditRecipe).Methods("PUT")
apiV1.HandleFunc("/{userID}/recipes/{recipeID}", ws.DeleteRecipe).Methods("DELETE")
//fermentables
apiV1.HandleFunc("/fermentables/", ws.GetFermentables).Methods("GET")
apiV1.HandleFunc("/fermentables/{id}", ws.GetFermentable).Methods("GET")
//yeasts
apiV1.HandleFunc("/yeasts/", ws.GetYeasts).Methods("GET")
apiV1.HandleFunc("/yeasts/{id}", ws.GetYeast).Methods("GET")
//middleware for CORS
handler := cors.New(cors.Options{
AllowedOrigins: []string{"http://localhost:8081"},
AllowCredentials: true,
}).Handler(r)
log.Printf("Running at %s\n", address)
server := &http.Server{
Addr: address,
Handler: handler,
// TLSConfig: config,
}
log.Fatal(server.ListenAndServe())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment