Last active
October 12, 2015 15:56
-
-
Save keithchambers/f48a8cdd700cc88aa361 to your computer and use it in GitHub Desktop.
simple go http server
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
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