refactor: new (simpler) version of the sl library
This commit is contained in:
+14
-31
@@ -6,47 +6,30 @@ import (
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
type Interface interface {
|
||||
sl.Service
|
||||
var Slot = sl.NewSlot[*Config]()
|
||||
|
||||
Mode() string
|
||||
Host() string
|
||||
type Config struct {
|
||||
Mode string
|
||||
Host string
|
||||
}
|
||||
|
||||
type Custom struct {
|
||||
ModeValue string
|
||||
HostValue string
|
||||
}
|
||||
|
||||
func (c Custom) Mode() string { return c.ModeValue }
|
||||
func (c Custom) Host() string { return c.HostValue }
|
||||
|
||||
func (Custom) Initialize(l *sl.ServiceLocator) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type EnvConfig struct {
|
||||
mode string
|
||||
host string
|
||||
}
|
||||
|
||||
func (c EnvConfig) Mode() string { return c.mode }
|
||||
func (c EnvConfig) Host() string { return c.host }
|
||||
|
||||
func (c *EnvConfig) Initialize(l *sl.ServiceLocator) error {
|
||||
func Load(l *sl.ServiceLocator) (*Config, error) {
|
||||
m, err := godotenv.Read(".env")
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.mode = "production"
|
||||
mode := "production"
|
||||
if v, ok := m["MODE"]; ok {
|
||||
c.mode = v
|
||||
mode = v
|
||||
}
|
||||
c.host = ":4000"
|
||||
host := ":4000"
|
||||
if v, ok := m["HOST"]; ok {
|
||||
c.host = v
|
||||
host = v
|
||||
}
|
||||
|
||||
return nil
|
||||
return &Config{
|
||||
mode,
|
||||
host,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user