Skip to content

Commit

Permalink
add config
Browse files Browse the repository at this point in the history
  • Loading branch information
timurguseynov committed Dec 28, 2023
1 parent 461b0a9 commit 2582d58
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
WALLET_API_REST_HOST=127.0.0.1
WALLET_API_REST_PORT=3000
16 changes: 9 additions & 7 deletions cmd/apid/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@ import (
"sync"
"time"

"github.com/timurguseynov/go-wallet-api/config"
"github.com/timurguseynov/go-wallet-api/internal/db"

"github.com/timurguseynov/go-wallet-api/cmd/apid/handlers"
)

const (
host = "localhost"
port = "3000"
)

// init is called before main. We are using init to customize logging output.
func init() {
log.SetFlags(log.LstdFlags | log.Lmicroseconds | log.Lshortfile)
Expand All @@ -34,6 +30,12 @@ func init() {
func main() {
log.Println("main : Started")

// Read the config from .env file or environment variables
conf, err := config.Read()
if err != nil {
log.Fatal("main : couldn't read config", err)
}

// Register the Master Session for the database.
log.Println("main : Started : Capturing Master DB:")
dbConn, err := db.NewDB()
Expand All @@ -44,7 +46,7 @@ func main() {
}

server := http.Server{
Addr: host + ":" + port,
Addr: conf.REST.Host + ":" + conf.REST.Port,
Handler: handlers.API(dbConn),
}

Expand All @@ -54,7 +56,7 @@ func main() {

// Start the listener.
go func() {
log.Printf("startup : Listening %s", host)
log.Printf("startup : Listening %s:%s", conf.REST.Host, conf.REST.Port)
log.Printf("shutdown : Listener closed : %v", server.ListenAndServe())
wg.Done()
}()
Expand Down
29 changes: 29 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package config

import (
"github.com/joho/godotenv"

"github.com/kelseyhightower/envconfig"
)

type Config struct {
REST struct {
Host string `default:"127.0.0.1" envconfig:"HOST"`
Port string `default:"3000" envconfig:"PORT"`
}
}

func Read() (Config, error) {
var config Config

err := godotenv.Load()
if err != nil {
return config, err
}

if err := envconfig.Process("WALLET_API", &config); err != nil {
return config, err
}

return config, nil
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ require (
github.com/google/uuid v1.5.0
github.com/gorilla/websocket v1.5.1
github.com/hashicorp/go-memdb v1.3.4
github.com/joho/godotenv v1.5.1
github.com/kelseyhightower/envconfig v1.4.0
github.com/pborman/uuid v1.2.1
github.com/pkg/errors v0.8.0
github.com/stretchr/testify v1.2.2
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/b
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/pborman/uuid v1.2.1 h1:+ZZIw58t/ozdjRaXh/3awHfmWRbzYxJoAdNJxe/3pvw=
github.com/pborman/uuid v1.2.1/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
Expand Down

0 comments on commit 2582d58

Please sign in to comment.