Init v3.2

This commit is contained in:
JiXieShi
2024-03-21 15:25:12 +08:00
commit 6e70a1d4df
49 changed files with 2115 additions and 0 deletions

109
config/main.go Normal file
View File

@@ -0,0 +1,109 @@
package config
import (
"blog/utils"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
)
type Config struct {
userConfig
systemConfig
}
//
var Cfg Config
func init() {
var err error
Cfg.CurrentDir, err = os.Getwd()
if err != nil {
panic(err)
}
configFile, err := ioutil.ReadFile(Cfg.CurrentDir + "/config.json")
if err != nil {
panic(err)
}
err = json.Unmarshal(configFile, &Cfg)
if err != nil {
panic(err)
}
if "" == Cfg.Dashboard ||
!strings.HasPrefix(Cfg.Dashboard, "/") {
Cfg.Dashboard = "/admin"
}
if "" == Cfg.ThemePath ||
!strings.HasPrefix(Cfg.ThemePath, "/") {
Cfg.ThemesDir = Cfg.CurrentDir + "/themes/blog" //+ Cfg.ThemePath
} else {
Cfg.ThemesDir = Cfg.CurrentDir + "/themes" + Cfg.ThemePath
}
repoName, err := utils.GetRepoName(Cfg.DocumentGitUrl)
if err != nil {
panic(err)
}
Cfg.AppName = "WebServer"
Cfg.Version = 3.2
Cfg.DocumentDir = Cfg.CurrentDir + "/" + repoName
Cfg.GitHookUrl = "/api/git_push_hook"
Cfg.AppRepository = "https://gitea.starss.cc/JiXieShi/Blog"
}
func Initial() {
if _, err := exec.LookPath("git"); err != nil {
fmt.Println("请先安装git")
panic(err)
}
if !utils.IsDir(Cfg.DocumentDir) {
fmt.Println("正在克隆文档仓库,请稍等...")
out, err := utils.RunCmdByDir(Cfg.CurrentDir, "git", "clone", Cfg.DocumentGitUrl)
if err != nil {
panic(err)
}
fmt.Println(out)
} else {
out, err := utils.RunCmdByDir(Cfg.DocumentDir, "git", "pull")
fmt.Println(out)
if err != nil {
panic(err)
}
}
if err := checkDocDirAndBindConfig(&Cfg); err != nil {
fmt.Println("文档缺少必要的目录")
panic(err)
}
imgDir := Cfg.CurrentDir + "/images"
if !utils.IsDir(imgDir) {
if os.Mkdir(imgDir, os.ModePerm) != nil {
panic("生成images目录失败")
}
}
}
func checkDocDirAndBindConfig(cfg *Config) error {
dirs := []string{"assets", "content", "extra_nav"}
for _, dir := range dirs {
absoluteDir := Cfg.DocumentDir + "/" + dir
if !utils.IsDir(absoluteDir) {
return errors.New("documents cannot lack " + absoluteDir + " dir")
}
}
cfg.DocumentAssetsDir = cfg.DocumentDir + "/assets"
cfg.DocumentContentDir = cfg.DocumentDir + "/content"
cfg.DocumentExtraNavDir = cfg.DocumentDir + "/extra_nav"
return nil
}

14
config/system.go Normal file
View File

@@ -0,0 +1,14 @@
package config
type systemConfig struct {
AppName string
Version float32
CurrentDir string
ThemesDir string
GitHookUrl string
AppRepository string
DocumentDir string
DocumentAssetsDir string
DocumentContentDir string
DocumentExtraNavDir string
}

37
config/user.go Normal file
View File

@@ -0,0 +1,37 @@
package config
type userConfig struct {
SiteName string `json:"siteName"`
Author string `json:"author"`
Icp string `json:"icp"`
TimeLayout string `json:"timeLayout"`
Port int `json:"port"`
WebHookSecret string `json:"webHookSecret"`
CategoryDisplayQuantity int `json:"categoryDisplayQuantity"`
UtterancesRepo string `json:"utterancesRepo"`
PageSize int `json:"pageSize"`
DescriptionLen int `json:"descriptionLen"`
DocumentGitUrl string `json:"documentGitUrl"`
HtmlKeywords string `json:"htmlKeywords"`
HtmlDescription string `json:"htmlDescription"`
ThemePath string `json:"themePath"`
ThemeColor string `json:"themeColor"`
ThemeOption []string `json:"themeOption"`
Dashboard string `json:"dashboard"`
}