-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
79 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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://") */ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters