about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2024-04-17 22:39:45 +0200
committerAlan Pearce2024-04-18 09:14:23 +0200
commit22a2e624602212cf68b40d9cab54164c7051210c (patch)
treefab7dea20da38b2a1cdc00a5440da8285804d79e
parentc1d191576c80dada81671834539ed93ebcb63461 (diff)
downloadwebsite-22a2e624602212cf68b40d9cab54164c7051210c.tar.lz
website-22a2e624602212cf68b40d9cab54164c7051210c.tar.zst
website-22a2e624602212cf68b40d9cab54164c7051210c.zip
embed files
-rw-r--r--.dockerignore1
-rw-r--r--Dockerfile3
-rw-r--r--server.go (renamed from cmd/server/server.go)16
3 files changed, 16 insertions, 4 deletions
diff --git a/.dockerignore b/.dockerignore
index 359576c..48be77d 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -1,5 +1,6 @@
 *
 !cmd
+!server.go
 !content
 !internal
 !static
diff --git a/Dockerfile b/Dockerfile
index 90dd051..5929443 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -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
diff --git a/cmd/server/server.go b/server.go
index 7665143..2a1eed0 100644
--- a/cmd/server/server.go
+++ b/server.go
@@ -1,6 +1,7 @@
 package main
 
 import (
+	"embed"
 	"fmt"
 	"log"
 	"net/http"
@@ -31,6 +32,9 @@ type Host struct {
 	Fiber *fiber.App
 }
 
+//go:embed all:public/*
+var fs embed.FS
+
 func main() {
 	err := sentry.Init(sentry.ClientOptions{
 		Dsn:         os.Getenv("SENTRY_DSN"),
@@ -93,12 +97,20 @@ func main() {
 
 	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()),
 	}))