diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..7cc7612 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM golang:alpine + +ADD . /go/src/git.netflux.io/rob/simple-http-page-docker/ +RUN go install git.netflux.io/rob/simple-http-page-docker +ENTRYPOINT /go/bin/simple-http-page-docker + +EXPOSE 3000 diff --git a/LICENSE b/LICENSE index 204b93d..1a8b7f2 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -MIT License Copyright (c) +MIT License Copyright (c) 2020 Rob Watson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 337da2a..26b5c35 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,11 @@ # simple-http-page-docker -A Docker container that runs an HTTP server returning a simple one-line plain text response. \ No newline at end of file +A Docker container that runs an HTTP server returning a simple one-line plain text response. + +Available at https://hub.docker.com/r/netfluxio/simple-http-page-docker. + +Environment variables: + +* `RESPONSE_STRING` - the HTTP response body +* `STATUS_CODE` - the numeric HTTP status code (default: 200) +* `BIND_ADDR` - bind string (default: `:3000`) diff --git a/main.go b/main.go new file mode 100644 index 0000000..1dea62b --- /dev/null +++ b/main.go @@ -0,0 +1,48 @@ +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)) +}