main.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package main
  2. import (
  3. "log"
  4. "net"
  5. "os"
  6. "strings"
  7. gamelangpb "git.alfi.li/gamelang/protobuf/gamelang"
  8. um "git.alfi.li/gamelang/systems/usermanager"
  9. "google.golang.org/grpc"
  10. gcfg "gopkg.in/gcfg.v1"
  11. )
  12. type Config struct {
  13. Grpc struct {
  14. Listen string // ip:port
  15. }
  16. Kafka struct {
  17. Enabled bool
  18. Broker string
  19. }
  20. }
  21. func main() {
  22. var config Config
  23. err := gcfg.ReadFileInto(&config, "config")
  24. if err != nil {
  25. log.Print(err)
  26. log.Print("reading config from env")
  27. config.Grpc.Listen = os.Getenv("GRPC_LISTEN")
  28. config.Kafka.Enabled = os.Getenv("KAFKA_ENABLED") == "true"
  29. config.Kafka.Broker = os.Getenv("KAFKA_BROKER")
  30. }
  31. log.Print("config read")
  32. log.Printf("%+v", config)
  33. // init usermanager
  34. brokers := []string{}
  35. if config.Kafka.Enabled {
  36. brokers = strings.Split(config.Kafka.Broker, ",")
  37. }
  38. usermanager := um.NewUserManager(brokers)
  39. log.Print("usermanager initialized")
  40. // init GrpcServer
  41. lis, err := net.Listen("tcp", config.Grpc.Listen)
  42. if err != nil {
  43. log.Fatalf("failed to listen: %v", err)
  44. }
  45. grpcServer := grpc.NewServer()
  46. gamelangpb.RegisterUserServiceServer(grpcServer, um.NewUserServer(usermanager))
  47. grpcServer.Serve(lis)
  48. }