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" "fmt"
"os" "os"
"time" "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 { func SetDefaultConfig() error {
// Define default values with explanations cfg := Config{
defaults := map[string]string{ LogLevel: LogLevelInfo,
// Logging level: DEBUG, INFO, WARNING, ERROR MySQLUser: "mysql",
"LOG_LEVEL": "INFO", MySQLPassword: "pass",
MySQLHost: "localhost",
"MYSQL_USER": "mysql", MySQLPort: "3306",
"MYSQL_PASSWORD": "pass", MySQLDBName: "backgo",
"MYSQL_HOST": "mysql", APIServerBindAddress: "0.0.0.0",
"MYSQL_PORT": "3306", APIServerPort: "6678",
"MYSQL_DB_NAME": "backgo", TransportServerBindAddress: "0.0.0.0",
TransportServerPort: "6679",
// API server configuration S3Enabled: false,
"API_SERVER_BIND_ADDRESS": "0.0.0.0", // IP address to bind the API server S3ConnectionAddress: "",
"API_SERVER_PORT": "6678", // Port for the API server S3AuthorizationInfo: "",
S3Region: "us-east-1",
// Transport server configuration for internal communication S3BucketInfo: "",
"TRANSPORT_SERVER_BIND_ADDRESS": "0.0.0.0", // IP address for the transport server SMBEnabled: false,
"TRANSPORT_SERVER_PORT": "6679", // Port for the transport server SMBConnectionAddress: "",
SMBAuthorizationInfo: "",
// S3 storage configuration B2Enabled: false,
"S3_ENABLED": "false", // Enable S3 storage (true/false) B2ConnectionAddress: "",
"S3_CONNECTION_ADDRESS": "", // S3 connection address in the format {IP}:{PORT} B2AuthorizationInfo: "",
"S3_AUTHORIZATION_INFO": "", // S3 authorization information (access key:secret key) B2BucketInfo: "",
"S3_REGION": "us-east-1", // AWS region for S3 LocalStorageEnabled: true,
"S3_BUCKET_INFO": "", // S3 bucket name LocalStorageDirectory: fmt.Sprintf("/etc/PixelRidge/BackGo/local-data-storage/%s-%d", os.Getenv("HOSTNAME"), time.Now().Unix()),
Nodes: []string{},
// 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
} }
// Write defaults to environment variables return WriteConfigToFile(&cfg)
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)
}
}
// write these defaults to a file for persistence func WriteConfigToFile(cfg *Config) error {
// write to a JSON or YAML file 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 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/go-sql-driver/mysql v1.8.1
github.com/hirochachacha/go-smb2 v1.1.0 github.com/hirochachacha/go-smb2 v1.1.0
golang.org/x/crypto v0.21.0 golang.org/x/crypto v0.21.0
gopkg.in/yaml.v3 v3.0.1
) )
require ( 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.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 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= 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/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 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 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=