wip
This commit is contained in:
53
src/config.go
Normal file
53
src/config.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package src
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Listen string `json:"LISTEN"`
|
||||
ConnectionDriver string `json:"CONNECTION_DRIVER"`
|
||||
ConnectionString string `json:"CONNECTION_STRING"`
|
||||
db DB
|
||||
}
|
||||
|
||||
func NewConfig(ctx context.Context) (Config, error) {
|
||||
config, err := newConfig()
|
||||
if err != nil {
|
||||
return config, err
|
||||
}
|
||||
|
||||
db, err := NewDB(ctx, config.ConnectionDriver, config.ConnectionString)
|
||||
if err != nil {
|
||||
return config, err
|
||||
}
|
||||
config.db = db
|
||||
|
||||
return config, io.EOF
|
||||
}
|
||||
|
||||
func newConfig() (Config, error) {
|
||||
config := Config{
|
||||
Listen: ":10000",
|
||||
ConnectionDriver: "sqlite",
|
||||
ConnectionString: "/tmp/red-apter.db",
|
||||
}
|
||||
b, _ := json.Marshal(config)
|
||||
var m map[string]any
|
||||
if err := json.Unmarshal(b, &m); err != nil {
|
||||
return config, err
|
||||
}
|
||||
for k := range m {
|
||||
if v := os.Getenv(k); v != "" {
|
||||
m[k] = v
|
||||
}
|
||||
}
|
||||
b2, _ := json.Marshal(m)
|
||||
if err := json.Unmarshal(b2, &config); err != nil {
|
||||
return config, err
|
||||
}
|
||||
return config, nil
|
||||
}
|
||||
Reference in New Issue
Block a user