improve: 使用标准项目结构

This commit is contained in:
Mmx233
2023-03-01 18:58:11 +08:00
parent cb11426bd6
commit 3c63e9ddc3
29 changed files with 178 additions and 183 deletions

67
internal/global/config.go Normal file
View File

@@ -0,0 +1,67 @@
package global
import (
"github.com/Mmx233/BitSrunLoginGo/internal/global/models"
"github.com/Mmx233/BitSrunLoginGo/pkg/srun"
"github.com/Mmx233/tool"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"os"
"time"
)
var Config models.Config
var Timeout time.Duration
func readConfig() {
//配置文件默认值
viper.SetDefault("form", srun.LoginForm{
Domain: "www.msftconnecttest.com",
UserType: "cmcc",
})
viper.SetDefault("meta", srun.LoginMeta{
N: "200",
Type: "1",
Acid: "5",
Enc: "srun_bx1",
})
viper.SetDefault("settings", models.Settings{
Basic: models.Basic{
Timeout: 5,
},
Daemon: models.Daemon{
Path: ".BitSrun",
},
Guardian: models.Guardian{
Duration: 300,
},
Log: models.Log{
FilePath: "./",
},
DDNS: models.DDNS{
Enable: false,
TTL: 600,
Domain: "www.example.com",
},
})
//生成配置文件
if !tool.File.Exists(Flags.Path) {
e := viper.WriteConfigAs(Flags.Path)
if e != nil {
log.Fatalln("[init] 生成配置文件失败:", e)
}
log.Infoln("[init] 已生成配置文件,请编辑 '" + Flags.Path + "' 然后重试")
os.Exit(0)
}
//读取配置文件
viper.SetConfigFile(Flags.Path)
if e := viper.ReadInConfig(); e != nil {
log.Fatalln("[init] 读取配置文件失败:", e)
}
if e := viper.Unmarshal(&Config); e != nil {
log.Fatalln("[init] 解析配置文件失败:", e)
}
}

24
internal/global/flags.go Normal file
View File

@@ -0,0 +1,24 @@
package global
import (
"flag"
)
var Flags struct {
//配置文件路径
Path string
//daemon模式内置标记
RunningDaemon bool
//强制daemon
Daemon bool
//指定 client ip
ClientIP string
}
func initFlags() {
flag.StringVar(&Flags.Path, "config", "Config.yaml", "config path")
flag.StringVar(&Flags.ClientIP, "ip", "", "client ip for login")
flag.BoolVar(&Flags.RunningDaemon, "running-daemon", false, "")
flag.BoolVar(&Flags.Daemon, "daemon", false, "")
flag.Parse()
}

18
internal/global/init.go Normal file
View File

@@ -0,0 +1,18 @@
package global
import (
"time"
)
func init() {
initFlags()
//配置文件初始化
readConfig()
//初始化常变量
Timeout = time.Duration(Config.Settings.Basic.Timeout) * time.Second
//初始化日志配置
initLog()
}

48
internal/global/log.go Normal file
View File

@@ -0,0 +1,48 @@
package global
import (
"io"
"os"
"strings"
"time"
nested "github.com/antonfisher/nested-logrus-formatter"
log "github.com/sirupsen/logrus"
)
func initLog() {
if Config.Settings.Log.DebugLevel {
log.SetLevel(log.DebugLevel)
}
if Config.Settings.Log.WriteFile {
//日志路径初始化与处理
if !strings.HasSuffix(Config.Settings.Log.FilePath, "/") {
Config.Settings.Log.FilePath += "/"
}
e := os.MkdirAll(Config.Settings.Log.FilePath, os.ModePerm)
if e != nil {
log.Fatalln(e)
}
if Config.Settings.Log.FileName == "" {
Config.Settings.Log.FileName = time.Now().Format("2006.01.02-15.04.05") + ".log"
}
f, e := os.OpenFile(Config.Settings.Log.FilePath+Config.Settings.Log.FileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if e != nil {
log.Fatalln(e)
}
//设置双重输出
mw := io.MultiWriter(os.Stdout, f)
log.SetOutput(mw)
//设置输出格式
log.SetFormatter(&nested.Formatter{
HideKeys: true,
NoColors: Config.Settings.Log.WriteFile,
TimestampFormat: "2006-01-02 15:04:05",
})
}
}

View File

@@ -0,0 +1,52 @@
package models
import (
"github.com/Mmx233/BitSrunLoginGo/pkg/srun"
)
type Daemon struct {
Enable bool `json:"enable" yaml:"enable" mapstructure:"enable"`
Path string `json:"path" yaml:"path" mapstructure:"path"`
}
type Guardian struct {
Enable bool `json:"enable" yaml:"enable" mapstructure:"enable"`
Duration uint `json:"duration" yaml:"duration" mapstructure:"duration"`
}
type Basic struct {
Https bool `json:"https" yaml:"https" mapstructure:"https"`
SkipCertVerify bool `json:"skip_cert_verify" yaml:"skip_cert_verify" mapstructure:"skip_cert_verify"`
Timeout uint `json:"timeout" yaml:"timeout" mapstructure:"timeout"`
Interfaces string `json:"interfaces" yaml:"interfaces" mapstructure:"interfaces"`
UseDhcpIP bool `json:"use_dhcp_ip" yaml:"use_dhcp_ip" mapstructure:"use_dhcp_ip"`
}
type Log struct {
DebugLevel bool `json:"debug_level" yaml:"debug_level" mapstructure:"debug_level"`
WriteFile bool `json:"write_file" yaml:"write_file" mapstructure:"write_file"`
FilePath string `json:"log_path" yaml:"log_path" mapstructure:"log_path"`
FileName string `json:"log_name" yaml:"log_name" mapstructure:"log_name"`
}
type DDNS struct {
Enable bool `json:"enable" yaml:"enable" mapstructure:"enable"`
TTL uint `json:"ttl" yaml:"ttl" mapstructure:"ttl"`
Domain string `json:"domain" yaml:"domain" mapstructure:"domain"`
Provider string `json:"provider" yaml:"provider" mapstructure:"provider"`
Config map[string]interface{} `mapstructure:",remain"`
}
type Settings struct {
Basic Basic `json:"basic" yaml:"basic" mapstructure:"basic"`
Guardian Guardian `json:"guardian" yaml:"guardian" mapstructure:"guardian"`
Daemon Daemon `json:"daemon" yaml:"daemon" mapstructure:"daemon"`
Log Log `json:"log" yaml:"log" mapstructure:"log"`
DDNS DDNS `json:"ddns" yaml:"ddns" mapstructure:"ddns"`
}
type Config struct {
Form srun.LoginForm `json:"form" yaml:"form" mapstructure:"form"`
Meta srun.LoginMeta `json:"meta" yaml:"meta" mapstructure:"meta"`
Settings Settings `json:"settings" yaml:"settings" mapstructure:"settings"`
}