Skip to content

Instantly share code, notes, and snippets.

@shashank404error
Last active October 27, 2020 07:35
Show Gist options
  • Save shashank404error/93381b1b5f42c655b5b4c3c5e631c6e7 to your computer and use it in GitHub Desktop.
Save shashank404error/93381b1b5f42c655b5b4c3c5e631c6e7 to your computer and use it in GitHub Desktop.
A simple go server with a single route.
package main
import (
"net/http"
"os"
"log"
"github.com/gorilla/mux"
)
func determineListenAddress() (string, error) {
port := os.Getenv("PORT")
if port == ""{
port = "80"
}
return ":" + port, nil
}
func main(){
addr, err := determineListenAddress()
if err != nil {
log.Fatal(err)
}
r := mux.NewRouter()
r.HandleFunc("/", index).Methods("GET")
http.Handle("/",r)
if err := http.ListenAndServe(addr, nil);
err != nil {
panic(err)
}
}
func index(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"message": "hello world"}`))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment