first demo

This commit is contained in:
xkm
2025-04-14 22:57:36 +08:00
parent 0c76d2169a
commit a960184433
22 changed files with 1047 additions and 218 deletions

View File

@@ -0,0 +1,48 @@
package config
import (
"log"
"github.com/spf13/viper"
)
type AppConfig struct {
Env string
Port int
}
type DatabaseConfig struct {
DSN string
}
type StorageConfig struct {
Type string
Database DatabaseConfig
}
type ApiConfig struct {
App AppConfig
Storage StorageConfig
}
func LoadApi(configFile string) *ApiConfig {
v := viper.New()
v.SetConfigFile(configFile)
v.SetConfigType("yaml")
// Set default values
v.SetDefault("app.env", "development")
v.SetDefault("app.port", 8080)
v.SetDefault("storage.type", "memory")
if err := v.ReadInConfig(); err != nil {
log.Fatalf("Failed to read config file: %v", err)
}
var cfg ApiConfig
if err := v.Unmarshal(&cfg); err != nil {
log.Fatalf("Failed to unmarshal config: %v", err)
}
return &cfg
}

View 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
}