Created
April 1, 2019 02:27
-
-
Save rzane/31dc53c8902170322df218c95fbfbcc3 to your computer and use it in GitHub Desktop.
Tiny debug image
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
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"] |
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 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