Switched to redis/postgres, major refactoring

- loginMetadata is passed to shards through redis now
- shards announce they're alive via redis.AnnounceShard() which just
  populates a hashset keyed 'shards'
- login servers grab the 'shards' hashset and randomly picks a shard to
  pass the player to (for now)
- ./service shard && ./service login
- Many new environment variables, check config/config.go for more info.
  or for a tl;dr just read the Dockerfile for the required ones
- Shard and login services now run in different processes ! (and
  containers?? wooaaah)
This commit is contained in:
2023-06-22 01:53:38 -05:00
parent 983588b6c9
commit d7445e0f0f
20 changed files with 489 additions and 192 deletions

42
cmd/login.go Normal file
View File

@@ -0,0 +1,42 @@
package main
import (
"context"
"flag"
"log"
"github.com/CPunch/gopenfusion/config"
"github.com/CPunch/gopenfusion/login"
"github.com/google/subcommands"
)
type loginCommand struct {
port int
}
func (s *loginCommand) Name() string {
return "login"
}
func (s *loginCommand) Synopsis() string {
return "Starts login service"
}
func (s *loginCommand) Usage() string {
return s.Name() + " - " + s.Synopsis() + ":\n"
}
func (s *loginCommand) SetFlags(f *flag.FlagSet) {
f.IntVar(&s.port, "port", config.LOGIN_PORT, "Hosts the service on this port")
}
func (s *loginCommand) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
loginServer, err := login.NewLoginServer(dbHndlr, redisHndlr, s.port)
if err != nil {
log.Panicf("failed to create shard server: %v", err)
}
loginServer.Start()
return subcommands.ExitSuccess
}

43
cmd/main.go Normal file
View File

@@ -0,0 +1,43 @@
package main
import (
"context"
"flag"
"log"
"os"
"github.com/CPunch/gopenfusion/config"
"github.com/CPunch/gopenfusion/core/db"
"github.com/CPunch/gopenfusion/core/redis"
"github.com/google/subcommands"
)
var dbHndlr *db.DBHandler
var redisHndlr *redis.RedisHandler
func main() {
subcommands.Register(subcommands.HelpCommand(), "")
subcommands.Register(subcommands.FlagsCommand(), "")
subcommands.Register(subcommands.CommandsCommand(), "")
subcommands.Register(&shardCommand{}, "")
subcommands.Register(&loginCommand{}, "")
var err error
dbHndlr, err = db.OpenPostgresDB(config.GetDBAddr())
if err != nil {
log.Panicf("failed to open db: %v", err)
}
if err = dbHndlr.Setup(); err != nil {
log.Panicf("failed to setup db: %v", err)
}
redisHndlr, err = redis.OpenRedis(config.GetRedisAddr())
if err != nil {
log.Panicf("failed to open redis: %v", err)
}
flag.Parse()
os.Exit(int(subcommands.Execute(context.Background())))
}

42
cmd/shard.go Normal file
View File

@@ -0,0 +1,42 @@
package main
import (
"context"
"flag"
"log"
"github.com/CPunch/gopenfusion/config"
"github.com/CPunch/gopenfusion/shard"
"github.com/google/subcommands"
)
type shardCommand struct {
port int
}
func (s *shardCommand) Name() string {
return "shard"
}
func (s *shardCommand) Synopsis() string {
return "Starts shard service"
}
func (s *shardCommand) Usage() string {
return s.Name() + " - " + s.Synopsis() + ":\n"
}
func (s *shardCommand) SetFlags(f *flag.FlagSet) {
f.IntVar(&s.port, "port", config.SHARD_PORT, "Hosts the service on this port")
}
func (s *shardCommand) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
shardServer, err := shard.NewShardServer(dbHndlr, redisHndlr, s.port)
if err != nil {
log.Panicf("failed to create shard server: %v", err)
}
shardServer.Start()
return subcommands.ExitSuccess
}