From 226c73711ac15017877908e0f053b8ebdc2b2792 Mon Sep 17 00:00:00 2001 From: wuko233 Date: Sun, 29 Mar 2026 00:22:32 +0800 Subject: [PATCH] =?UTF-8?q?[config]=20=E5=88=9D=E6=AD=A5=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?yaml=E9=85=8D=E7=BD=AE=E8=AF=BB=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/main.go | 22 +++++++++++++++++++++ config.yaml | 15 ++++++++++++++ go.mod | 5 +++++ go.sum | 3 +++ internal/config/config.go | 41 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 86 insertions(+) create mode 100644 cmd/main.go create mode 100644 config.yaml create mode 100644 go.mod create mode 100644 go.sum create mode 100644 internal/config/config.go diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..51280f2 --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,22 @@ +package main + +import ( + "fmt" + "log" + "sysmonitord/internal/config" +) + +func main() { + configPath := "./config.yaml" + + cfg, err := config.LoadConfig(configPath) + if err != nil { + log.Fatalf("加载配置失败: %v", err) + } + + fmt.Println("加载配置成功:") + fmt.Printf("审计配置: %+v\n", cfg.Audit) + fmt.Printf("扫描配置: %+v\n", cfg.Scanner) + + fmt.Printf("审计服务器地址:%s:%d\n", cfg.Audit.Server, cfg.Audit.Port) +} diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..d4aaf9c --- /dev/null +++ b/config.yaml @@ -0,0 +1,15 @@ +server: + host: "127.0.0.1" + port: 8080 + +audit: + enabled: true + server: "192.168.1.100" + port: 9000 + buffer_size: 1000 + +scanner: + file: + exclude_paths: + - /proc + - /sys \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b729640 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module sysmonitord + +go 1.26.1 + +require gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4bc0337 --- /dev/null +++ b/go.sum @@ -0,0 +1,3 @@ +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..9e98127 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,41 @@ +package config + +import ( + "fmt" + "os" + + "gopkg.in/yaml.v3" +) + +type Config struct { + Audit AuditConfig `yaml:"audit"` + Scanner ScannerConfig `yaml:"scanner"` +} + +type AuditConfig struct { + Enabled bool `yaml:"enabled"` + Server string `yaml:"server"` + Port int `yaml:"port"` + BufferSize int `yaml:"buffer_size"` +} + +type ScannerConfig struct { + File FileScannerConfig `yaml:"file"` +} + +type FileScannerConfig struct { + ExcludePaths []string `yaml:"exclude_paths"` +} + +func LoadConfig(path string) (*Config, error) { + data, err := os.ReadFile(path) + if err != nil { + return nil, fmt.Errorf("无法读取配置文件: %w", err) + } + + var cfg Config + if err := yaml.Unmarshal(data, &cfg); err != nil { + return nil, fmt.Errorf("无法解析配置文件: %w", err) + } + return &cfg, nil +}