Skip to content

Instantly share code, notes, and snippets.

@olafkotur
Created January 29, 2020 20:33
Show Gist options
  • Save olafkotur/4864d170a9307e675b6c0c8d83ddae5f to your computer and use it in GitHub Desktop.
Save olafkotur/4864d170a9307e675b6c0c8d83ddae5f to your computer and use it in GitHub Desktop.
REST Server setup using Gorilla mux in Golang
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