Skip to content

Commit

Permalink
🦄 refactor(config): 将config作为一个模块独立
Browse files Browse the repository at this point in the history
  • Loading branch information
dingdinglz committed Oct 8, 2024
1 parent 6fbe15c commit a51b971
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 37 deletions.
6 changes: 3 additions & 3 deletions config.go → appconfig/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package appconfig

import (
"encoding/json"
Expand All @@ -13,10 +13,10 @@ func ConfigInit() {
tool.DictoryCreateN(filepath.Join(rootPath, "data"))
if !tool.FileIsExsits(filepath.Join(rootPath, "data", "config.json")) {
Init = true
appConfig.Port = 7000
AppConfigVar.Port = 7000
} else {
Init = false
jsonText, _ := os.ReadFile(filepath.Join(rootPath, "data", "config.json"))
json.Unmarshal(jsonText, &appConfig)
json.Unmarshal(jsonText, &AppConfigVar)
}
}
16 changes: 16 additions & 0 deletions appconfig/global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package appconfig

import (
"github.com/gofiber/fiber/v2"
)

var (
MainServer *fiber.App
Init bool
AppConfigVar AppConfig
Version string = "v2 beta"
)

type AppConfig struct {
Port int `json:"port"`
}
15 changes: 0 additions & 15 deletions global.go

This file was deleted.

6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package main
import (
"fmt"

"github.com/dingdinglz/dingbot/route"
"github.com/dingdinglz/dingbot/appconfig"
)

func main() {
fmt.Println("dingbot " + route.Version + " Copyright © dinglz 2024")
ConfigInit()
fmt.Println("dingbot " + appconfig.Version + " Copyright © dinglz 2024")
appconfig.ConfigInit()
ServerInit()
ServerRun()
}
7 changes: 5 additions & 2 deletions route/tool.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package route

import "github.com/gofiber/fiber/v2"
import (
"github.com/dingdinglz/dingbot/appconfig"
"github.com/gofiber/fiber/v2"
)

func GenerateRenderMap() fiber.Map {
return fiber.Map{"Version": Version}
return fiber.Map{"Version": appconfig.Version}
}
5 changes: 0 additions & 5 deletions route/var.go

This file was deleted.

19 changes: 10 additions & 9 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"
"strconv"

"github.com/dingdinglz/dingbot/appconfig"
"github.com/dingdinglz/dingbot/route"
"github.com/dingdinglz/dingbot/tool"
"github.com/gofiber/fiber/v2"
Expand All @@ -18,13 +19,13 @@ func ServerInit() {
rootPath, _ := os.Getwd()
tEngine := html.New(filepath.Join(rootPath, "web", "index"), ".html")
tEngine.ShouldReload = true
MainServer = fiber.New(fiber.Config{Views: tEngine})
MainServer.Use(recover.New(), logger.New())
MainServer.Static("/", filepath.Join(rootPath, "web", "public"))
appconfig.MainServer = fiber.New(fiber.Config{Views: tEngine})
appconfig.MainServer.Use(recover.New(), logger.New())
appconfig.MainServer.Static("/", filepath.Join(rootPath, "web", "public"))
}

func ServerRun() {
if Init {
if appconfig.Init {
ServerInitRun()
} else {
ServerCommonRun()
Expand All @@ -34,18 +35,18 @@ func ServerRun() {
func ServerInitRun() {
rootPath, _ := os.Getwd()
fmt.Println("Init Mode:Please open http://127.0.0.1:7000/ to init the program.")
apiRoute := MainServer.Group("/api")
apiRoute := appconfig.MainServer.Group("/api")
apiRoute.Post("/init", route.InitRoute)
MainServer.Static("/", filepath.Join(rootPath, "web", "init", "index.html"))
err := MainServer.Listen("0.0.0.0:" + strconv.Itoa(appConfig.Port))
appconfig.MainServer.Static("/", filepath.Join(rootPath, "web", "init", "index.html"))
err := appconfig.MainServer.Listen("0.0.0.0:" + strconv.Itoa(appconfig.AppConfigVar.Port))
if err != nil {
tool.ErrorOut("startServer", "Listen", err)
}
}

func ServerCommonRun() {
MainServer.Get("/", route.IndexRoute)
err := MainServer.Listen("0.0.0.0:" + strconv.Itoa(appConfig.Port))
appconfig.MainServer.Get("/", route.IndexRoute)
err := appconfig.MainServer.Listen("0.0.0.0:" + strconv.Itoa(appconfig.AppConfigVar.Port))
if err != nil {
tool.ErrorOut("startServer", "Listen", err)
}
Expand Down

0 comments on commit a51b971

Please sign in to comment.