diff --git a/cmd/backup/config/config.go b/cmd/backup/config/config.go index cdfcef5..b2b0aef 100644 --- a/cmd/backup/config/config.go +++ b/cmd/backup/config/config.go @@ -30,7 +30,7 @@ func init() { // run mode mode := config.RunMode(os.Getenv("RUN_MODE")) if !mode.IsRunMode() { - panic("config: unsupported env RUN_MODE" + mode) + panic("config: unsupported env RUN_MODE: " + mode) } logrus.Infof("Run mode:%s", mode) @@ -39,8 +39,8 @@ func init() { if err != nil { panic(err) } - path := filepath.Join(dir, "etc", "app.yml") + data, err := os.ReadFile(path) if err != nil { panic(err) diff --git a/cmd/eiblog/config/config.go b/cmd/eiblog/config/config.go index ed31f76..3f19b1a 100644 --- a/cmd/eiblog/config/config.go +++ b/cmd/eiblog/config/config.go @@ -44,7 +44,7 @@ func init() { // run mode mode := config.RunMode(os.Getenv("RUN_MODE")) if !mode.IsRunMode() { - panic("config: unsupported env RUN_MODE" + mode) + panic("config: unsupported env RUN_MODE: " + mode) } logrus.Infof("Run mode:%s", mode) @@ -54,8 +54,8 @@ func init() { if err != nil { panic(err) } - path := filepath.Join(WorkDir, "etc", "app.yml") + data, err := os.ReadFile(path) if err != nil { panic(err) diff --git a/cmd/eiblog/handler/admin/admin.go b/cmd/eiblog/handler/admin/admin.go index 76bb42e..3c2cc50 100644 --- a/cmd/eiblog/handler/admin/admin.go +++ b/cmd/eiblog/handler/admin/admin.go @@ -69,7 +69,7 @@ func handleAcctLogin(c *gin.Context) { internal.Ei.Account.LoginIP = c.ClientIP() internal.Ei.Account.LoginAt = time.Now() - internal.Ei.UpdateAccount(context.Background(), user, map[string]interface{}{ + internal.Store.UpdateAccount(context.Background(), user, map[string]interface{}{ "login_ip": internal.Ei.Account.LoginIP, "login_at": internal.Ei.Account.LoginAt, }) @@ -89,7 +89,7 @@ func handleAPIBlogger(c *gin.Context) { return } - err := internal.Ei.UpdateBlogger(context.Background(), map[string]interface{}{ + err := internal.Store.UpdateBlogger(context.Background(), map[string]interface{}{ "blog_name": bn, "b_title": bt, "bei_an": ba, @@ -124,7 +124,7 @@ func handleAPIAccount(c *gin.Context) { return } - err := internal.Ei.UpdateAccount(context.Background(), internal.Ei.Account.Username, + err := internal.Store.UpdateAccount(context.Background(), internal.Ei.Account.Username, map[string]interface{}{ "email": e, "phone_n": pn, @@ -160,7 +160,7 @@ func handleAPIPassword(c *gin.Context) { } newPwd := tools.EncryptPasswd(internal.Ei.Account.Username, nw) - err := internal.Ei.UpdateAccount(context.Background(), internal.Ei.Account.Username, + err := internal.Store.UpdateAccount(context.Background(), internal.Ei.Account.Username, map[string]interface{}{ "password": newPwd, }) @@ -181,7 +181,7 @@ func handleDraftDelete(c *gin.Context) { responseNotice(c, NoticeNotice, "参数错误", "") return } - err = internal.Ei.RemoveArticle(context.Background(), id) + err = internal.Store.RemoveArticle(context.Background(), id) if err != nil { logrus.Error("handleDraftDelete.RemoveArticle: ", err) responseNotice(c, NoticeNotice, "删除失败", "") @@ -310,7 +310,7 @@ func handleAPIPostCreate(c *gin.Context) { article.UpdatedAt = time.Now() } // 数据库更新 - err = internal.Ei.UpdateArticle(context.Background(), article.ID, map[string]interface{}{ + err = internal.Store.UpdateArticle(context.Background(), article.ID, map[string]interface{}{ "title": article.Title, "content": article.Content, "serie_id": article.SerieID, @@ -384,7 +384,7 @@ func handleAPISerieCreate(c *gin.Context) { responseNotice(c, NoticeNotice, "专题不存在", "") return } - err = internal.Ei.UpdateSerie(context.Background(), mid, map[string]interface{}{ + err = internal.Store.UpdateSerie(context.Background(), mid, map[string]interface{}{ "slug": slug, "name": name, "desc": desc, @@ -422,7 +422,7 @@ func handleAPITrashDelete(c *gin.Context) { responseNotice(c, NoticeNotice, "参数错误", "") return } - err = internal.Ei.RemoveArticle(context.Background(), id) + err = internal.Store.RemoveArticle(context.Background(), id) if err != nil { responseNotice(c, NoticeNotice, err.Error(), "") return @@ -440,7 +440,7 @@ func handleAPITrashRecover(c *gin.Context) { return } - err = internal.Ei.UpdateArticle(context.Background(), id, map[string]interface{}{ + err = internal.Store.UpdateArticle(context.Background(), id, map[string]interface{}{ "deleted_at": time.Time{}, "is_draft": true, }) diff --git a/cmd/eiblog/handler/internal/cache.go b/cmd/eiblog/handler/internal/cache.go index 07f7328..ad00bd9 100644 --- a/cmd/eiblog/handler/internal/cache.go +++ b/cmd/eiblog/handler/internal/cache.go @@ -39,22 +39,13 @@ var ( func init() { // init timezone var err error - tools.TimeLocation, err = time.LoadLocation( - config.Conf.General.Timezone) - if err != nil { - panic(err) - } - // init store - logrus.Info("store drivers: ", store.Drivers()) - store, err := store.NewStore(config.Conf.Database.Driver, - config.Conf.Database.Source) + tools.TimeLocation, err = time.LoadLocation(config.Conf.General.Timezone) if err != nil { panic(err) } // Ei init Ei = &Cache{ lock: sync.Mutex{}, - Store: store, TagArticles: make(map[string]model.SortedArticles), ArticlesMap: make(map[string]*model.Article), } @@ -70,7 +61,6 @@ func init() { // Cache 整站缓存 type Cache struct { lock sync.Mutex - store.Store // load from db Blogger *model.Blogger @@ -92,7 +82,7 @@ func (c *Cache) AddArticle(article *model.Article) error { defer c.lock.Unlock() // store - err := c.InsertArticle(context.Background(), article, ArticleStartID) + err := Store.InsertArticle(context.Background(), article, ArticleStartID) if err != nil { return err } @@ -131,7 +121,7 @@ func (c *Cache) DelArticle(id int) error { return nil } // set delete - err := c.UpdateArticle(context.Background(), id, map[string]interface{}{ + err := Store.UpdateArticle(context.Background(), id, map[string]interface{}{ "deleted_at": time.Now(), }) if err != nil { @@ -147,7 +137,7 @@ func (c *Cache) AddSerie(serie *model.Serie) error { c.lock.Lock() defer c.lock.Unlock() - err := c.InsertSerie(context.Background(), serie) + err := Store.InsertSerie(context.Background(), serie) if err != nil { return err } @@ -166,7 +156,7 @@ func (c *Cache) DelSerie(id int) error { if len(serie.Articles) > 0 { return errors.New("请删除该专题下的所有文章") } - err := c.RemoveSerie(context.Background(), id) + err := Store.RemoveSerie(context.Background(), id) if err != nil { return err } @@ -236,7 +226,7 @@ func (c *Cache) PageArticleBE(se int, kw string, draft, del bool, p, search.Fields[store.SearchArticleTitle] = kw } } - articles, count, err := c.LoadArticleList(context.Background(), search) + articles, count, err := Store.LoadArticleList(context.Background(), search) if err != nil { return nil, 0 } @@ -410,7 +400,7 @@ func (c *Cache) loadOrInit() error { BTitle: fmt.Sprintf("%s's Blog", strings.Title(config.Conf.Account.Username)), Copyright: `本站使用「署名 4.0 国际」创作共享协议,转载请注明作者及原网址。`, } - created, err := c.LoadInsertBlogger(context.Background(), blogger) + created, err := Store.LoadInsertBlogger(context.Background(), blogger) if err != nil { return err } @@ -423,7 +413,7 @@ func (c *Cache) loadOrInit() error { Slug: "about", CreatedAt: time.Time{}.AddDate(0, 0, 1), } - err = c.InsertArticle(context.Background(), about, ArticleStartID) + err = Store.InsertArticle(context.Background(), about, ArticleStartID) if err != nil { return err } @@ -436,7 +426,7 @@ func (c *Cache) loadOrInit() error { Slug: "blogroll", CreatedAt: time.Time{}.AddDate(0, 0, 7), } - err = c.InsertArticle(context.Background(), blogroll, ArticleStartID) + err = Store.InsertArticle(context.Background(), blogroll, ArticleStartID) if err != nil { return err } @@ -449,13 +439,13 @@ func (c *Cache) loadOrInit() error { Username: config.Conf.Account.Username, Password: pwd, } - _, err = c.LoadInsertAccount(context.Background(), account) + _, err = Store.LoadInsertAccount(context.Background(), account) if err != nil { return err } c.Account = account // series - series, err := c.LoadAllSerie(context.Background()) + series, err := Store.LoadAllSerie(context.Background()) if err != nil { return err } @@ -466,7 +456,7 @@ func (c *Cache) loadOrInit() error { Limit: 9999, Fields: map[string]interface{}{store.SearchArticleDraft: false}, } - articles, _, err := c.LoadArticleList(context.Background(), search) + articles, _, err := Store.LoadArticleList(context.Background(), search) if err != nil { return err } @@ -561,7 +551,7 @@ func (c *Cache) timerClean() { for now := range ticker.C { exp := now.Add(TrashArticleExp) - err := c.CleanArticles(context.Background(), exp) + err := Store.CleanArticles(context.Background(), exp) if err != nil { logrus.Error("cache.timerClean.CleanArticles: ", err) } diff --git a/cmd/eiblog/handler/internal/internal.go b/cmd/eiblog/handler/internal/internal.go index 0d99ed4..bdb840e 100644 --- a/cmd/eiblog/handler/internal/internal.go +++ b/cmd/eiblog/handler/internal/internal.go @@ -2,6 +2,7 @@ package internal import ( "github.com/eiblog/eiblog/cmd/eiblog/config" + "github.com/eiblog/eiblog/cmd/eiblog/handler/internal/store" "github.com/eiblog/eiblog/pkg/third/disqus" "github.com/eiblog/eiblog/pkg/third/es" "github.com/eiblog/eiblog/pkg/third/pinger" @@ -15,6 +16,7 @@ var ( DisqusClient *disqus.DisqusClient QiniuClient *qiniu.QiniuClient Pinger *pinger.Pinger + Store store.Store ) func init() { @@ -23,16 +25,25 @@ func init() { if err != nil { logrus.Fatal("init es client: ", err) } + DisqusClient, err = disqus.NewDisqusClient(config.Conf.Host, config.Conf.Disqus) if err != nil { logrus.Fatal("init disqus client: ", err) } + QiniuClient, err = qiniu.NewQiniuClient(config.Conf.Qiniu) if err != nil { logrus.Fatal("init qiniu client: ", err) } + Pinger, err = pinger.NewPinger(config.Conf.Host, config.Conf.FeedRPC) if err != nil { logrus.Fatal("init pinger: ", err) } + + logrus.Info("store drivers: ", store.Drivers()) + Store, err = store.NewStore(config.Conf.Database.Driver, config.Conf.Database.Source) + if err != nil { + logrus.Fatal("init store: ", err) + } } diff --git a/cmd/eiblog/handler/page/be.go b/cmd/eiblog/handler/page/be.go index d53a181..327def7 100644 --- a/cmd/eiblog/handler/page/be.go +++ b/cmd/eiblog/handler/page/be.go @@ -60,7 +60,7 @@ func handleAdminPost(c *gin.Context) { params := baseBEParams(c) id, err := strconv.Atoi(c.Query("cid")) if err == nil && id > 0 { - article, _ := internal.Ei.LoadArticle(context.Background(), id) + article, _ := internal.Store.LoadArticle(context.Background(), id) if article != nil { params["Title"] = "编辑文章 | " + internal.Ei.Blogger.BTitle params["Edit"] = article @@ -169,7 +169,7 @@ func handleDraftDelete(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": "参数错误"}) return } - err = internal.Ei.RemoveArticle(context.Background(), id) + err = internal.Store.RemoveArticle(context.Background(), id) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "删除错误"}) return @@ -190,7 +190,7 @@ func handleAdminDraft(c *gin.Context) { Limit: 9999, Fields: map[string]interface{}{store.SearchArticleDraft: true}, } - params["List"], _, err = internal.Ei.LoadArticleList(context.Background(), search) + params["List"], _, err = internal.Store.LoadArticleList(context.Background(), search) if err != nil { logrus.Error("handleDraft.LoadDraftArticles: ", err) c.Status(http.StatusBadRequest) @@ -212,7 +212,7 @@ func handleAdminTrash(c *gin.Context) { Limit: 9999, Fields: map[string]interface{}{store.SearchArticleTrash: true}, } - params["List"], _, err = internal.Ei.LoadArticleList(context.Background(), search) + params["List"], _, err = internal.Store.LoadArticleList(context.Background(), search) if err != nil { logrus.Error("handleTrash.LoadArticleList: ", err) } diff --git a/cmd/eiblog/handler/page/fe.go b/cmd/eiblog/handler/page/fe.go index 69d0f9b..7493057 100644 --- a/cmd/eiblog/handler/page/fe.go +++ b/cmd/eiblog/handler/page/fe.go @@ -238,7 +238,7 @@ func handleDisqusList(c *gin.Context) { } else if internal.DisqusClient.ThreadDetails(artc) == nil { dcs.Data.Thread = artc.Thread } - internal.Ei.UpdateArticle(context.Background(), artc.ID, + internal.Store.UpdateArticle(context.Background(), artc.ID, map[string]interface{}{ "thread": artc.Thread, }) diff --git a/pkg/config/config.go b/pkg/config/config.go index d350a03..12eb98e 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -8,8 +8,9 @@ import ( // RunMode 列表 const ( - RunModeDev RunMode = "dev" // 开发环境 - RunModeProd RunMode = "pro" // 生产环境 + RunModeLocal RunMode = "local" // 本地环境 + RunModeDev RunMode = "dev" // 开发环境 + RunModeProd RunMode = "pro" // 生产环境 ) // RunMode 运行模式 @@ -20,14 +21,9 @@ func (mode RunMode) IsReleaseMode() bool { return mode == RunModeProd } -// IsDevMode 是否时开发模式 -func (mode RunMode) IsDevMode() bool { - return mode == RunModeDev -} - // IsRunMode 是否是runmode func (mode RunMode) IsRunMode() bool { - return mode == RunModeDev || mode == RunModeProd + return mode == RunModeDev || mode == RunModeProd || mode == RunModeLocal } // WalkWorkDir walk work dir diff --git a/scripts/run_app.sh b/scripts/run_app.sh index eacac9e..9df495a 100755 --- a/scripts/run_app.sh +++ b/scripts/run_app.sh @@ -1,3 +1,3 @@ #!/usr/bin/env sh -go run cmd/$1/main.go +cd cmd/$1 && go run main.go