mirror of
https://github.com/dreamstarsky/runbin.git
synced 2026-05-15 14:23:07 +00:00
first demo
This commit is contained in:
43
internal/config/LoadWorker.go
Normal file
43
internal/config/LoadWorker.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type LimitConfig struct {
|
||||
Cpu float32
|
||||
Memory int
|
||||
Time float32
|
||||
}
|
||||
|
||||
type WorkerConfig struct {
|
||||
Storage StorageConfig
|
||||
Limit LimitConfig
|
||||
Process int
|
||||
Name string
|
||||
}
|
||||
|
||||
func LoadWorker(configFile string) *WorkerConfig {
|
||||
v := viper.New()
|
||||
v.SetConfigFile(configFile)
|
||||
v.SetConfigType("yaml")
|
||||
|
||||
v.SetDefault("limit.cpu", 1.0)
|
||||
v.SetDefault("limit.time", 10.0)
|
||||
v.SetDefault("limit.memory", 512*1024)
|
||||
v.SetDefault("process", 1)
|
||||
v.SetDefault("name", "default name")
|
||||
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
log.Fatalf("Failed to read config file: %v", err)
|
||||
}
|
||||
|
||||
var cfg WorkerConfig
|
||||
if err := v.Unmarshal(&cfg); err != nil {
|
||||
log.Fatalf("Failed to unmarshal config: %v", err)
|
||||
}
|
||||
|
||||
return &cfg
|
||||
}
|
||||
Reference in New Issue
Block a user