Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dockerfile Build, Certificate Generation, and Health Check #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
FROM golang:1.13-alpine3.10 AS build

WORKDIR /go/src/app

ARG CGO_ENABLED=0

RUN apk --no-cache add ca-certificates git openssl && \
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes -subj "/CN=example.com"

COPY . .

RUN go get -d -v ./... && \
go build -ldflags "-s -w" -o ./echo-server ./cmd/echo-server

FROM scratch
COPY bin /bin

WORKDIR /bin

COPY --from=build /go/src/app/echo-server .
COPY --from=build /go/src/app/cert.pem .
COPY --from=build /go/src/app/key.pem .

ENV PORT 8080
ENV SSLPORT 8443

EXPOSE 8080 8443
EXPOSE "$PORT" "$SSLPORT"

ENV ADD_HEADERS='{"X-Real-Server": "echo-server"}'

WORKDIR /bin
ENTRYPOINT ["/bin/echo-server"]
30 changes: 0 additions & 30 deletions bin/cert.pem

This file was deleted.

Binary file removed bin/echo-server
Binary file not shown.
52 changes: 0 additions & 52 deletions bin/key.pem

This file was deleted.

29 changes: 25 additions & 4 deletions cmd/echo-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func RunServer(addr string, sslAddr string, ssl map[string]string) chan error {
if err := http.ListenAndServe(addr, nil); err != nil {
errs <- err
}

}()

go func() {
Expand Down Expand Up @@ -70,18 +69,40 @@ var upgrader = websocket.Upgrader{
}

func handler(wr http.ResponseWriter, req *http.Request) {
fmt.Printf("%s | %s %s\n", req.RemoteAddr, req.Method, req.URL)
fmt.Printf("%s | %s | %s %s\n", req.RemoteAddr, req.UserAgent(), req.Method, req.URL)
if websocket.IsWebSocketUpgrade(req) {
serveWebSocket(wr, req)
} else if req.URL.Path == "/ws" {
} else if req.URL.Path == "/ws" {
wr.Header().Add("Content-Type", "text/html")
wr.WriteHeader(200)
io.WriteString(wr, websocketHTML)
} else {
} else if req.URL.Path == "/health" {
healthEndpoint(wr, req)
} else {
serveHTTP(wr, req)
}
}

func healthEndpoint(wr http.ResponseWriter, req *http.Request) {

// Health Check endpoint

type Health struct {
Status string
}

health := Health{"ok"}

healthResponse, err := json.Marshal(health)
if err != nil {
panic(err)
}

wr.Header().Set("Content-Type", "application/json")
wr.Write(healthResponse)

}

func serveWebSocket(wr http.ResponseWriter, req *http.Request) {
connection, err := upgrader.Upgrade(wr, req, nil)
if err != nil {
Expand Down