commit cd79aeff03ff96ccb327dc61c97d341b2ede2e66
parent 82b88934a8172cc77382a3a702aefa4621a45e80
Author: Joel-Haeberli <haebu@rubigen.ch>
Date: Wed, 22 May 2024 15:09:09 +0200
fix: logging, db connection string
Diffstat:
6 files changed, 52 insertions(+), 21 deletions(-)
diff --git a/c2ec/c2ec-config.conf b/c2ec/c2ec-config.conf
@@ -37,10 +37,11 @@ EXCHANGE_ACCOUNT = payto://iban/CH50030202099498
CURRENCY = CHF
# How many retries shall be triggered, when the attestation
-# of a transaction fails
-MAX_RETRIES = 3
+# of a transaction fails (when negative, the )
+MAX_RETRIES = -1
-# How long shall the attestations retry be delayed in milliseconds.
+# How long shall the attestations retry be delayed in milliseconds at max?
+# When the delay is
RETRY_DELAY_MS = 1000
[wire-gateway]
@@ -51,7 +52,7 @@ PASSWORD = secret
[database]
-CONFIG = postgres://local:local@localhost:5432/postgres
+CONFIG = postgres:///c2ec
[provider-wallee]
@@ -62,3 +63,8 @@ KEY = secret
NAME = Simulation
KEY = secret
+
+# [provider-xyz]
+#
+# NAME = xyz
+# KEY = secret
diff --git a/c2ec/c2ec-config.yaml b/c2ec/c2ec-config.yaml
@@ -14,6 +14,7 @@ c2ec:
username: "wire"
password: "secret"
db:
+ connstr: ""
host: "localhost"
port: 5432
username: "local"
diff --git a/c2ec/config.go b/c2ec/config.go
@@ -36,11 +36,12 @@ type C2ECWireGatewayConfig struct {
}
type C2ECDatabseConfig struct {
- Host string `yaml:"host"`
- Port int `yaml:"port"`
- Username string `yaml:"username"`
- Password string `yaml:"password"`
- Database string `yaml:"database"`
+ ConnectionString string `yaml:"connstr"`
+ Host string `yaml:"host"`
+ Port int `yaml:"port"`
+ Username string `yaml:"username"`
+ Password string `yaml:"password"`
+ Database string `yaml:"database"`
}
type C2ECProviderConfig struct {
@@ -224,11 +225,7 @@ func ParseIni(content []byte) (*C2ECConfig, error) {
connstr := value.String()
- // TODO do proper
- err = os.Setenv("PGHOST", connstr)
- if err != nil {
- return nil, err
- }
+ cfg.Database.ConnectionString = connstr
}
if strings.HasPrefix(s.Name(), "provider-") {
diff --git a/c2ec/db-postgres.go b/c2ec/db-postgres.go
@@ -157,6 +157,10 @@ type C2ECPostgres struct {
func PostgresConnectionString(cfg *C2ECDatabseConfig) string {
+ if cfg.ConnectionString != "" {
+ return cfg.ConnectionString
+ }
+
pgHost := os.Getenv("PGHOST")
if pgHost != "" {
LogInfo("postgres", "pghost was set")
@@ -185,7 +189,7 @@ func PostgresConnectionString(cfg *C2ECDatabseConfig) string {
pgPassword = cfg.Password
}
- pgDb := os.Getenv(" PGDATABASE")
+ pgDb := os.Getenv("PGDATABASE")
if pgDb != "" {
LogInfo("postgres", "pghost was set")
} else {
diff --git a/c2ec/logger.go b/c2ec/logger.go
@@ -52,10 +52,10 @@ func logAppend(src string, level LogLevel, msg string) {
func openAppendClose(s string) {
// first try opening only append
- f, err := os.OpenFile(LOG_PATH, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
+ f, err := os.OpenFile(LOG_PATH, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil || f == nil {
// if file does not yet exist, open with create flag.
- f, err = os.OpenFile(LOG_PATH, os.O_CREATE|os.O_APPEND|os.O_WRONLY, os.ModeAppend)
+ f, err = os.OpenFile(LOG_PATH, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if err != nil || f == nil {
fmt.Println("error: ", err.Error())
panic("failed opening or creating log file")
diff --git a/c2ec/main.go b/c2ec/main.go
@@ -46,12 +46,27 @@ var PROVIDER_CLIENTS = map[string]ProviderClient{}
// 8. listen for incoming requests (as specified in config)
func main() {
- LogInfo("main", fmt.Sprintf("starting c2ec at %s", time.Now().Format(time.UnixDate)))
-
cfgPath := DEFAULT_C2EC_CONFIG_PATH
- if len(os.Args) > 1 && os.Args[1] != "" {
- cfgPath = os.Args[1]
+ if len(os.Args) > 1 {
+
+ nextIsConf := false
+ for i, arg := range os.Args {
+ if i == 0 {
+ continue
+ } else if nextIsConf {
+ cfgPath = arg
+ nextIsConf = false
+ } else if arg == "-h" {
+ helpAndExit("")
+ } else if arg == "-c" {
+ nextIsConf = true
+ } else {
+ helpAndExit(arg)
+ }
+ }
}
+
+ LogInfo("main", fmt.Sprintf("starting c2ec at %s", time.Now().Format(time.UnixDate)))
cfg, err := Parse(cfgPath)
if err != nil {
panic("unable to load config: " + err.Error())
@@ -320,3 +335,11 @@ func startListening(router *http.ServeMux, errs chan error) {
}()
}
}
+
+func helpAndExit(unknownOptionOrEmpty string) {
+ if unknownOptionOrEmpty != "" {
+ fmt.Println("unkown option provided:", unknownOptionOrEmpty)
+ }
+ fmt.Println("usage: -h (help) | -c PATH (config file)")
+ os.Exit(0)
+}