Created
June 25, 2018 13:20
-
-
Save ohookins/f36baf110275f585493bb144b685b227 to your computer and use it in GitHub Desktop.
Simple Go-based HTTP health smoke test 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" | |
"flag" | |
"io" | |
"log" | |
"net/http" | |
"strconv" | |
) | |
var ( | |
statusCode = 200 | |
port = flag.Int("port", 80, "Port number") | |
) | |
func StatusServer(w http.ResponseWriter, req *http.Request) { | |
log.Printf("%v\n", req) | |
if req.Method == "GET" { | |
w.WriteHeader(statusCode) | |
io.WriteString(w, fmt.Sprintf("Port %d, status code is %d\n", *port, statusCode)) | |
return | |
} | |
if req.Method == "POST" { | |
w.WriteHeader(201) | |
newStatus := req.URL.Query()["status"] | |
if len(newStatus) > 0 { | |
i, _ := strconv.Atoi(newStatus[0]) | |
statusCode = i | |
io.WriteString(w, "Accepted new status code: " + newStatus[0] + "\n") | |
return | |
} | |
io.WriteString(w, "Did not understand\n") | |
return | |
} | |
} | |
func main() { | |
flag.Parse() | |
log.Printf("Listening on port %d\n", *port) | |
http.HandleFunc("/", StatusServer) | |
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment