package main import ( "flag" "fmt" "log" "net" "git.chrishayward.xyz/x/users/proto" "git.chrishayward.xyz/x/users/server" "github.com/google/uuid" "google.golang.org/grpc" "gorm.io/driver/postgres" "gorm.io/driver/sqlite" "gorm.io/gorm" ) var ( port = flag.Uint("port", 8080, "--port=8080") secretDefault = uuid.NewString() secret = flag.String("secret", secretDefault, "--secret=SECRET") dbType = flag.String("dbType", "sqlite", "--dbType=sqlite,postgress") dbFile = flag.String("dbFile", "users.db", "--dbFile=users.db") dbHost = flag.String("dbHost", "localhost", "--dbHost=localhost") dbPort = flag.Uint("dbPort", 5432, "--dbPort=5432") dbName = flag.String("dbName", "postgres", "--dbName=postgres") dbUser = flag.String("dbUser", "postgres", "--dbUser=postgres") dbPass = flag.String("dbPass", "postgres", "--dbPass=postgres") ) func main() { // Parse the optional flags. flag.Parse() // If the secret has not been set print it to the console. if *secret == secretDefault { log.Printf("SECRET=%s\n", secretDefault) } // Initialize the database. var db *gorm.DB var err error switch *dbType { case "postgres": db, _ = gorm.Open(postgres.Open(fmt.Sprintf( "host=%s user=%s password=%s dbname=%s port=%d sslmode=disable", *dbHost, *dbUser, *dbPass, *dbName, *dbPort))) case "sqlite": default: db, _ = gorm.Open(sqlite.Open(*dbFile), &gorm.Config{}) } // Create the network listener. lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port)) if err != nil { log.Fatal(err) } // Start listening for requests. srv := grpc.NewServer() proto.RegisterUsersServer(srv, server.NewUsersServer(secret, db)) log.Printf("Listening on :%d", *port) srv.Serve(lis) }