Skip to content

Commit

Permalink
Separate web server and fullrender commands, update to Go 1.21
Browse files Browse the repository at this point in the history
  • Loading branch information
weqqr committed Dec 15, 2023
1 parent 7b48eee commit d48540a
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./...
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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 ./
Expand Down
100 changes: 57 additions & 43 deletions cmd/panorama/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package main

import (
"flag"
"log"
"log/slog"
"os"
"path"

"github.com/lord-server/panorama/internal/config"
Expand All @@ -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)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/lord-server/panorama

go 1.20
go 1.21

require (
github.com/BurntSushi/toml v1.2.1
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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=

0 comments on commit d48540a

Please sign in to comment.