mirror of
https://github.com/ZSCNetSupportDept/scheduler.git
synced 2025-10-28 20:45:05 +08:00
改善配置模块代码~
This commit is contained in:
@@ -1,5 +1,10 @@
|
|||||||
# This is a example file for the system
|
# This is a example file for the system
|
||||||
|
|
||||||
|
app:
|
||||||
|
Name: "scheduler for ZSC Network Support staff"
|
||||||
|
File: "ignore/aa.csv" #成员信息文件的路径,看文档
|
||||||
ListenPort: 25005
|
ListenPort: 25005
|
||||||
|
|
||||||
DB:
|
DB:
|
||||||
Type: "SQLite"
|
Type: "SQLite"
|
||||||
Path: "./scheduler.db" # not need if use SQLite
|
Path: "./scheduler.db" # not need if use SQLite
|
||||||
@@ -7,9 +12,9 @@ DB:
|
|||||||
User: "" # not need if use SQLite
|
User: "" # not need if use SQLite
|
||||||
Password: "" # not need if use SQLite
|
Password: "" # not need if use SQLite
|
||||||
Name: "" # not need if use SQLite
|
Name: "" # not need if use SQLite
|
||||||
#[business]
|
|
||||||
|
business:
|
||||||
Session: "2024-2025" #学年
|
Session: "2024-2025" #学年
|
||||||
Semester: 1 #学期,1 或 2
|
Semester: 1 #学期,1 或 2
|
||||||
StartTime: "2025-3-10" #开始值班的日期,日期必须是星期一
|
StartTime: "2025-3-10" #开始值班的日期,日期必须是星期一
|
||||||
Week: 15 #准备值班多少周
|
Week: 15 #准备值班多少周
|
||||||
File: "member.csv" #成员信息文件的路径,看文档
|
|
||||||
|
|||||||
@@ -5,24 +5,15 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/golang-module/carbon/v2"
|
"github.com/golang-module/carbon/v2"
|
||||||
|
"github.com/spf13/pflag"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Load() {
|
func Load() {
|
||||||
// where to read config
|
|
||||||
viper.SetConfigName("config")
|
|
||||||
viper.SetConfigType("yaml")
|
|
||||||
viper.AddConfigPath(".")
|
|
||||||
|
|
||||||
|
parseArgs()
|
||||||
readconfig()
|
readconfig()
|
||||||
|
fmt.Println(Default)
|
||||||
check()
|
|
||||||
|
|
||||||
debugprint()
|
|
||||||
|
|
||||||
if len(os.Args) != 1 {
|
|
||||||
handleArguments()
|
|
||||||
}
|
|
||||||
|
|
||||||
carbon.SetDefault(carbon.Default{
|
carbon.SetDefault(carbon.Default{
|
||||||
Layout: carbon.DateTimeLayout,
|
Layout: carbon.DateTimeLayout,
|
||||||
@@ -33,66 +24,20 @@ func Load() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func check() {
|
|
||||||
// 暂时只支持SQLite
|
|
||||||
if DB.Type != "SQLite" {
|
|
||||||
fmt.Println("sorry,we support SQLite only so far(At config/config.go : check())")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
if startTime := carbon.Parse(StartTime); startTime.IsMonday() != true {
|
|
||||||
fmt.Println("the start time must be a monday")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
func debugprint() {
|
|
||||||
|
|
||||||
fmt.Printf("ListenPort=%v\n", ListenPort)
|
|
||||||
fmt.Printf("database type:%s\n", DB.Type)
|
|
||||||
fmt.Printf("database path : %s\n", DB.Path)
|
|
||||||
fmt.Printf("database port:%d\n", DB.Port)
|
|
||||||
fmt.Printf("database user:%s\n", DB.User)
|
|
||||||
fmt.Printf("database passowrd:%s\n", DB.Password)
|
|
||||||
fmt.Printf("database name:%s\n", DB.Name)
|
|
||||||
fmt.Printf("session:%s\n", Session)
|
|
||||||
fmt.Printf("semester:%d\n", Semester)
|
|
||||||
fmt.Printf("start time:%s\n", StartTime)
|
|
||||||
fmt.Printf("week:%d\n", Week)
|
|
||||||
fmt.Printf("File=%v\n", File)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleArguments() {
|
|
||||||
if len(os.Args) > 2 {
|
|
||||||
fmt.Println("Please enter only 1 argument")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
switch os.Args[1] {
|
|
||||||
case "newsemester":
|
|
||||||
if DB.Type == "SQLite" {
|
|
||||||
sqliteNewSemester()
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
panic("invalid argument")
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func readconfig() {
|
func readconfig() {
|
||||||
if err := viper.ReadInConfig(); err != nil {
|
if err := viper.ReadInConfig(); err != nil {
|
||||||
fmt.Printf("Error reading config file: %v\n", err)
|
fmt.Printf("Error reading config file: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
ListenPort = viper.GetInt("ListenPort")
|
if err := viper.Unmarshal(&Default); err != nil {
|
||||||
DB.Type = viper.GetString("DB.Type")
|
panic(fmt.Errorf("映射配置到结构体失败: %s", err))
|
||||||
DB.Path = viper.GetString("DB.Path")
|
}
|
||||||
DB.Port = viper.GetInt("DB.Port")
|
}
|
||||||
DB.User = viper.GetString("DB.User")
|
|
||||||
DB.Password = viper.GetString("DB.Password")
|
func parseArgs() {
|
||||||
DB.Name = viper.GetString("DB.Name")
|
pflag.String("config", "config.yaml", "the path to config file.")
|
||||||
Session = viper.GetString("Session")
|
viper.BindPFlags(pflag.CommandLine)
|
||||||
Semester = viper.GetInt("Semester")
|
pflag.Parse()
|
||||||
StartTime = viper.GetString("StartTime")
|
pathToConfigure = viper.GetString("config")
|
||||||
Week = viper.GetInt("Week")
|
viper.SetConfigFile(pathToConfigure)
|
||||||
File = viper.GetString("File")
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +1,26 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
var (
|
type Config struct {
|
||||||
ListenPort int
|
App struct {
|
||||||
DB database
|
Name string `mapstructure:"Name"`
|
||||||
Session string
|
ListenPort int `mapstructure:"ListenPort"`
|
||||||
Semester int
|
File string `mapstructure:"File"`
|
||||||
StartTime string
|
} `mapstructure:"app"`
|
||||||
Week int
|
DB struct {
|
||||||
File string
|
Type string `mapstructure:"Tyoe"`
|
||||||
)
|
Path string `mapstructure:"Path"`
|
||||||
|
Port int `mapstructure:"Port"`
|
||||||
type database struct {
|
User string `mapstructure:"User"`
|
||||||
Type string
|
Password string `mapstructure:"Password"`
|
||||||
Path string
|
Name string `mapstructure:"Name"`
|
||||||
Port int
|
} `mapstructure:"DB"`
|
||||||
User string
|
Business struct {
|
||||||
Password string
|
Session string `mapstructure:"Session"`
|
||||||
Name string
|
Semester int `mapstructure:"Semester"`
|
||||||
|
StartTime string `mapstructure:"StartTime"`
|
||||||
|
Week int `mapstructure:"Week"`
|
||||||
|
} `mapstructure:"business"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var pathToConfigure string
|
||||||
|
var Default Config
|
||||||
|
|||||||
@@ -13,11 +13,12 @@ import (
|
|||||||
var err error
|
var err error
|
||||||
|
|
||||||
func Connect() {
|
func Connect() {
|
||||||
switch config.DB.Type {
|
switch config.Default.DB.Type {
|
||||||
case "SQLite":
|
case "SQLite":
|
||||||
connectSQLite()
|
connectSQLite()
|
||||||
case "PostgreSQL":
|
case "PostgreSQL":
|
||||||
connectPGSQL()
|
//connectPGSQL()
|
||||||
|
PGSQL()
|
||||||
default:
|
default:
|
||||||
panic("DBType error")
|
panic("DBType error")
|
||||||
}
|
}
|
||||||
@@ -25,7 +26,7 @@ func Connect() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func connectSQLite() {
|
func connectSQLite() {
|
||||||
Main, err = gorm.Open(sqlite.Open(config.DB.Path), &gorm.Config{})
|
Main, err = gorm.Open(sqlite.Open(config.Default.DB.Path), &gorm.Config{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("error in connecting to SQLite:")
|
fmt.Printf("error in connecting to SQLite:")
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
@@ -35,7 +36,7 @@ func connectSQLite() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func connectPGSQL() {
|
func connectPGSQL() {
|
||||||
Main, err = gorm.Open(postgres.Open(config.DB.Path))
|
Main, err = gorm.Open(postgres.Open(config.Default.DB.Path), &gorm.Config{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -34,7 +34,7 @@ require (
|
|||||||
github.com/sourcegraph/conc v0.3.0 // indirect
|
github.com/sourcegraph/conc v0.3.0 // indirect
|
||||||
github.com/spf13/afero v1.11.0 // indirect
|
github.com/spf13/afero v1.11.0 // indirect
|
||||||
github.com/spf13/cast v1.6.0 // indirect
|
github.com/spf13/cast v1.6.0 // indirect
|
||||||
github.com/spf13/pflag v1.0.5 // indirect
|
github.com/spf13/pflag v1.0.6 // indirect
|
||||||
github.com/subosito/gotenv v1.6.0 // indirect
|
github.com/subosito/gotenv v1.6.0 // indirect
|
||||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||||
github.com/valyala/fasttemplate v1.2.2 // indirect
|
github.com/valyala/fasttemplate v1.2.2 // indirect
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -66,6 +66,8 @@ github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
|
|||||||
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
|
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
|
||||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
|
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
|
||||||
|
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI=
|
github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI=
|
||||||
github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg=
|
github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ func generateTable(time carbon.Carbon) (*[7][]*model.Member, error) {
|
|||||||
week, dayOfWeek := getWorkDay(time)
|
week, dayOfWeek := getWorkDay(time)
|
||||||
//检查传入时间有没有问题
|
//检查传入时间有没有问题
|
||||||
//TODO:这里好像有bug(对日期是否在值班时间内的判断部分),不过不怎么影响使用
|
//TODO:这里好像有bug(对日期是否在值班时间内的判断部分),不过不怎么影响使用
|
||||||
if (week < 0) || (week > config.Week) {
|
if (week < 0) || (week > config.Default.Business.Week) {
|
||||||
return nil, errors.New("日期错误,日期需要在本学期的值班日期内并且格式正确")
|
return nil, errors.New("日期错误,日期需要在本学期的值班日期内并且格式正确")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,7 +135,7 @@ func generateTable(time carbon.Carbon) (*[7][]*model.Member, error) {
|
|||||||
|
|
||||||
// 读取csv文件
|
// 读取csv文件
|
||||||
func readTableData(m *[]*model.Member) error {
|
func readTableData(m *[]*model.Member) error {
|
||||||
data, err := os.OpenFile(config.File, os.O_RDWR|os.O_CREATE, os.ModePerm)
|
data, err := os.OpenFile(config.Default.App.File, os.O_RDWR|os.O_CREATE, os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
// 输入一个时间,返回时间是第几周的第几天
|
// 输入一个时间,返回时间是第几周的第几天
|
||||||
func getWorkDay(in carbon.Carbon) (weekOffset int, dayOffset int) {
|
func getWorkDay(in carbon.Carbon) (weekOffset int, dayOffset int) {
|
||||||
time := carbon.Parse(config.StartTime)
|
time := carbon.Parse(config.Default.Business.StartTime)
|
||||||
_weekOffset := time.DiffInWeeks(in)
|
_weekOffset := time.DiffInWeeks(in)
|
||||||
_dayOffset := in.DayOfWeek()
|
_dayOffset := in.DayOfWeek()
|
||||||
return int(_weekOffset), _dayOffset
|
return int(_weekOffset), _dayOffset
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -18,7 +18,7 @@ func main() {
|
|||||||
app := echo.New()
|
app := echo.New()
|
||||||
register(app)
|
register(app)
|
||||||
|
|
||||||
listenAddress := fmt.Sprintf(":%d", config.ListenPort)
|
listenAddress := fmt.Sprintf(":%d", config.Default.App.ListenPort)
|
||||||
|
|
||||||
app.Logger.Fatal(app.Start(listenAddress))
|
app.Logger.Fatal(app.Start(listenAddress))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user