Refactor default config writer

This commit is contained in:
VetheonGames 2024-03-28 18:48:18 -06:00
parent b6ad826204
commit 123b4e7da7
3 changed files with 45 additions and 55 deletions

View File

@ -4,67 +4,53 @@ import (
"fmt"
"os"
"time"
yaml "gopkg.in/yaml.v3"
)
// SetDefaultConfig sets the default configuration by writing to environment variables
// and create a default configuration file.
func SetDefaultConfig() error {
// Define default values with explanations
defaults := map[string]string{
// Logging level: DEBUG, INFO, WARNING, ERROR
"LOG_LEVEL": "INFO",
"MYSQL_USER": "mysql",
"MYSQL_PASSWORD": "pass",
"MYSQL_HOST": "mysql",
"MYSQL_PORT": "3306",
"MYSQL_DB_NAME": "backgo",
// API server configuration
"API_SERVER_BIND_ADDRESS": "0.0.0.0", // IP address to bind the API server
"API_SERVER_PORT": "6678", // Port for the API server
// Transport server configuration for internal communication
"TRANSPORT_SERVER_BIND_ADDRESS": "0.0.0.0", // IP address for the transport server
"TRANSPORT_SERVER_PORT": "6679", // Port for the transport server
// S3 storage configuration
"S3_ENABLED": "false", // Enable S3 storage (true/false)
"S3_CONNECTION_ADDRESS": "", // S3 connection address in the format {IP}:{PORT}
"S3_AUTHORIZATION_INFO": "", // S3 authorization information (access key:secret key)
"S3_REGION": "us-east-1", // AWS region for S3
"S3_BUCKET_INFO": "", // S3 bucket name
// SMB storage configuration
"SMB_ENABLED": "false", // Enable SMB storage (true/false)
"SMB_CONNECTION_ADDRESS": "", // SMB connection address in the format {IP}:{PORT}
"SMB_AUTHORIZATION_INFO": "", // SMB authorization information (e.g., username and password)
// BackBlaze B2 storage configuration
"B2_ENABLED": "false", // Enable BackBlaze B2 storage (true/false)
"B2_CONNECTION_ADDRESS":"", // BackBlaze B2 connection address in the format {IP}:{PORT}
"B2_AUTHORIZATION_INFO":"", // BackBlaze B2 authorization information
"B2_BUCKET_INFO": "", // BackBlaze B2 bucket name
// Local storage configuration
"LOCAL_STORAGE_ENABLED": "true", // Enable local storage (true/false)
"LOCAL_STORAGE_DIRECTORY": fmt.Sprintf("/etc/PixelRidge/BackGo/local-data-storage/%s-%d", os.Getenv("HOSTNAME"), time.Now().Unix()), // Path for local storage directory
// Nodes configuration for cluster setup
"NODES": "", // Comma-separated list of node addresses in the cluster
cfg := Config{
LogLevel: LogLevelInfo,
MySQLUser: "mysql",
MySQLPassword: "pass",
MySQLHost: "localhost",
MySQLPort: "3306",
MySQLDBName: "backgo",
APIServerBindAddress: "0.0.0.0",
APIServerPort: "6678",
TransportServerBindAddress: "0.0.0.0",
TransportServerPort: "6679",
S3Enabled: false,
S3ConnectionAddress: "",
S3AuthorizationInfo: "",
S3Region: "us-east-1",
S3BucketInfo: "",
SMBEnabled: false,
SMBConnectionAddress: "",
SMBAuthorizationInfo: "",
B2Enabled: false,
B2ConnectionAddress: "",
B2AuthorizationInfo: "",
B2BucketInfo: "",
LocalStorageEnabled: true,
LocalStorageDirectory: fmt.Sprintf("/etc/PixelRidge/BackGo/local-data-storage/%s-%d", os.Getenv("HOSTNAME"), time.Now().Unix()),
Nodes: []string{},
}
// Write defaults to environment variables
for key, value := range defaults {
if err := os.Setenv(key, value); err != nil {
return fmt.Errorf("failed to set default config for %s: %v", key, err)
}
}
return WriteConfigToFile(&cfg)
}
// write these defaults to a file for persistence
// write to a JSON or YAML file
func WriteConfigToFile(cfg *Config) error {
file, err := os.OpenFile(configFilePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("failed to open config file for writing: %v", err)
}
defer file.Close()
encoder := yaml.NewEncoder(file)
encoder.SetIndent(2)
if err := encoder.Encode(cfg); err != nil {
return fmt.Errorf("failed to write config to file: %v", err)
}
return nil
}
// include a function here to write the configuration to a file

1
go.mod
View File

@ -10,6 +10,7 @@ require (
github.com/go-sql-driver/mysql v1.8.1
github.com/hirochachacha/go-smb2 v1.1.0
golang.org/x/crypto v0.21.0
gopkg.in/yaml.v3 v3.0.1
)
require (

3
go.sum
View File

@ -65,6 +65,9 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=