all repos — homestead @ 6b6a67390b7366678883f74faf0747c794998448

Code for my website

embed files

Alan Pearce
commit

6b6a67390b7366678883f74faf0747c794998448

parent

59227d920697c1cc8e5457f9532b3e8486900eb5

3 files changed, 16 insertions(+), 4 deletions(-)

jump to
M .dockerignore.dockerignore
@@ -1,5 +1,6 @@
* !cmd +!server.go !content !internal !static
M DockerfileDockerfile
@@ -16,7 +16,7 @@ RUN go run ./cmd/build
RUN cp -r static/.well-known static/* public ENV GOOS=linux GOARCH=amd64 CGO_ENABLED=0 -RUN go build ./cmd/server/ +RUN go build server.go # Final stage for app image FROM gcr.io/distroless/static
@@ -25,7 +25,6 @@ WORKDIR /app
# Copy built application COPY --link config.toml . -COPY --from=builder /app/public public COPY --from=builder /app/server server # Start the server by default, this can be overwritten at runtime
M cmd/server/server.goserver.go
@@ -1,6 +1,7 @@
package main import ( + "embed" "fmt" "log" "net/http"
@@ -30,6 +31,9 @@
type Host struct { Fiber *fiber.App } + +//go:embed all:public/* +var fs embed.FS func main() { err := sentry.Init(sentry.ClientOptions{
@@ -93,12 +97,20 @@ }))
website.Use(recover.New(recover.Config{})) + files := http.FS(fs) notFoundHandler := func(c *fiber.Ctx) error { - return c.Status(fiber.StatusNotFound).SendFile("public/404.html") + c.Status(fiber.StatusNotFound).Type("html", "utf-8") + content, err := fs.ReadFile("public/404.html") + if err != nil { + content = []byte("404 Not Found") + c.Type("txt") + } + return c.Send(content) } website.Get("/404.html", notFoundHandler) website.Use("/", filesystem.New(filesystem.Config{ - Root: http.Dir("./public"), + Root: files, + PathPrefix: "public", ContentTypeCharset: "utf-8", MaxAge: int((24 * time.Hour).Seconds()), }))