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.

63 lines
1.8 KiB

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