Last active
October 27, 2020 07:35
-
-
Save shashank404error/93381b1b5f42c655b5b4c3c5e631c6e7 to your computer and use it in GitHub Desktop.
A simple go server with a single route.
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
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