-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Recipe struct{} | |
func NewRecipe(*router router) *router { | |
router.HandleFunc("/recipe/{id}", GetById) | |
} | |
func GetById(writer w, *request r) *handler { | |
//do something | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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