This commit is contained in:
2024-11-17 13:42:58 +08:00
parent 2d741a3f0f
commit 983cde2c7d
7 changed files with 106 additions and 33 deletions

View File

@@ -7,42 +7,79 @@ import (
)
func Load() {
// where to read config
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
fmt.Printf("Error reading config file: %v\n", err)
os.Exit(1)
}
readconfig()
ListenPort = viper.GetInt("ListenPort")
File = viper.GetString("File")
DB.Path = viper.GetString("DB.Path")
//DB.Port = viper.GetString("DB.Port")
DB.Type = viper.GetString("DB.Type")
err := check()
if err != nil {
fmt.Println("check your config!")
os.Exit(1)
}
check()
debugprint()
if len(os.Args) != 1 {
handleArguments()
}
}
func check() error {
return nil
func check() {
// 暂时只支持SQLite
if DB.Type != "SQLite" {
fmt.Println("sorry,we support SQLite only so far(At config/config.go : check())")
os.Exit(1)
}
}
func debugprint() {
fmt.Printf("ListenPort=%v\n", ListenPort)
fmt.Printf("File=%v\n", File)
fmt.Printf("database path : %s\n", DB.Path)
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() {
if err := viper.ReadInConfig(); err != nil {
fmt.Printf("Error reading config file: %v\n", err)
os.Exit(1)
}
ListenPort = viper.GetInt("ListenPort")
DB.Type = viper.GetString("DB.Type")
DB.Path = viper.GetString("DB.Path")
DB.Port = viper.GetInt("DB.Port")
DB.User = viper.GetString("DB.User")
DB.Password = viper.GetString("DB.Password")
DB.Name = viper.GetString("DB.Name")
Session = viper.GetString("Session")
Semester = viper.GetInt("Semester")
StartTime = viper.GetString("StartTime")
Week = viper.GetInt("Week")
File = viper.GetString("File")
}

View File

@@ -2,16 +2,19 @@ package config
var (
ListenPort int
File string
DB database
Session string
Semester int
StartTime string
Week int
File string
)
type database struct {
Path string
//Port string
//User string
//Password string
// enable if you want use an instance other than SQLite
Type string
Type string
Path string
Port int
User string
Password string
Name string
}

5
config/newsemester.go Normal file
View File

@@ -0,0 +1,5 @@
package config
func sqliteNewSemester() {
}

View File

@@ -2,9 +2,11 @@ package config
import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func Route(app *echo.Echo) {
// here is the route for our site
app.File("/", "FrontEnd/index.html")
staticFiles := app.Group("/")
staticFiles.Use(middleware.Static("./FrontEnd"))
}