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
63 lines
1.8 KiB
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"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 {
|
|
fmt.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 {
|
|
fmt.Printf("Failed to listen: %v", err)
|
|
}
|
|
|
|
// Start listening for requests.
|
|
srv := grpc.NewServer()
|
|
proto.RegisterUsersServer(srv, server.NewUsersServer(secret, db))
|
|
fmt.Printf("Listening on :%d", *port)
|
|
srv.Serve(lis)
|
|
}
|