Skip to content

Instantly share code, notes, and snippets.

@rzane
Created April 1, 2019 02:27
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 rzane/31dc53c8902170322df218c95fbfbcc3 to your computer and use it in GitHub Desktop.
Save rzane/31dc53c8902170322df218c95fbfbcc3 to your computer and use it in GitHub Desktop.
Tiny debug image
FROM golang:alpine as build
WORKDIR /usr/src/app
RUN apk --no-cache add upx
COPY ./server.go ./
ENV CGO_ENABLED=0
RUN go build -o server server.go
RUN upx --brute server
# ---
FROM scratch
COPY --from=build /usr/src/app/server ./server
ENTRYPOINT ["./server"]
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func respondOk(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "OK")
}
func respondEnv(w http.ResponseWriter, r *http.Request) {
for _, pair := range os.Environ() {
fmt.Fprintln(w, pair)
}
}
func respondName(w http.ResponseWriter, r *http.Request) {
name := os.Getenv("NAME")
if len(name) == 0 {
fmt.Fprintln(w, "NONE")
} else {
fmt.Fprintln(w, name)
}
}
func withLogging(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
handler.ServeHTTP(w, r)
})
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", respondOk)
mux.HandleFunc("/env", respondEnv)
mux.HandleFunc("/name", respondName)
log.Println("Listening on port 8080...")
log.Fatal(http.ListenAndServe(":8080", withLogging(mux)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment