diff --git a/cmd/main.go b/cmd/main.go index 89892ba..78050d5 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -33,7 +33,7 @@ func main() { redisOpt := &redis.Options{ Addr: fmt.Sprintf("%s:%d", c.Redis.Host, c.Redis.Port), Password: c.Redis.Password, // no password set - DB: c.Redis.DB, // use default DB + DB: c.Redis.DB, // use default Database MaxRetries: 5, MinRetryBackoff: 3, MaxRetryBackoff: 7, @@ -49,7 +49,7 @@ func main() { redisCli := redis.NewClient(redisOpt) redisCli.AddHook(nrredis.NewHook(redisOpt)) - db, err := models.NewDB(c.DB.Host, c.DB.User, c.DB.Password, c.DB.Name, c.DB.Port, c.DB.SslMode) + db, err := models.NewDB(c.Database.URL) if err != nil { log.Fatalf("%+v", err) } diff --git a/config.env.example b/config.env.example index 0e35449..5e6abb5 100644 --- a/config.env.example +++ b/config.env.example @@ -1,20 +1,17 @@ -APP_NAME="captive-portal" -APP_ENV="dev" +APP_ENV=dev +APP_GRACEFUL_TIMEOUT_SECOND=1 APP_HOST=127.0.0.1 +APP_LOG_LEVEL=debug +APP_NAME=captive-portal APP_PORT=8080 -APP_GRACEFUL_TIMEOUT_SECOND=1 -APP_LOG_LEVEL="debug" -OPENWRT_ENCRYPTION_KEY="YOUR_ENCRYPTION_KEY_IN_OPENNDS" +DATABASE_URL=postgres://postgres:example@localhost:5432/captive-portal?sslmode=disable -REDIS_HOST=localhost -REDIS_PORT=6379 -REDIS_DB=0 -REDIS_PASSWORD="password" +NEWRELIC_LICENSE_KEY= -DB_HOST=localhost -DB_PORT=5432 -DB_USER="username" -DB_NAME="db name" -DB_PASSWORD="password" -DB_SSL_MODE="require" +OPENWRT_ENCRYPTION_KEY= + +REDIS_DB=0 +REDIS_HOST=127.0.0.1 +REDIS_PASSWORD= +REDIS_PORT=6370 \ No newline at end of file diff --git a/config.yml.example b/config.yml.example deleted file mode 100644 index 2e11923..0000000 --- a/config.yml.example +++ /dev/null @@ -1,26 +0,0 @@ -app: - name: "captive-portal" - env: "dev" - host: 127.0.0.1 - port: 8080 - graceful_timeout_second: 0.1 - log_level: "debug" - -newrelic: - license_key: "example" - -openwrt: - encryption_key: "example" - -redis: - host: 127.0.0.1 - port: 6379 - db: 0 - password: "" - -db: - host: 127.0.0.1 - port: 5432 - user: "postgres" - name: "captive-portal" - password: "example" diff --git a/db/templates/singleton/psql.go.tpl b/db/templates/singleton/psql.go.tpl index be2a540..ad7faff 100644 --- a/db/templates/singleton/psql.go.tpl +++ b/db/templates/singleton/psql.go.tpl @@ -1,6 +1,5 @@ import ( "database/sql" - "fmt" "github.com/pkg/errors" log "github.com/sirupsen/logrus" @@ -13,10 +12,8 @@ const ( accountBulkInsertCount = 3000 ) -func NewDB(host, user, pass, dbName string, port int, sslmode string) (*sql.DB, error) { - db, err := sql.Open("postgres", fmt.Sprintf( - "user=%s password=%s host=%s port=%d dbname=%s sslmode=%s", user, pass, host, port, dbName, sslmode, - )) +func NewDB(url string) (*sql.DB, error) { + db, err := sql.Open("postgres", url) if err != nil { return nil, errors.WithStack(err) } diff --git a/go.mod b/go.mod index 0163da9..a19f0a1 100644 --- a/go.mod +++ b/go.mod @@ -7,9 +7,9 @@ require ( github.com/go-redis/redismock/v9 v9.0.3 github.com/labstack/echo/v4 v4.11.1 github.com/labstack/gommon v0.4.0 - github.com/lib/pq v1.10.6 github.com/newrelic/go-agent/v3 v3.26.0 github.com/newrelic/go-agent/v3/integrations/nrecho-v4 v1.0.4 + github.com/newrelic/go-agent/v3/integrations/nrpq v1.1.1 github.com/newrelic/go-agent/v3/integrations/nrredis-v9 v1.0.0 github.com/pkg/errors v0.9.1 github.com/redis/go-redis/v9 v9.1.0 @@ -31,6 +31,7 @@ require ( github.com/golang-jwt/jwt v3.2.2+incompatible // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/klauspost/compress v1.16.3 // indirect + github.com/lib/pq v1.10.6 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.19 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect diff --git a/go.sum b/go.sum index 096536c..49f1b19 100644 --- a/go.sum +++ b/go.sum @@ -326,6 +326,7 @@ github.com/labstack/echo/v4 v4.11.1/go.mod h1:YuYRTSM3CHs2ybfrL8Px48bO6BAnYIN4l8 github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8= github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM= github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.10.6 h1:jbk+ZieJ0D7EVGJYpL9QTz7/YW6UHbmdnZWYyK5cdBs= github.com/lib/pq v1.10.6/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= @@ -371,10 +372,13 @@ github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3P github.com/montanaflynn/stats v0.6.6/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/newrelic/go-agent/v3 v3.3.0/go.mod h1:H28zDNUC0U/b7kLoY4EFOhuth10Xu/9dchozUiOseQQ= github.com/newrelic/go-agent/v3 v3.26.0 h1:xJkqiQgLtC3ys5zoBxD91ITm7sVHZNEF+7/mqmFjnl0= github.com/newrelic/go-agent/v3 v3.26.0/go.mod h1:sE2WdlLF3B/xI5HUuIHTa7Aht1gpcIY65pzaUoN1pJs= github.com/newrelic/go-agent/v3/integrations/nrecho-v4 v1.0.4 h1:c9kq15TraxDAZr0bHDmBq5VSRZYPGwH6IyksshILxnY= github.com/newrelic/go-agent/v3/integrations/nrecho-v4 v1.0.4/go.mod h1:UdtAjlKMd7hzPxylHMXzCvv3Ryx6+Du1DWZez01iK+M= +github.com/newrelic/go-agent/v3/integrations/nrpq v1.1.1 h1:HlVcLXw7ZZPjeRx3lQUAN8qfpJVDmuq4L237M1+PS8A= +github.com/newrelic/go-agent/v3/integrations/nrpq v1.1.1/go.mod h1:UvI7Z0Dok/36E44UiTysh9HQZudDdpiChbe3+eqSB0I= github.com/newrelic/go-agent/v3/integrations/nrredis-v9 v1.0.0 h1:jPjdL6i69gAXeOHJH/GCmLXxrzMl0J19wE5H1U3FdGE= github.com/newrelic/go-agent/v3/integrations/nrredis-v9 v1.0.0/go.mod h1:oRTLRKiOcFsOJZgioSMRUQ54vUMomoQkoQW8wql4B4s= github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= diff --git a/pkg/config/env.go b/pkg/config/env.go index 72d77ef..ea0f26d 100644 --- a/pkg/config/env.go +++ b/pkg/config/env.go @@ -46,16 +46,11 @@ type Redis struct { Host string `env:"HOST"` Port int `env:"PORT"` Password string `env:"PASSWORD"` - DB int `env:"DB"` + DB int `env:"Database"` } -type DB struct { - Host string `env:"HOST"` - Port int `env:"PORT"` - User string `env:"USER"` - Password string `env:"PASSWORD"` - Name string `env:"NAME"` - SslMode string `env:"SSL_MODE"` +type Database struct { + URL string `env:"URL"` } type App struct { @@ -72,7 +67,7 @@ type Config struct { Newrelic `envPrefix:"NEWRELIC_"` Openwrt `envPrefix:"OPENWRT_"` Redis `envPrefix:"REDIS_"` - DB `envPrefix:"DB_"` + Database `envPrefix:"DATABASE_"` } // FromEnv parses env to Config diff --git a/pkg/models/psql.go b/pkg/models/psql.go index c6ad9d1..e0c8877 100644 --- a/pkg/models/psql.go +++ b/pkg/models/psql.go @@ -5,12 +5,11 @@ package models import ( "database/sql" - "fmt" "github.com/pkg/errors" log "github.com/sirupsen/logrus" - _ "github.com/lib/pq" + _ "github.com/newrelic/go-agent/v3/integrations/nrpq" ) const ( @@ -18,10 +17,8 @@ const ( accountBulkInsertCount = 3000 ) -func NewDB(host, user, pass, dbName string, port int, sslmode string) (*sql.DB, error) { - db, err := sql.Open("postgres", fmt.Sprintf( - "user=%s password=%s host=%s port=%d dbname=%s sslmode=%s", user, pass, host, port, dbName, sslmode, - )) +func NewDB(url string) (*sql.DB, error) { + db, err := sql.Open("postgres", url) if err != nil { return nil, errors.WithStack(err) }