Skip to content
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: 24 additions & 0 deletions app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM golang:1.24.5-alpine AS builder

WORKDIR /src

COPY go.mod ./
RUN go mod download

COPY . .

RUN CGO_ENABLED=0 go build \
-trimpath \
-ldflags="-s -w" \
-o /quicknotes

FROM gcr.io/distroless/static:nonroot

COPY --from=builder /quicknotes /quicknotes
COPY --from=builder /src/seed.json /seed.json

EXPOSE 8080

USER 65532:65532

ENTRYPOINT ["/quicknotes"]
17 changes: 15 additions & 2 deletions app/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,28 @@ func NewServer(store *Store) *Server {
return &Server{store: store, requestsByCode: by}
}

func (s *Server) Routes() *http.ServeMux {
func SecurityHeaders(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("Cross-Origin-Resource-Policy", "same-origin")
w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("X-Frame-Options", "DENY")
w.Header().Set("Content-Security-Policy", "default-src 'none'; frame-ancestors 'none'")

next.ServeHTTP(w, r)
})
}

func (s *Server) Routes() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("GET /health", s.wrap(s.handleHealth))
mux.HandleFunc("GET /metrics", s.wrap(s.handleMetrics))
mux.HandleFunc("GET /notes", s.wrap(s.handleListNotes))
mux.HandleFunc("POST /notes", s.wrap(s.handleCreateNote))
mux.HandleFunc("GET /notes/{id}", s.wrap(s.handleGetNote))
mux.HandleFunc("DELETE /notes/{id}", s.wrap(s.handleDeleteNote))
return mux
return SecurityHeaders(mux)
}

type statusWriter struct {
Expand Down
9 changes: 9 additions & 0 deletions app/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,12 @@ func TestMetrics_ExposesPrometheusFormat(t *testing.T) {
}
}

func TestSecurityHeaders(t *testing.T) {
srv := newTestServer(t)

rec := do(t, srv, http.MethodGet, "/health", nil)

if rec.Header().Get("X-Content-Type-Options") != "nosniff" {
t.Fatal("missing X-Content-Type-Options header")
}
}
22 changes: 22 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
services:
quicknotes:
image: quicknotes:lab6
build:
context: ./app
user: "0:0"

ports:
- "8080:8080"

environment:
ADDR: ":8080"
DATA_PATH: "/data/notes.json"
SEED_PATH: "/seed.json"

volumes:
- quicknotes-data:/data

restart: unless-stopped

volumes:
quicknotes-data:
Loading