Skip to content

Instantly share code, notes, and snippets.

@philcleveland
Last active November 2, 2015 17:35
Show Gist options
  • Save philcleveland/1d8a6cb1ad33da160268 to your computer and use it in GitHub Desktop.
Save philcleveland/1d8a6cb1ad33da160268 to your computer and use it in GitHub Desktop.
Currently how I define my routes for api's in Go. Up for discussion
r := mux.NewRouter()
r.StrictSlash(true)
apiV1 := r.PathPrefix("/api/v1").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