From c1df1ec84aff9cdf37f82cc75b7bacd85452005a Mon Sep 17 00:00:00 2001 From: Dustin Stiles Date: Sun, 23 Mar 2025 10:17:45 -0400 Subject: [PATCH] refactor(internal/peer): change DSFX_STORAGE_DIR to DSFX_DATA_DIR --- docs/hosting.md | 16 ++++++------ internal/peer/peer.go | 57 ++++++++++++++++++++++++++----------------- 2 files changed, 43 insertions(+), 30 deletions(-) diff --git a/docs/hosting.md b/docs/hosting.md index bce21b8..e06865b 100644 --- a/docs/hosting.md +++ b/docs/hosting.md @@ -46,18 +46,18 @@ go build -o ./cmd/dsfx DSFX uses the following environment variables to configure its behavior: -| Variable | Description | Default Value | -| ---------------- | ----------------------------------------------------------- | ---------------- | -| DSFX_HOST | The host on which the DSFX server will run | 0.0.0.0 | -| DSFX_PORT | The port on which the DSFX server will listen | 8000 | -| DSFX_LOG_LEVEL | The log level to use. One of (error, warn, info, debug) | info | -| DSFX_CONFIG_DIR | The directory where the DSFX configuration files are stored | /etc/dsfx/config | -| DSFX_STORAGE_DIR | The directory where the DSFX storage files are stored | /etc/dsfx/data | +| Variable | Description | Default Value | +| --------------- | ----------------------------------------------------------- | ---------------- | +| DSFX_HOST | The host on which the DSFX server will run | 0.0.0.0 | +| DSFX_PORT | The port on which the DSFX server will listen | 8000 | +| DSFX_LOG_LEVEL | The log level to use. One of (error, warn, info, debug) | info | +| DSFX_DATA_DIR | The directory where the DSFX data files are stored | /etc/dsfx/data | +| DSFX_CONFIG_DIR | The directory where the DSFX configuration files are stored | /etc/dsfx/config | ## Local Files The DSFX server uses local files for configuration and storage. The default directories for these -files are specified in the `DSFX_CONFIG_DIR` and `DSFX_STORAGE_DIR` environment variables. You can +files are specified in the `DSFX_CONFIG_DIR` and `DSFX_DATA_DIR` environment variables. You can change these directories by setting the corresponding environment variables before starting the server. For docker installations, it is recommended to mount the local directories to the container diff --git a/internal/peer/peer.go b/internal/peer/peer.go index 733db9c..b5a379a 100644 --- a/internal/peer/peer.go +++ b/internal/peer/peer.go @@ -21,22 +21,35 @@ import ( ) const ( - // DefaultConfigDir is the default directory for the dsfx configuration. - DefaultConfigDir = "/etc/dsfx/config" - // DefaultStorageDir is the default directory for the dsfx storage. - DefaultStorageDir = "/etc/dsfx/data" + // EnvHost is the environment variable for the dsfx host. + EnvHost = "DSFX_HOST" + // EnvPort is the environment variable for the dsfx port. + EnvPort = "DSFX_PORT" + // EnvLogLevel is the environment variable for the dsfx log level. + EnvLogLevel = "DSFX_LOG_LEVEL" + // EnvDataDir is the environment variable for the dsfx storage directory. + EnvDataDir = "DSFX_DATA_DIR" + // EnvConfigDir is the environment variable for the dsfx configuration directory. + EnvConfigDir = "DSFX_CONFIG_DIR" +) + +const ( // DefaultHost is the default host for the dsfxctl application. DefaultHost = "0.0.0.0" // DefaultPort is the default port for the dsfxctl application. DefaultPort = "8000" + // DefaultDataDir is the default directory for the dsfx storage. + DefaultDataDir = "/etc/dsfx/data" + // DefaultConfigDir is the default directory for the dsfx configuration. + DefaultConfigDir = "/etc/dsfx/config" ) // Conf holds the configuration for the dsfxctl application. type Conf struct { LogLevel string // Directories - ConfigDir string - StorageDir string + ConfigDir string + DataDir string // Networking Host string Port string @@ -61,26 +74,26 @@ func (c Conf) SlogLevel() slog.Level { func loadConfigFromSystem(sys system.System) Conf { var c Conf - c.ConfigDir = sys.GetEnv("DSFX_CONFIG_DIR") - if c.ConfigDir == "" { - c.ConfigDir = DefaultConfigDir - } - - c.StorageDir = sys.GetEnv("DSFX_STORAGE_DIR") - if c.StorageDir == "" { - c.StorageDir = DefaultStorageDir - } - - c.Host = sys.GetEnv("DSFX_HOST") - if c.Host == "" { + c.Host = sys.GetEnv(EnvHost) + if len(c.Host) == 0 { c.Host = DefaultHost } - c.Port = sys.GetEnv("DSFX_PORT") - if c.Port == "" { + c.Port = sys.GetEnv(EnvPort) + if len(c.Port) == 0 { c.Port = DefaultPort } + c.ConfigDir = sys.GetEnv(EnvConfigDir) + if len(c.ConfigDir) == 0 { + c.ConfigDir = DefaultConfigDir + } + + c.DataDir = sys.GetEnv(EnvDataDir) + if len(c.DataDir) == 0 { + c.DataDir = DefaultDataDir + } + // defaults are handled by Conf.SlogLevel. c.LogLevel = sys.GetEnv("DSFX_LOG_LEVEL") @@ -101,7 +114,7 @@ func New(disk disk.Disk, system system.System) *Peer { conf := loadConfigFromSystem(system) config := scoped.New(disk, conf.ConfigDir) - storage := scoped.New(disk, conf.StorageDir) + storage := scoped.New(disk, conf.DataDir) return &Peer{disk, system, config, storage, conf} } @@ -123,7 +136,7 @@ func (p *Peer) Run(ctx context.Context) error { return err } - err = p.disk.MkdirAll(p.conf.StorageDir, 0755) + err = p.disk.MkdirAll(p.conf.DataDir, 0755) if err != nil { logger.ErrorContext(ctx, "failed to create storage dir", slog.Any("error", err)) return err