1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-23 21:44:41 -05:00
sub2clash/config/config.go

78 lines
1.8 KiB
Go
Raw Normal View History

2023-09-12 06:40:24 -04:00
package config
2023-09-12 12:46:17 -04:00
import (
"github.com/joho/godotenv"
"os"
"strconv"
)
2023-09-12 06:40:24 -04:00
type Config struct {
2023-09-12 12:46:17 -04:00
Port int
MetaTemplate string
ClashTemplate string
RequestRetryTimes int
RequestMaxFileSize int64
2023-09-13 01:47:22 -04:00
CacheExpire int64
LogLevel string
2023-09-17 04:59:02 -04:00
BasePath string
2023-09-12 06:40:24 -04:00
}
var Default *Config
func init() {
Default = &Config{
2023-09-12 12:46:17 -04:00
MetaTemplate: "template_meta.yaml",
ClashTemplate: "template_clash.yaml",
RequestRetryTimes: 3,
RequestMaxFileSize: 1024 * 1024 * 1,
Port: 8011,
2023-09-13 01:47:22 -04:00
CacheExpire: 60 * 5,
LogLevel: "info",
2023-09-17 04:59:02 -04:00
BasePath: "/",
2023-09-12 12:46:17 -04:00
}
2023-09-17 05:02:59 -04:00
_ = godotenv.Load()
2023-09-12 12:46:17 -04:00
if os.Getenv("PORT") != "" {
atoi, err := strconv.Atoi(os.Getenv("PORT"))
if err != nil {
2023-09-13 01:47:22 -04:00
panic("PORT invalid")
2023-09-12 12:46:17 -04:00
}
Default.Port = atoi
}
if os.Getenv("META_TEMPLATE") != "" {
Default.MetaTemplate = os.Getenv("META_TEMPLATE")
}
if os.Getenv("CLASH_TEMPLATE") != "" {
Default.ClashTemplate = os.Getenv("CLASH_TEMPLATE")
}
if os.Getenv("REQUEST_RETRY_TIMES") != "" {
atoi, err := strconv.Atoi(os.Getenv("REQUEST_RETRY_TIMES"))
if err != nil {
2023-09-13 01:47:22 -04:00
panic("REQUEST_RETRY_TIMES invalid")
2023-09-12 12:46:17 -04:00
}
Default.RequestRetryTimes = atoi
}
if os.Getenv("REQUEST_MAX_FILE_SIZE") != "" {
atoi, err := strconv.Atoi(os.Getenv("REQUEST_MAX_FILE_SIZE"))
if err != nil {
2023-09-13 01:47:22 -04:00
panic("REQUEST_MAX_FILE_SIZE invalid")
2023-09-12 12:46:17 -04:00
}
Default.RequestMaxFileSize = int64(atoi)
2023-09-12 06:40:24 -04:00
}
2023-09-13 01:47:22 -04:00
if os.Getenv("CACHE_EXPIRE") != "" {
atoi, err := strconv.Atoi(os.Getenv("CACHE_EXPIRE"))
if err != nil {
panic("CACHE_EXPIRE invalid")
}
Default.CacheExpire = int64(atoi)
}
if os.Getenv("LOG_LEVEL") != "" {
Default.LogLevel = os.Getenv("LOG_LEVEL")
}
2023-09-17 04:59:02 -04:00
if os.Getenv("BASE_PATH") != "" {
Default.BasePath = os.Getenv("BASE_PATH")
if Default.BasePath[len(Default.BasePath)-1] != '/' {
Default.BasePath += "/"
}
}
2023-09-12 06:40:24 -04:00
}