Skip to content
This repository has been archived by the owner on Oct 28, 2023. It is now read-only.

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Kcrong committed Oct 8, 2023
1 parent d500e68 commit effb16a
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 64 deletions.
4 changes: 2 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
Expand Down
27 changes: 12 additions & 15 deletions config.env.example
Original file line number Diff line number Diff line change
@@ -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
26 changes: 0 additions & 26 deletions config.yml.example

This file was deleted.

7 changes: 2 additions & 5 deletions db/templates/singleton/psql.go.tpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import (
"database/sql"
"fmt"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
Expand All @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
13 changes: 4 additions & 9 deletions pkg/config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
9 changes: 3 additions & 6 deletions pkg/models/psql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit effb16a

Please sign in to comment.