49 lines
907 B
Go
49 lines
907 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
const (
|
|
defaultBindAddress = ":3000"
|
|
defaultResponseString = "Hello world"
|
|
defaultResponseStatus = http.StatusOK
|
|
)
|
|
|
|
func main() {
|
|
addr, ok := os.LookupEnv("BIND_ADDR")
|
|
if !ok {
|
|
addr = defaultBindAddress
|
|
}
|
|
|
|
responseString, ok := os.LookupEnv("RESPONSE_STRING")
|
|
if !ok {
|
|
responseString = defaultResponseString
|
|
}
|
|
|
|
var statusCode int
|
|
statusCodeString, ok := os.LookupEnv("STATUS_CODE")
|
|
if ok {
|
|
var err error
|
|
statusCode, err = strconv.Atoi(statusCodeString)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
} else {
|
|
statusCode = defaultResponseStatus
|
|
}
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
w.WriteHeader(statusCode)
|
|
fmt.Fprintf(w, responseString)
|
|
})
|
|
|
|
fmt.Printf("Listening on %s...\n", addr)
|
|
log.Fatal(http.ListenAndServe(addr, nil))
|
|
}
|