Skip to content

Commit

Permalink
fix config parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Chystik committed Oct 26, 2023
1 parent 11b15aa commit 6d2429b
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 26 deletions.
20 changes: 11 additions & 9 deletions cmd/gophermart/flags.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package main

/* import (
import (
"flag"
"strings"

"github.com/Chystik/gophermart/config"
)

func parseFlags(cfg *config.App) {
var addr, accr string
// checking interface implementation
_ = flag.Value(&cfg.Address)
_ = flag.Value(&cfg.DBuri)
_ = flag.Value(&cfg.AccrualAddress)

flag.StringVar(&addr, "a", "", "app address")
flag.StringVar(&cfg.DBuri, "d", "", "database uri")
flag.StringVar(&accr, "r", "", "accal service address")
flag.Var(&cfg.Address, "a", "app address")
flag.Var(&cfg.DBuri, "d", "database uri")
flag.Var(&cfg.AccrualAddress, "r", "accal service address")
flag.Parse()

cfg.Address = strings.TrimPrefix(addr, "http://")
cfg.AccrualAddress = strings.TrimPrefix(accr, "http://")
} */
/* cfg.Address = strings.TrimPrefix(addr, "http://")
cfg.AccrualAddress = strings.TrimPrefix(accr, "http://") */
}
7 changes: 4 additions & 3 deletions cmd/gophermart/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"log"
"os"
"os/signal"
"syscall"
Expand All @@ -12,13 +13,13 @@ import (
func main() {
cfg := config.NewAppConfig()

parseFlags(cfg)
err := parseEnv(cfg)
if err != nil {
panic(err)
log.Fatalln(err)
}
//parseFlags(cfg)

// Graceful shutdown setup
// channel for Graceful shutdown
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)

Expand Down
67 changes: 61 additions & 6 deletions config/app.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,69 @@
package config

type App struct {
Address string `env:"RUN_ADDRESS"`
DBuri string `env:"DATABASE_URI"`
AccrualAddress string `env:"ACCRUAL_SYSTEM_ADDRESS"`
JWTkey []byte
}
import (
"errors"
"strconv"
"strings"
)

type (
App struct {
Address `env:"RUN_ADDRESS"`
DBuri `env:"DATABASE_URI"`
AccrualAddress `env:"ACCRUAL_SYSTEM_ADDRESS"`
JWTkey []byte
}

Address string
DBuri string
AccrualAddress string
)

func NewAppConfig() *App {
return &App{
JWTkey: []byte("my_secret_key"),
}
}

func (addr Address) String() string {
return string(addr)
}

func (addr *Address) Set(s string) error {
hp := strings.Split(s, ":")
if len(hp) != 2 {
return errors.New("expect address in a form host:port")
}
_, err := strconv.Atoi(hp[1])
if err != nil {
return errors.New("only digits allowed for port in a form host:port")
}
*addr = Address(s)
return nil
}

func (db DBuri) String() string {
return string(db)
}

func (db *DBuri) Set(s string) error {
*db = DBuri(s)
return nil
}

func (accr AccrualAddress) String() string {
return string(accr)
}

func (accr *AccrualAddress) Set(s string) error {
hp := strings.Split(s, ":")
if len(hp) != 2 {
return errors.New("expect address in a form host:port")
}
_, err := strconv.Atoi(hp[1])
if err != nil {
return errors.New("only digits allowed for port in a form host:port")
}
*accr = AccrualAddress(s)
return nil
}
5 changes: 0 additions & 5 deletions pkg/httpserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@ import (
"net/http"
)

const (
defaultAddr = ":80"
)

type Server struct {
server *http.Server
}

func NewServer(handler http.Handler, opts ...Options) *Server {
httpServer := &http.Server{
Addr: defaultAddr,
Handler: handler,
}

Expand Down
6 changes: 3 additions & 3 deletions run/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func App(cfg *config.App, quit chan os.Signal) {
}

// Postgres client
pgClient, err := postgres.NewPgClient(cfg.DBuri, logger)
pgClient, err := postgres.NewPgClient(cfg.DBuri.String(), logger)
if err != nil {
logger.Fatal(err.Error())
}
Expand All @@ -67,7 +67,7 @@ func App(cfg *config.App, quit chan os.Signal) {
httpClient := httpclient.NewClient(httpclient.Timeout(20 * time.Second))

// Accrual web API
accrualWebAPI := webapi.NewAccrualWebAPI(httpClient, webapi.Address(cfg.AccrualAddress))
accrualWebAPI := webapi.NewAccrualWebAPI(httpClient, webapi.Address(cfg.AccrualAddress.String()))

// Repository
userRepo := repository.NewUserRepository(pgClient)
Expand All @@ -86,7 +86,7 @@ func App(cfg *config.App, quit chan os.Signal) {
restapihandlers.NewRouter(handler, userInteractor, orderInteractor, cfg.JWTkey, logger)

// HTTP server
server := httpserver.NewServer(handler, httpserver.Address(cfg.Address))
server := httpserver.NewServer(handler, httpserver.Address(cfg.Address.String()))
go func() {
logger.Info(fmt.Sprintf(logHTTPServerStart, cfg.Address))
if err := server.Startup(); !errors.Is(err, http.ErrServerClosed) {
Expand Down

0 comments on commit 6d2429b

Please sign in to comment.