Initial commit.
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
// Command server runs the Super Auto Pets board game server: game API,
|
||||
// WebSocket sync, and the built web frontend.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/greyson/super-auto-pets-board-game/internal/env"
|
||||
"github.com/greyson/super-auto-pets-board-game/internal/server"
|
||||
"github.com/greyson/super-auto-pets-board-game/internal/store"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := env.Load(".env"); err != nil {
|
||||
slog.Error("failed to read .env", "err", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
dataDir := env.Get("DATA_DIR", "data")
|
||||
port := env.Get("PORT", "8080")
|
||||
staticDir := env.Get("STATIC_DIR", "web/dist")
|
||||
|
||||
st, err := store.Open(dataDir)
|
||||
if err != nil {
|
||||
slog.Error("failed to open store", "err", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer st.Close()
|
||||
|
||||
if _, err := os.Stat(staticDir); err != nil {
|
||||
slog.Warn("static dir missing; only the API will be served", "dir", staticDir)
|
||||
staticDir = ""
|
||||
}
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: ":" + port,
|
||||
Handler: server.New(st, staticDir).Handler(),
|
||||
}
|
||||
|
||||
go func() {
|
||||
slog.Info("listening", "addr", "http://localhost:"+port, "dataDir", dataDir)
|
||||
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
slog.Error("server failed", "err", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}()
|
||||
|
||||
stop := make(chan os.Signal, 1)
|
||||
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
|
||||
<-stop
|
||||
slog.Info("shutting down")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
srv.Shutdown(ctx)
|
||||
}
|
||||
Reference in New Issue
Block a user