|
@ -5,60 +5,73 @@ import ( |
|
|
"fmt" |
|
|
"fmt" |
|
|
"log" |
|
|
"log" |
|
|
"net" |
|
|
"net" |
|
|
|
|
|
"os" |
|
|
|
|
|
|
|
|
"git.chrishayward.xyz/x/users/proto" |
|
|
"git.chrishayward.xyz/x/users/proto" |
|
|
"git.chrishayward.xyz/x/users/server" |
|
|
"git.chrishayward.xyz/x/users/server" |
|
|
"github.com/google/uuid" |
|
|
|
|
|
"google.golang.org/grpc" |
|
|
"google.golang.org/grpc" |
|
|
|
|
|
"gopkg.in/yaml.v3" |
|
|
"gorm.io/driver/postgres" |
|
|
"gorm.io/driver/postgres" |
|
|
"gorm.io/driver/sqlite" |
|
|
"gorm.io/driver/sqlite" |
|
|
"gorm.io/gorm" |
|
|
"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") |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
var configPath = flag.String("config", "./config.yaml", "--config=config.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() { |
|
|
func main() { |
|
|
// Parse the optional flags.
|
|
|
// Parse the optional flags.
|
|
|
flag.Parse() |
|
|
flag.Parse() |
|
|
|
|
|
|
|
|
// If the secret has not been set print it to the console.
|
|
|
|
|
|
if *secret == secretDefault { |
|
|
|
|
|
log.Printf("SECRET=%s\n", secretDefault) |
|
|
|
|
|
|
|
|
// 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.
|
|
|
// Initialize the database.
|
|
|
var db *gorm.DB |
|
|
var db *gorm.DB |
|
|
var err error |
|
|
|
|
|
switch *dbType { |
|
|
|
|
|
|
|
|
switch config.Database.Type { |
|
|
case "postgres": |
|
|
case "postgres": |
|
|
db, _ = gorm.Open(postgres.Open(fmt.Sprintf( |
|
|
db, _ = gorm.Open(postgres.Open(fmt.Sprintf( |
|
|
"host=%s user=%s password=%s dbname=%s port=%d sslmode=disable", |
|
|
"host=%s user=%s password=%s dbname=%s port=%d sslmode=disable", |
|
|
*dbHost, *dbUser, *dbPass, *dbName, *dbPort))) |
|
|
|
|
|
|
|
|
config.Database.Host, |
|
|
|
|
|
config.Database.User, |
|
|
|
|
|
config.Database.Password, |
|
|
|
|
|
config.Database.Name, |
|
|
|
|
|
config.Database.Port))) |
|
|
case "sqlite": |
|
|
case "sqlite": |
|
|
default: |
|
|
default: |
|
|
db, _ = gorm.Open(sqlite.Open(*dbFile), &gorm.Config{}) |
|
|
|
|
|
|
|
|
db, _ = gorm.Open(sqlite.Open(config.Database.File), &gorm.Config{}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Create the network listener.
|
|
|
// Create the network listener.
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port)) |
|
|
|
|
|
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", config.Port)) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Fatal(err) |
|
|
log.Fatal(err) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Start listening for requests.
|
|
|
// Start listening for requests.
|
|
|
srv := grpc.NewServer() |
|
|
srv := grpc.NewServer() |
|
|
proto.RegisterUsersServer(srv, server.NewUsersServer(secret, db)) |
|
|
|
|
|
log.Printf("Listening on :%d", *port) |
|
|
|
|
|
|
|
|
proto.RegisterUsersServer(srv, server.NewUsersServer(&config.Secret, db)) |
|
|
|
|
|
log.Printf("Listening on :%d", config.Port) |
|
|
srv.Serve(lis) |
|
|
srv.Serve(lis) |
|
|
} |
|
|
} |