You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.7 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "net"
  7. "git.chrishayward.xyz/x/users/proto"
  8. "git.chrishayward.xyz/x/users/server"
  9. "github.com/google/uuid"
  10. "google.golang.org/grpc"
  11. "gorm.io/driver/postgres"
  12. "gorm.io/driver/sqlite"
  13. "gorm.io/gorm"
  14. )
  15. var (
  16. port = flag.Uint("port", 8080, "--port=8080")
  17. secretDefault = uuid.NewString()
  18. secret = flag.String("secret", secretDefault, "--secret=SECRET")
  19. dbType = flag.String("dbType", "sqlite", "--dbType=sqlite,postgress")
  20. dbFile = flag.String("dbFile", "users.db", "--dbFile=users.db")
  21. dbHost = flag.String("dbHost", "localhost", "--dbHost=localhost")
  22. dbPort = flag.Uint("dbPort", 5432, "--dbPort=5432")
  23. dbName = flag.String("dbName", "postgres", "--dbName=postgres")
  24. dbUser = flag.String("dbUser", "postgres", "--dbUser=postgres")
  25. dbPass = flag.String("dbPass", "postgres", "--dbPass=postgres")
  26. )
  27. func main() {
  28. // Parse the optional flags.
  29. flag.Parse()
  30. // If the secret has not been set print it to the console.
  31. if *secret == secretDefault {
  32. log.Printf("SECRET=%s\n", secretDefault)
  33. }
  34. // Initialize the database.
  35. var db *gorm.DB
  36. var err error
  37. switch *dbType {
  38. case "postgres":
  39. db, _ = gorm.Open(postgres.Open(fmt.Sprintf(
  40. "host=%s user=%s password=%s dbname=%s port=%d sslmode=disable",
  41. *dbHost, *dbUser, *dbPass, *dbName, *dbPort)))
  42. case "sqlite":
  43. default:
  44. db, _ = gorm.Open(sqlite.Open(*dbFile), &gorm.Config{})
  45. }
  46. // Create the network listener.
  47. lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
  48. if err != nil {
  49. log.Fatal(err)
  50. }
  51. // Start listening for requests.
  52. srv := grpc.NewServer()
  53. proto.RegisterUsersServer(srv, server.NewUsersServer(secret, db))
  54. log.Printf("Listening on :%d", *port)
  55. srv.Serve(lis)
  56. }