mirror of
https://github.com/lovelaze/nebula-sync.git
synced 2025-11-05 18:29:19 +01:00
Use envconfig and add tests.
This commit is contained in:
committed by
lovelaze
parent
5fa9441847
commit
d698a49cfb
@@ -86,6 +86,7 @@ The following environment variables can be specified:
|
||||
| `TZ` | n/a | `Europe/London` | Specifies the timezone for logs and cron |
|
||||
| `CLIENT_SKIP_TLS_VERIFICATION` | false | true | Skips SSL certificate verification |
|
||||
| `CLIENT_RETRY_DELAY_SECONDS` | 1 | 5 | Seconds to delay between connection attempts |
|
||||
| `CLIENT_TIMEOUT_SECONDS` | 20 | 60 | Http client timeout in seconds |
|
||||
|
||||
> **Note:** The following optional settings apply only if `FULL_SYNC=false`. They allow for granular control of synchronization if a full sync is not wanted.
|
||||
|
||||
|
||||
@@ -3,6 +3,9 @@ package config
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/kelseyhightower/envconfig"
|
||||
"github.com/lovelaze/nebula-sync/internal/pihole/model"
|
||||
@@ -19,6 +22,7 @@ type Config struct {
|
||||
type Client struct {
|
||||
SkipSSLVerification bool `default:"false" envconfig:"CLIENT_SKIP_TLS_VERIFICATION"`
|
||||
RetryDelay int64 `default:"1" envconfig:"CLIENT_RETRY_DELAY_SECONDS"`
|
||||
Timeout uint `default:"20" envconfig:"CLIENT_TIMEOUT_SECONDS"`
|
||||
}
|
||||
|
||||
type GravitySettings struct {
|
||||
@@ -119,20 +123,11 @@ func (c *Config) String() string {
|
||||
return fmt.Sprintf("primary=%s, replicas=%s, fullSync=%t, cron=%s, sync=%s", c.Primary.Url, replicas, c.Sync.FullSync, cron, sync)
|
||||
}
|
||||
|
||||
func (cs *Client) NewHttpClient() *http.Client {
|
||||
defaultTimeout := 20 * time.Second
|
||||
|
||||
timeoutEnv := os.Getenv("HTTP_CLIENT_TIMEOUT")
|
||||
if timeoutEnv != "" {
|
||||
if timeout, err := strconv.Atoi(timeoutEnv); err == nil {
|
||||
defaultTimeout = time.Duration(timeout) * time.Second
|
||||
}
|
||||
}
|
||||
|
||||
func (settings *Client) NewHttpClient() *http.Client {
|
||||
return &http.Client{
|
||||
Timeout: defaultTimeout,
|
||||
Timeout: time.Duration(settings.Timeout) * time.Second,
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: cs.SkipSSLVerification},
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: settings.SkipSSLVerification},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,11 +82,13 @@ func TestConfig_LoadClient(t *testing.T) {
|
||||
conf := Config{}
|
||||
|
||||
t.Setenv("CLIENT_SKIP_TLS_VERIFICATION", "true")
|
||||
t.Setenv("CLIENT_TIMEOUT_SECONDS", "45")
|
||||
|
||||
err := conf.loadClient()
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, true, conf.Client.SkipSSLVerification)
|
||||
assert.Equal(t, uint(45), conf.Client.Timeout)
|
||||
}
|
||||
|
||||
func TestConfig_LoadEnvFile(t *testing.T) {
|
||||
@@ -102,6 +104,7 @@ func TestConfig_LoadEnvFile(t *testing.T) {
|
||||
assert.Equal(t, "Europe/London", os.Getenv("TZ"))
|
||||
|
||||
assert.Equal(t, "true", os.Getenv("CLIENT_SKIP_TLS_VERIFICATION"))
|
||||
assert.Equal(t, "40", os.Getenv("CLIENT_TIMEOUT_SECONDS"))
|
||||
|
||||
assert.Equal(t, "true", os.Getenv("SYNC_CONFIG_DNS"))
|
||||
assert.Equal(t, "true", os.Getenv("SYNC_CONFIG_DHCP"))
|
||||
|
||||
Vendored
+1
@@ -6,6 +6,7 @@ CRON=* * * * *
|
||||
TZ=Europe/London
|
||||
|
||||
CLIENT_SKIP_TLS_VERIFICATION=true
|
||||
CLIENT_TIMEOUT_SECONDS=40
|
||||
|
||||
SYNC_CONFIG_DNS=true
|
||||
SYNC_CONFIG_DHCP=true
|
||||
|
||||
Reference in New Issue
Block a user