From 9c21c44f3b259a12f507074f88518ec333b994b5 Mon Sep 17 00:00:00 2001 From: Christopher James Hayward Date: Mon, 17 Jul 2023 09:20:06 -0400 Subject: [PATCH] Switch to YAML config --- cmd/gateway/config.yaml | 7 +++++ cmd/gateway/main.go | 43 +++++++++++++++++++----------- cmd/server/config.yaml | 10 +++++++ cmd/server/main.go | 59 +++++++++++++++++++++++++---------------- go.mod | 3 +++ go.sum | 8 ++++++ 6 files changed, 91 insertions(+), 39 deletions(-) create mode 100644 cmd/gateway/config.yaml create mode 100644 cmd/server/config.yaml diff --git a/cmd/gateway/config.yaml b/cmd/gateway/config.yaml new file mode 100644 index 0000000..f3b8179 --- /dev/null +++ b/cmd/gateway/config.yaml @@ -0,0 +1,7 @@ +port: 8081 +domain: http://localhost +server: + address: localhost + port: 8080 + secret: secret + \ No newline at end of file diff --git a/cmd/gateway/main.go b/cmd/gateway/main.go index e936c03..d104db9 100644 --- a/cmd/gateway/main.go +++ b/cmd/gateway/main.go @@ -5,32 +5,43 @@ import ( "fmt" "log" "net/http" + "os" "git.chrishayward.xyz/x/users/gateway" "git.chrishayward.xyz/x/users/proto" "google.golang.org/grpc" + "gopkg.in/yaml.v3" ) -var ( - port = flag.Int("port", 8081, "--port=8081") - domain = flag.String("domain", "http://localhost", "--domain=localhost") - serverAddr = flag.String("serverAddr", "localhost", "--serverAddr=localhost") - serverPort = flag.Int("serverPort", 8080, "--serverPort=8080") - serverSecret = flag.String("serverSecret", "...", "--serverSecret=...") -) - -func authorized(f http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { +var configPath = flag.String("config", "./config.yaml", "--config=config.yaml") - } +type config struct { + Port int `yaml:"port"` + Domain string `yaml:"domain"` + Server struct { + Address string `yaml:"address"` + Port int `yaml:"port"` + Secret string `yaml:"secret"` + } `yaml:"server"` } func main() { // Parse the optional flags. flag.Parse() + // 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) + } + // Create the connection to the server. - conn, err := grpc.Dial(fmt.Sprintf("%s:%d", *serverAddr, *serverPort)) + conn, err := grpc.Dial(fmt.Sprintf("%s:%d", config.Server.Address, config.Server.Port)) if err != nil { log.Fatal(err) } @@ -44,13 +55,13 @@ func main() { // Setup HTTP endpoints. http.HandleFunc("/register", gateway.Register(client)) http.HandleFunc("/login", gateway.Login(client)) - http.HandleFunc("/logout", gateway.Authorize(client, serverSecret, gateway.Logout(client))) - http.HandleFunc("/reset_password", gateway.ResetPassword(client, fmt.Sprintf("%s, %d", *domain, *port))) + http.HandleFunc("/logout", gateway.Authorize(client, &config.Server.Secret, gateway.Logout(client))) + http.HandleFunc("/reset_password", gateway.ResetPassword(client, fmt.Sprintf("%s, %d", config.Domain, config.Port))) http.HandleFunc("/change_password", gateway.ChangePassword(client)) // Listen for requests. - log.Printf("Forwarding from :%d to %s:%d", *port, *serverAddr, *serverPort) - if err := http.ListenAndServe(fmt.Sprintf(":%d", *port), nil); err != nil { + log.Printf("Forwarding from :%d to %s:%d", config.Port, config.Server.Address, config.Server.Port) + if err := http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil); err != nil { log.Fatal(err) } } diff --git a/cmd/server/config.yaml b/cmd/server/config.yaml new file mode 100644 index 0000000..2849c26 --- /dev/null +++ b/cmd/server/config.yaml @@ -0,0 +1,10 @@ +port: 8080 +secret: secret +database: + type: sqlite + file: users.db + host: localhost + port: 5432 + name: postgres + user: postgres + password: postgres \ No newline at end of file diff --git a/cmd/server/main.go b/cmd/server/main.go index f66beda..0cc0032 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -5,60 +5,73 @@ import ( "fmt" "log" "net" + "os" "git.chrishayward.xyz/x/users/proto" "git.chrishayward.xyz/x/users/server" - "github.com/google/uuid" "google.golang.org/grpc" + "gopkg.in/yaml.v3" "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") -) +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() { // 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) + // 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. var db *gorm.DB - var err error - switch *dbType { + switch config.Database.Type { 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))) + config.Database.Host, + config.Database.User, + config.Database.Password, + config.Database.Name, + config.Database.Port))) case "sqlite": default: - db, _ = gorm.Open(sqlite.Open(*dbFile), &gorm.Config{}) + db, _ = gorm.Open(sqlite.Open(config.Database.File), &gorm.Config{}) } // 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 { log.Fatal(err) } // Start listening for requests. 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) } diff --git a/go.mod b/go.mod index 2104887..41b37f2 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( golang.org/x/crypto v0.10.0 google.golang.org/grpc v1.56.1 google.golang.org/protobuf v1.31.0 + gopkg.in/yaml.v3 v3.0.1 gorm.io/driver/postgres v1.5.2 gorm.io/driver/sqlite v1.5.2 gorm.io/gorm v1.25.2 @@ -19,7 +20,9 @@ require ( github.com/jackc/pgx/v5 v5.3.1 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect + github.com/kr/text v0.2.0 // indirect github.com/mattn/go-sqlite3 v1.14.17 // indirect + github.com/rogpeppe/go-internal v1.11.0 // indirect golang.org/x/net v0.10.0 // indirect golang.org/x/sys v0.9.0 // indirect golang.org/x/text v0.10.0 // indirect diff --git a/go.sum b/go.sum index 807e326..8bcdcc9 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,4 @@ +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= @@ -17,10 +18,15 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM= github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= @@ -43,8 +49,10 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gorm.io/driver/postgres v1.5.2 h1:ytTDxxEv+MplXOfFe3Lzm7SjG09fcdb3Z/c056DTBx0= gorm.io/driver/postgres v1.5.2/go.mod h1:fmpX0m2I1PKuR7mKZiEluwrP3hbs+ps7JIGMUBpCgl8= gorm.io/driver/sqlite v1.5.2 h1:TpQ+/dqCY4uCigCFyrfnrJnrW9zjpelWVoEVNy5qJkc=