Refactor configuration management and enhance build process
- Introduced a YAML-based configuration system, allowing for automatic generation of a default `config.yaml` file. - Updated the application to load configuration settings from the YAML file, improving flexibility and ease of use. - Added a Makefile to streamline development tasks, including running the application, testing, and managing dependencies. - Enhanced `.gitignore` to include build artifacts and configuration files. - Removed unused Prometheus metrics and related code to simplify the codebase. - Updated dependencies in `go.mod` and `go.sum` for improved functionality and performance.
This commit is contained in:
83
cmd/root.go
83
cmd/root.go
@@ -2,26 +2,22 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"s1d3sw1ped/SteamCache2/config"
|
||||
"s1d3sw1ped/SteamCache2/steamcache"
|
||||
"s1d3sw1ped/SteamCache2/steamcache/logger"
|
||||
"s1d3sw1ped/SteamCache2/version"
|
||||
"strings"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
threads int
|
||||
|
||||
memory string
|
||||
disk string
|
||||
diskpath string
|
||||
upstream string
|
||||
|
||||
memoryGC string
|
||||
diskGC string
|
||||
threads int
|
||||
configPath string
|
||||
|
||||
logLevel string
|
||||
logFormat string
|
||||
@@ -58,7 +54,47 @@ var rootCmd = &cobra.Command{
|
||||
logger.Logger.Info().
|
||||
Msg("SteamCache2 " + version.Version + " " + version.Date + " starting...")
|
||||
|
||||
address := ":80"
|
||||
// Load configuration
|
||||
cfg, err := config.LoadConfig(configPath)
|
||||
if err != nil {
|
||||
// Check if the error is because the config file doesn't exist
|
||||
// The error is wrapped, so we check the error message
|
||||
if strings.Contains(err.Error(), "no such file") ||
|
||||
strings.Contains(err.Error(), "cannot find the file") ||
|
||||
strings.Contains(err.Error(), "The system cannot find the file") {
|
||||
logger.Logger.Info().
|
||||
Str("config_path", configPath).
|
||||
Msg("Config file not found, creating default configuration")
|
||||
|
||||
if err := config.SaveDefaultConfig(configPath); err != nil {
|
||||
logger.Logger.Error().
|
||||
Err(err).
|
||||
Str("config_path", configPath).
|
||||
Msg("Failed to create default configuration")
|
||||
fmt.Fprintf(os.Stderr, "Error: Failed to create default config at %s: %v\n", configPath, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
logger.Logger.Info().
|
||||
Str("config_path", configPath).
|
||||
Msg("Default configuration created successfully. Please edit the file and run again.")
|
||||
|
||||
fmt.Printf("Default configuration created at %s\n", configPath)
|
||||
fmt.Println("Please edit the configuration file as needed and run the application again.")
|
||||
os.Exit(0)
|
||||
} else {
|
||||
logger.Logger.Error().
|
||||
Err(err).
|
||||
Str("config_path", configPath).
|
||||
Msg("Failed to load configuration")
|
||||
fmt.Fprintf(os.Stderr, "Error: Failed to load configuration from %s: %v\n", configPath, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
logger.Logger.Info().
|
||||
Str("config_path", configPath).
|
||||
Msg("Configuration loaded successfully")
|
||||
|
||||
if runtime.GOMAXPROCS(-1) != threads {
|
||||
runtime.GOMAXPROCS(threads)
|
||||
@@ -68,17 +104,17 @@ var rootCmd = &cobra.Command{
|
||||
}
|
||||
|
||||
sc := steamcache.New(
|
||||
address,
|
||||
memory,
|
||||
disk,
|
||||
diskpath,
|
||||
upstream,
|
||||
memoryGC,
|
||||
diskGC,
|
||||
cfg.ListenAddress,
|
||||
cfg.Cache.Memory.Size,
|
||||
cfg.Cache.Disk.Size,
|
||||
cfg.Cache.Disk.Path,
|
||||
cfg.Upstream,
|
||||
cfg.Cache.Memory.GCAlgorithm,
|
||||
cfg.Cache.Disk.GCAlgorithm,
|
||||
)
|
||||
|
||||
logger.Logger.Info().
|
||||
Msg("SteamCache2 " + version.Version + " started on " + address)
|
||||
Msg("SteamCache2 " + version.Version + " started on " + cfg.ListenAddress)
|
||||
|
||||
sc.Run()
|
||||
|
||||
@@ -97,17 +133,10 @@ func Execute() {
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.Flags().StringVarP(&configPath, "config", "c", "config.yaml", "Path to configuration file")
|
||||
|
||||
rootCmd.Flags().IntVarP(&threads, "threads", "t", runtime.GOMAXPROCS(-1), "Number of worker threads to use for processing requests")
|
||||
|
||||
rootCmd.Flags().StringVarP(&memory, "memory", "m", "0", "The size of the memory cache")
|
||||
rootCmd.Flags().StringVarP(&disk, "disk", "d", "0", "The size of the disk cache")
|
||||
rootCmd.Flags().StringVarP(&diskpath, "disk-path", "p", "", "The path to the disk cache")
|
||||
|
||||
rootCmd.Flags().StringVarP(&upstream, "upstream", "u", "", "The upstream server to proxy requests overrides the host header from the client but forwards the original host header to the upstream server")
|
||||
|
||||
rootCmd.Flags().StringVarP(&memoryGC, "memory-gc", "", "lru", "Memory cache GC algorithm: lru, lfu, fifo, largest, smallest, hybrid")
|
||||
rootCmd.Flags().StringVarP(&diskGC, "disk-gc", "", "lru", "Disk cache GC algorithm: lru, lfu, fifo, largest, smallest, hybrid")
|
||||
|
||||
rootCmd.Flags().StringVarP(&logLevel, "log-level", "l", "info", "Logging level: debug, info, error")
|
||||
rootCmd.Flags().StringVarP(&logFormat, "log-format", "f", "console", "Logging format: json, console")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user