From 18a07dd5b3b23c4aeaa39cd54dbbd96d09a92361 Mon Sep 17 00:00:00 2001 From: govolokatliai Date: Fri, 18 Jul 2025 12:14:29 +0800 Subject: [PATCH] 1 --- Makefile | 7 +++++++ aa.csv | 0 build/Makefile | 2 +- config.yaml | 18 ++++++++++++++++++ member.csv | 2 ++ src/config/config.go | 11 ++++++++--- src/config/model.go | 2 +- src/main.go | 13 ++++--------- src/route/route.go | 3 ++- 9 files changed, 43 insertions(+), 15 deletions(-) delete mode 100755 aa.csv create mode 100644 config.yaml create mode 100644 member.csv diff --git a/Makefile b/Makefile index 9fe2aa6..8bdff5f 100644 --- a/Makefile +++ b/Makefile @@ -18,4 +18,11 @@ CONFIG_FILE_PATH = $(PWD)/ignore/secret.yaml # 如果运行,使用的成员信息文件在哪里? CSV_PATH = $(PWD)/ignore/aa.csv +# 运行端口 +LISTEN_PORT = 25005 +# 模板文件的目录 +TEMPLATE_DIR = $(PWD)/src/templates +# 前端文件的目录 +FRONTEND_PATH = $(PWD)/src/FrontEnd + include build/Makefile diff --git a/aa.csv b/aa.csv deleted file mode 100755 index e69de29..0000000 diff --git a/build/Makefile b/build/Makefile index 89e4dfd..5966b83 100644 --- a/build/Makefile +++ b/build/Makefile @@ -15,5 +15,5 @@ build: BackEnd run: - $(TARGET_PATH)/scheduler --config $(CONFIG_FILE_PATH) --csv-path $(CSV_PATH) + TEMPLATE=$(TEMPLATE_DIR) FRONTEND=$(FRONTEND_PATH) CSV_PATH=$(CSV_PATH) $(TARGET_PATH)/scheduler --config $(CONFIG_FILE_PATH) diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..fb48f10 --- /dev/null +++ b/config.yaml @@ -0,0 +1,18 @@ +# 其中有的配置可以通过环境变量覆盖,前端,模板的路径通过环境变量设置 +# 这是一个示例配置文件。 +app: + Name: "scheduler for ZSC Network Support staff" + ListenPort: 25005 + File: "member.csv" #成员信息文件的路径,看文档 +DB: + Type: "SQLite" + Path: "./scheduler.db" + Port: "" + User: "" + Password: "" + Name: "" +business: + Session: "2024-2025" #学年 + Semester: 1 #学期,1 或 2 + StartTime: "2025-3-10" #开始值班的日期,日期必须是星期一 + Week: 15 #准备值班多少周 diff --git a/member.csv b/member.csv new file mode 100644 index 0000000..54ba065 --- /dev/null +++ b/member.csv @@ -0,0 +1,2 @@ +工号,姓名,性别,有空,有空(备用),权限 +1001,示例,1,0,0,1 diff --git a/src/config/config.go b/src/config/config.go index 51e83b5..bece82e 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -3,6 +3,7 @@ package config import ( "fmt" "os" + "strconv" "github.com/golang-module/carbon/v2" "github.com/spf13/pflag" @@ -34,21 +35,25 @@ func readconfig() { if err := viper.Unmarshal(&Default); err != nil { panic(fmt.Errorf("映射配置到结构体失败: %s", err)) } + + FrontEnd = os.Getenv("FRONTEND") } func parseArgs() { pflag.String("config", "./config.yaml", "the path to config file.") pflag.Bool("init-db", false, "whether to initialize the database on starting,useful when migrating to a new one.") - pflag.String("csv-path", "./member.csv", "the CSV file containing member information") viper.BindPFlags(pflag.CommandLine) pflag.Parse() pathToConfigure = viper.GetString("config") InitDB = viper.GetBool("init-db") - CSVPath = viper.GetString("csv-path") } func overrides() { - if CSVPath != "" { + if CSVPath := os.Getenv("CSV_PATH"); CSVPath != "" { Default.App.File = CSVPath } + + if ListenPort, err := strconv.Atoi(os.Getenv("LISTEN_PORT")); ListenPort != 0 && err != nil { + Default.App.ListenPort = ListenPort + } } diff --git a/src/config/model.go b/src/config/model.go index 6626a8e..a0f6f87 100644 --- a/src/config/model.go +++ b/src/config/model.go @@ -26,4 +26,4 @@ type Config struct { var pathToConfigure string //配置文件的路径 var Default Config //系统的默认配置 var InitDB bool -var CSVPath string +var FrontEnd string diff --git a/src/main.go b/src/main.go index d2e99c5..e485b69 100644 --- a/src/main.go +++ b/src/main.go @@ -27,8 +27,10 @@ func main() { route.Route(app) //注册路由表 route.Middleware(app) //注册中间件 - // 暂时在初始化时不注册模板,因为用不上 - //registerTemplate(app) //注册模板 + renderer := tl.Tlw{ + Tl: template.Must(template.ParseGlob(os.Getenv("TEMPLATE") + "/*.html")), + } + app.Renderer = renderer //注册模板 listenAddress := fmt.Sprintf(":%d", config.Default.App.ListenPort) @@ -48,10 +50,3 @@ func csv() { panic(err) } } - -func registerTemplate(app *echo.Echo) { - renderer := tl.Tlw{ - Tl: template.Must(template.ParseGlob("templates/*.html")), - } - app.Renderer = renderer -} diff --git a/src/route/route.go b/src/route/route.go index 2d6e3d6..fee4473 100644 --- a/src/route/route.go +++ b/src/route/route.go @@ -3,13 +3,14 @@ package route import ( "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" + "zsxyww.com/scheduler/config" "zsxyww.com/scheduler/handler" ) func Route(app *echo.Echo) { // here is the route for our site staticFiles := app.Group("/*") - staticFiles.Use(middleware.Static("./FrontEnd")) + staticFiles.Use(middleware.Static(config.FrontEnd)) api := app.Group("/api/") api.GET("getAssignment", handler.GetAssignment)