Skip to content

Instantly share code, notes, and snippets.

@keithchambers
Last active October 12, 2015 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keithchambers/f48a8cdd700cc88aa361 to your computer and use it in GitHub Desktop.
Save keithchambers/f48a8cdd700cc88aa361 to your computer and use it in GitHub Desktop.
simple go http server
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func handle(writer http.ResponseWriter, request *http.Request) {
log.Printf("%s", request.URL.Path)
if "ok" == request.URL.Path[1:] {
writer.WriteHeader(http.StatusOK)
fmt.Fprintf(writer, "OK\n")
} else if "fail" == request.URL.Path[1:] {
writer.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(writer, "FAIL\n")
} else {
writer.WriteHeader(http.StatusOK)
fmt.Fprintf(writer, "Hello, World!\n")
}
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
http.HandleFunc("/", handle)
log.Printf("server started on port %v", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment