From 794401aaadfaa4efd91587db40439226231ff303 Mon Sep 17 00:00:00 2001 From: a73x Date: Sun, 25 Aug 2024 15:14:41 +0100 Subject: initial commit --- Dockerfile | 16 +++++++++++++++ go.mod | 7 +++++++ go.sum | 14 +++++++++++++ main.go | 49 +++++++++++++++++++++++++++++++++++++++++++++ public/index.html | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 146 insertions(+) create mode 100644 Dockerfile create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 public/index.html diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..7b372e0 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM golang:1.22 as builder + +WORKDIR /usr/src/app + +COPY go.mod go.sum ./ +RUN go mod download && go mod verify + +COPY . . + +RUN CGO_ENABLED=0 go build -ldflags "-s -w" -o /bin/site . + +FROM scratch +COPY --from=builder /bin/site /app + +ENTRYPOINT ["/app"] + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f7db60e --- /dev/null +++ b/go.mod @@ -0,0 +1,7 @@ +module git.sr.ht/~a73x/home + +go 1.22.5 + +require go.uber.org/zap v1.27.0 + +require go.uber.org/multierr v1.10.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2d29b57 --- /dev/null +++ b/go.sum @@ -0,0 +1,14 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= +go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= +go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..d594cd1 --- /dev/null +++ b/main.go @@ -0,0 +1,49 @@ +package main + +import ( + "embed" + "io/fs" + "log" + "net/http" + "time" + + "go.uber.org/zap" +) + +//go:embed public/index.html +var content embed.FS + +func main() { + logger, _ := zap.NewProduction() + loggingMiddleware := func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + start := time.Now() + next.ServeHTTP(w, r) + logger.Info("request received", + zap.String("url", r.URL.Path), + zap.String("method", r.Method), + zap.Duration("duration", time.Since(start)), + zap.String("user-agent", r.UserAgent()), + ) + }) + } + + mux := http.NewServeMux() + + staticFs, err := fs.Sub(content, "public") + if err != nil { + log.Fatal(err) + } + + mux.Handle("/", http.FileServer(http.FS(staticFs))) + + server := http.Server{ + Addr: ":8080", + Handler: loggingMiddleware(mux), + } + + err = server.ListenAndServe() + if err != nil { + log.Fatal(err) + } +} diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..3a00176 --- /dev/null +++ b/public/index.html @@ -0,0 +1,60 @@ + + + + + + + + + + a73x + + + +

a73x

+ +

daily learnings

+

#go

+ +

#git

+ + +

Books

+ + + + + -- cgit v1.2.3