diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9ffd2bb..3517084 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -16,7 +16,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v3 with: - go-version: '1.20' + go-version: '1.21' - name: Build run: go build -v ./... diff --git a/Dockerfile b/Dockerfile index cbdf758..bd5bbc7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM docker.io/golang:1.20-alpine AS backend_builder +FROM docker.io/golang:1.21-alpine AS backend_builder WORKDIR /app RUN apk add git COPY go.mod go.sum ./ diff --git a/cmd/panorama/main.go b/cmd/panorama/main.go index a119878..45dc8da 100644 --- a/cmd/panorama/main.go +++ b/cmd/panorama/main.go @@ -2,7 +2,8 @@ package main import ( "flag" - "log" + "log/slog" + "os" "path" "github.com/lord-server/panorama/internal/config" @@ -15,71 +16,84 @@ import ( ) type Args struct { - FullRender bool - Downscale bool - Serve bool ConfigPath string } var args Args -func init() { - flag.BoolVar(&args.FullRender, "fullrender", false, "Render entire map") - flag.BoolVar(&args.Downscale, "downscale", false, "Downscale existing tiles (--fullrender does this automatically)") - flag.BoolVar(&args.Serve, "serve", false, "Serve tiles over the web") - flag.StringVar(&args.ConfigPath, "config", "config.toml", "Path to config file") - flag.Parse() -} - -func main() { - log.Printf("Config path: `%v`", args.ConfigPath) - config, err := config.LoadConfig(args.ConfigPath) - if err != nil { - log.Fatalf("Unable to load config: %v\n", err) - } - - quit := make(chan bool) - - if args.Serve { - log.Printf("Serving tiles @ %v", config.Web.ListenAddress) - go func() { - web.Serve(&config) - quit <- true - }() - } - - log.Printf("Game path: `%v`\n", config.System.GamePath) - +func fullrender(config config.Config) error { descPath := path.Join(config.System.WorldPath, "nodes_dump.json") - log.Printf("Game description: `%v`\n", descPath) + + slog.Info("loading game description", "game", config.System.GamePath, "desc", descPath) game, err := game.LoadGame(descPath, config.System.GamePath) if err != nil { - log.Fatalf("Unable to load game description: %v\n", err) + slog.Error("unable to load game description", "error", err) + return err } backend, err := world.NewPostgresBackend(config.System.WorldDSN) if err != nil { - log.Fatalf("Unable to connect to world DB: %v\n", err) + slog.Error("unable to connect to world DB", "error", err) + return err } world := world.NewWorldWithBackend(backend) tiler := tile.NewTiler(config.Region, config.Renderer.ZoomLevels, config.System.TilesPath) - if args.FullRender { - log.Printf("Performing a full render using %v workers", config.Renderer.Workers) + slog.Info("performing a full render", "workers", config.Renderer.Workers, "region", config.Region) + + tiler.FullRender(&game, &world, config.Renderer.Workers, config.Region, func() render.Renderer { + return isometric.NewRenderer(config.Region, &game) + }) + + tiler.DownscaleTiles() + + return nil +} + +func run(config config.Config) error { + quit := make(chan bool) - log.Printf("Region: %v", config.Region) + slog.Info("starting web server", "address", config.Web.ListenAddress) + go func() { + web.Serve(&config) + quit <- true + }() - tiler.FullRender(&game, &world, config.Renderer.Workers, config.Region, func() render.Renderer { - return isometric.NewRenderer(config.Region, &game) - }) + <-quit + + return nil +} + +func main() { + if len(os.Args) < 2 { + slog.Error("expected a subcommand: (available subcommands: run, fullrender)") + os.Exit(1) } - if args.Downscale || args.FullRender { - tiler.DownscaleTiles() + subcommand := os.Args[1] + + commonFlags := flag.NewFlagSet("common flags", flag.ExitOnError) + commonFlags.StringVar(&args.ConfigPath, "config", "config.toml", "Path to config file") + commonFlags.Parse(os.Args[2:]) + + slog.Info("loading config", "config_path", args.ConfigPath) + config, err := config.LoadConfig(args.ConfigPath) + if err != nil { + slog.Error("unable to load config", "error", err) } - <-quit + switch subcommand { + case "run": + err = run(config) + + case "fullrender": + err = fullrender(config) + } + + if err != nil { + os.Exit(1) + } } diff --git a/go.mod b/go.mod index 7051a68..b10f710 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/lord-server/panorama -go 1.20 +go 1.21 require ( github.com/BurntSushi/toml v1.2.1 diff --git a/go.sum b/go.sum index 327c7f2..15ba6b2 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,7 @@ github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/ github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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/gofiber/fiber/v2 v2.42.0 h1:Fnp7ybWvS+sjNQsFvkhf4G8OhXswvB6Vee8hM/LyS+8= github.com/gofiber/fiber/v2 v2.42.0/go.mod h1:3+SGNjqMh5VQH5Vz2Wdi43zTIV16ktlFd3x3R6O1Zlc= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= @@ -48,6 +49,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/tinylib/msgp v1.1.6/go.mod h1:75BAfg2hauQhs3qedfdDZmWAPcFMAvJE5b9rGOMufyw= github.com/tinylib/msgp v1.1.8 h1:FCXC1xanKO4I8plpHGH2P7koL/RzZs12l/+r7vakfm0= github.com/tinylib/msgp v1.1.8/go.mod h1:qkpG+2ldGg4xRFmx+jfTvZPxfGFhi64BcnL9vkCm/Tw= @@ -116,3 +118,4 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8T gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=