Created
January 29, 2020 20:33
-
-
Save olafkotur/4864d170a9307e675b6c0c8d83ddae5f to your computer and use it in GitHub Desktop.
REST Server setup using Gorilla mux in Golang
This file contains 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
import "github.com/gorilla/mux" | |
// Setup and create handlers | |
router := mux.NewRouter().StrictSlash(true) | |
router.HandleFunc("/api/example", getExample).Methods("GET") | |
// Able to set up a function to execute before each request | |
_ = http.ListenAndServe(":"+port, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
// Do something here | |
router.ServeHTTP(w, r) | |
})) | |
// Example handler | |
func getExample(w http.ResponseWriter, r *http.Request) { | |
msg := []byte("Hello World") | |
w.Header().Set("Content-Type", "some/type") | |
w.Write(msg) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment