package main import ( "flag" "fmt" "log" "net" "os" "git.chrishayward.xyz/x/users/proto" "git.chrishayward.xyz/x/users/server" "google.golang.org/grpc" "gopkg.in/yaml.v3" "gorm.io/driver/postgres" "gorm.io/driver/sqlite" "gorm.io/gorm" ) var configPath = flag.String("config", "./config/server.yaml", "--config=./config/server.yaml") type config struct { Port int `yaml:"port"` Secret string `yaml:"secret"` Database struct { Type string `yaml:"type"` File string `yaml:"file"` Host string `yaml:"host"` Port int `yaml:"port"` Name string `yaml:"name"` User string `yaml:"user"` Password string `yaml:"password"` } `yaml:"database"` } func main() { // Parse the optional flags. flag.Parse() // Read the config file. config := &config{} file, err := os.Open(*configPath) if err != nil { log.Fatal(err) } defer file.Close() if err := yaml.NewDecoder(file).Decode(&config); err != nil { log.Fatal(err) } // Initialize the database. var db *gorm.DB switch config.Database.Type { case "postgres": db, _ = gorm.Open(postgres.Open(fmt.Sprintf( "host=%s user=%s password=%s dbname=%s port=%d sslmode=disable", config.Database.Host, config.Database.User, config.Database.Password, config.Database.Name, config.Database.Port))) case "sqlite": fallthrough default: db, _ = gorm.Open(sqlite.Open(config.Database.File), &gorm.Config{}) } // Create the network listener. lis, err := net.Listen("tcp", fmt.Sprintf(":%d", config.Port)) if err != nil { log.Fatal(err) } // Start listening for requests. srv := grpc.NewServer() proto.RegisterUsersServer(srv, server.NewUsersServer(config.Secret, db)) log.Printf("Listening on :%d", config.Port) srv.Serve(lis) }