Embed static assets and support external configuration

This commit is contained in:
Reese Norris
2025-05-18 14:13:44 -07:00
parent 0d8a0af404
commit f03855b7db
9 changed files with 113 additions and 17 deletions

6
.dockerignore Normal file
View File

@@ -0,0 +1,6 @@
docker-compose.yml
.gitignore
mkdocs.yml
README.md
docs
**tmp**

25
Dockerfile_fsd Normal file
View File

@@ -0,0 +1,25 @@
FROM golang:1.24 AS build
WORKDIR /go/src/fsd
COPY go.mod go.sum ./
# Cache module downloads
RUN go mod download
COPY . .
# Cache builds
ENV GOCACHE=/root/.cache/go-build
RUN --mount=type=cache,target="/root/.cache/go-build" CGO_ENABLED=0 go build -o /go/bin/fsd
FROM alpine:latest
RUN addgroup -g 2001 nonroot && \
adduser -u 2001 -G nonroot -D nonroot && \
mkdir /db && chown -R nonroot:nonroot /db
COPY --from=build --chown=nonroot:nonroot /go/bin/fsd /
USER 2001:2001
CMD ["/fsd"]

27
Dockerfile_web Normal file
View File

@@ -0,0 +1,27 @@
FROM golang:1.24 AS build
WORKDIR /go/src/fsdweb
COPY go.mod go.sum ./
# Cache module downloads
RUN go mod download
COPY . .
# Cache builds
ENV GOCACHE=/root/.cache/go-build
RUN --mount=type=cache,target="/root/.cache/go-build" \
cd web && \
CGO_ENABLED=0 go build -o /go/bin/fsdweb
FROM alpine:latest
RUN addgroup -g 2001 nonroot && \
adduser -u 2001 -G nonroot -D nonroot && \
mkdir /db && chown -R nonroot:nonroot /db
COPY --from=build --chown=nonroot:nonroot /go/bin/fsdweb /
USER 2001:2001
CMD ["/fsdweb"]

39
docker-compose.yml Normal file
View File

@@ -0,0 +1,39 @@
services:
fsd:
build:
context: .
dockerfile: Dockerfile_fsd
restart: unless-stopped
container_name: openfsd_fsd
hostname: openfsd_fsd
expose:
- "13618/tcp" # Internal HTTP REST API service. The webserver talks to this in order to obtain FSD state info.
ports:
- "6809/tcp"
environment:
DATABASE_SOURCE_NAME: /db/openfsd.db?_pragma=busy_timeout(5000)&_pragma=journal_mode(WAL)
DATABASE_AUTO_MIGRATE: true
volumes:
- sqlite:/db
fsdweb:
build:
context: .
dockerfile: Dockerfile_web
restart: unless-stopped
container_name: openfsd_web
hostname: openfsd_web
expose:
- "8000/tcp"
environment:
DATABASE_SOURCE_NAME: /db/openfsd.db?_pragma=busy_timeout(5000)&_pragma=journal_mode(WAL)
FSD_HTTP_SERVICE_ADDRESS: "http://openfsd_fsd:13618"
volumes:
- sqlite:/db
networks:
openfsd:
name: openfsd_net
volumes:
sqlite:

View File

@@ -11,7 +11,7 @@ type ServerConfig struct {
DatabaseDriver string `env:"DATABASE_DRIVER, default=sqlite"` // Golang sql database driver name
DatabaseSourceName string `env:"DATABASE_SOURCE_NAME, default=:memory:"` // Golang sql database source name
DatabaseAutoMigrate bool `env:"DATABASE_AUTO_MIGRATE, default=false"` // Whether to automatically run database migrations on startup
DatabaseMaxConns int `env:"DATABASE_MAX_CONNS, default=4"` // Max number of database connections
DatabaseMaxConns int `env:"DATABASE_MAX_CONNS, default=1"` // Max number of database connections
NumMetarWorkers int `env:"NUM_METAR_WORKERS, default=4"` // Number of METAR fetch workers to run

View File

@@ -4,7 +4,6 @@ import (
"context"
"github.com/renorris/openfsd/fsd"
"log/slog"
_ "modernc.org/sqlite"
"os"
"os/signal"
)
@@ -13,11 +12,6 @@ func main() {
setSlogLevel()
ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt)
os.Setenv("DATABASE_AUTO_MIGRATE", "true")
os.Setenv("DATABASE_DRIVER", "sqlite")
os.Setenv("DATABASE_SOURCE_NAME", "test.db")
server, err := fsd.NewDefaultServer(ctx)
if err != nil {
panic(err)
@@ -26,7 +20,7 @@ func main() {
if err = server.Run(ctx); err != nil {
slog.Error(err.Error())
}
slog.Info("server closed")
slog.Info("FSD server closed")
}
func setSlogLevel() {

View File

@@ -8,10 +8,9 @@ import (
type ServerConfig struct {
ListenAddr string `env:"LISTEN_ADDR, default=:8000"` // HTTP listen address
DatabaseDriver string `env:"DATABASE_DRIVER, default=sqlite"` // Golang sql database driver name
DatabaseSourceName string `env:"DATABASE_SOURCE_NAME, default=:memory:"` // Golang sql database source name
DatabaseAutoMigrate bool `env:"DATABASE_AUTO_MIGRATE, default=false"` // Whether to automatically run database migrations on startup
DatabaseMaxConns int `env:"DATABASE_MAX_CONNS, default=1"` // Max number of database connections
DatabaseDriver string `env:"DATABASE_DRIVER, default=sqlite"` // Golang sql database driver name
DatabaseSourceName string `env:"DATABASE_SOURCE_NAME, default=:memory:"` // Golang sql database source name
DatabaseMaxConns int `env:"DATABASE_MAX_CONNS, default=1"` // Max number of database connections
FsdHttpServiceAddress string `env:"FSD_HTTP_SERVICE_ADDRESS, required"` // HTTP address to talk to the FSD http service
}

View File

@@ -9,10 +9,6 @@ import (
func main() {
ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt)
os.Setenv("DATABASE_DRIVER", "sqlite")
os.Setenv("DATABASE_SOURCE_NAME", "../test.db")
os.Setenv("FSD_HTTP_SERVICE_ADDRESS", "http://localhost:13618")
server, err := NewDefaultServer(ctx)
if err != nil {
panic(err)

View File

@@ -1,11 +1,17 @@
package main
import (
"embed"
"github.com/gin-gonic/gin"
"io/fs"
"log"
"net/http"
"os"
)
//go:embed static/*
var staticFS embed.FS
func (s *Server) setupRoutes() (e *gin.Engine) {
e = gin.New()
e.Use(gin.Recovery())
@@ -29,7 +35,11 @@ func (s *Server) setupRoutes() (e *gin.Engine) {
s.setupFrontendRoutes(e.Group(""))
// Serve static files
e.Static("/static", "./static")
subFS, err := fs.Sub(staticFS, "static")
if err != nil {
log.Fatal(err)
}
e.StaticFS("/static", http.FS(subFS))
return
}