mirror of
https://github.com/eiblog/eiblog.git
synced 2026-02-10 08:32:26 +08:00
chore: run blog
This commit is contained in:
119
pkg/cache/cache.go
vendored
119
pkg/cache/cache.go
vendored
@@ -3,6 +3,7 @@ package cache
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -23,7 +24,7 @@ var (
|
||||
Ei *Cache
|
||||
|
||||
// regenerate pages chan
|
||||
pagesCh = make(chan string, 1)
|
||||
pagesCh = make(chan string, 2)
|
||||
pageSeries = "series-md"
|
||||
pageArchive = "archive-md"
|
||||
)
|
||||
@@ -77,6 +78,79 @@ type Cache struct {
|
||||
ArticlesMap map[string]*model.Article // slug:article
|
||||
}
|
||||
|
||||
// // LoadInsertAccount 读取或创建账户
|
||||
// LoadInsertAccount(ctx context.Context, acct *model.Account) (*model.Account, error)
|
||||
// // UpdateAccount 更新账户
|
||||
// UpdateAccount(ctx context.Context, name string, fields map[string]interface{}) error
|
||||
//
|
||||
// // LoadInsertBlogger 读取或创建博客
|
||||
// LoadInsertBlogger(ctx context.Context, blogger *model.Blogger) (*model.Blogger, error)
|
||||
// // UpdateBlogger 更新博客
|
||||
// UpdateBlogger(ctx context.Context, fields map[string]interface{}) error
|
||||
//
|
||||
// // InsertSeries 创建专题
|
||||
// InsertSeries(ctx context.Context, series *model.Series) error
|
||||
// // RemoveSeries 删除专题
|
||||
// RemoveSeries(ctx context.Context, id int) error
|
||||
// // UpdateSeries 更新专题
|
||||
// UpdateSeries(ctx context.Context, id int, fields map[string]interface{}) error
|
||||
// // LoadAllSeries 读取所有专题
|
||||
// LoadAllSeries(ctx context.Context) (model.SortedSeries, error)
|
||||
//
|
||||
// // InsertArticle 创建文章
|
||||
// InsertArticle(ctx context.Context, article *model.Article) error
|
||||
// // RemoveArticle 硬删除文章
|
||||
// RemoveArticle(ctx context.Context, id int) error
|
||||
// // DeleteArticle 软删除文章,放入回收箱
|
||||
// DeleteArticle(ctx context.Context, id int) error
|
||||
// // CleanArticles 清理回收站文章
|
||||
// CleanArticles(ctx context.Context) error
|
||||
// // UpdateArticle 更新文章
|
||||
// UpdateArticle(ctx context.Context, id int, fields map[string]interface{}) error
|
||||
// // RecoverArticle 恢复文章到草稿
|
||||
// RecoverArticle(ctx context.Context, id int) error
|
||||
// // LoadAllArticle 读取所有文章
|
||||
// LoadAllArticle(ctx context.Context) (model.SortedArticles, error)
|
||||
// // LoadTrashArticles 读取回收箱
|
||||
// LoadTrashArticles(ctx context.Context) (model.SortedArticles, error)
|
||||
// // LoadDraftArticles 读取草稿箱
|
||||
// LoadDraftArticles(ctx context.Context) (model.SortedArticles, error)
|
||||
|
||||
// PageArticles 文章翻页
|
||||
func (c *Cache) PageArticles(page int, pageSize int) (prev,
|
||||
next int, articles []*model.Article) {
|
||||
|
||||
var l int
|
||||
for l = len(c.Articles); l > 0; l-- {
|
||||
if c.Articles[l-1].ID >= config.Conf.BlogApp.General.StartID {
|
||||
break
|
||||
}
|
||||
}
|
||||
if l == 0 {
|
||||
return 0, 0, nil
|
||||
}
|
||||
m := l / pageSize
|
||||
if d := l % pageSize; d > 0 {
|
||||
m++
|
||||
}
|
||||
if page > m {
|
||||
page = m
|
||||
}
|
||||
if page > 1 {
|
||||
prev = page - 1
|
||||
}
|
||||
if page < m {
|
||||
next = page + 1
|
||||
}
|
||||
s := (page - 1) * pageSize
|
||||
e := page * pageSize
|
||||
if e > l {
|
||||
e = l
|
||||
}
|
||||
articles = c.Articles[s:e]
|
||||
return
|
||||
}
|
||||
|
||||
// loadBlogger 博客信息
|
||||
func (c *Cache) loadBlogger() error {
|
||||
blogapp := config.Conf.BlogApp
|
||||
@@ -87,7 +161,7 @@ func (c *Cache) loadBlogger() error {
|
||||
BTitle: blogapp.Blogger.BTitle,
|
||||
Copyright: blogapp.Blogger.Copyright,
|
||||
}
|
||||
blogger, err := c.LoadOrCreateBlogger(blogger)
|
||||
blogger, err := c.LoadInsertBlogger(context.Background(), blogger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -108,7 +182,7 @@ func (c *Cache) loadAccount() error {
|
||||
PhoneN: blogapp.Account.PhoneNumber,
|
||||
Address: blogapp.Account.Address,
|
||||
}
|
||||
account, err := c.LoadOrCreateAccount(account)
|
||||
account, err := c.LoadInsertAccount(context.Background(), account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -118,7 +192,7 @@ func (c *Cache) loadAccount() error {
|
||||
|
||||
// loadArticles 文章信息
|
||||
func (c *Cache) loadArticles() error {
|
||||
articles, err := c.LoadAllArticles()
|
||||
articles, err := c.LoadAllArticle(context.Background())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -254,7 +328,7 @@ func (c *Cache) timerClean() {
|
||||
ticker := time.NewTicker(dur * time.Hour)
|
||||
|
||||
for range ticker.C {
|
||||
err := c.CleanArticles()
|
||||
err := c.CleanArticles(context.Background())
|
||||
if err != nil {
|
||||
logrus.Error("cache.timerClean.CleanArticles: ", err)
|
||||
}
|
||||
@@ -273,38 +347,3 @@ func (c *Cache) timerDisqus() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// PageArticles 文章翻页
|
||||
func (c *Cache) PageArticles(page int, pageSize int) (prev,
|
||||
next int, articles []*model.Article) {
|
||||
|
||||
var l int
|
||||
for l = len(c.Articles); l > 0; l-- {
|
||||
if c.Articles[l-1].ID >= config.Conf.BlogApp.General.StartID {
|
||||
break
|
||||
}
|
||||
}
|
||||
if l == 0 {
|
||||
return 0, 0, nil
|
||||
}
|
||||
m := l / pageSize
|
||||
if d := l % pageSize; d > 0 {
|
||||
m++
|
||||
}
|
||||
if page > m {
|
||||
page = m
|
||||
}
|
||||
if page > 1 {
|
||||
prev = page - 1
|
||||
}
|
||||
if page < m {
|
||||
next = page + 1
|
||||
}
|
||||
s := (page - 1) * pageSize
|
||||
e := page * pageSize
|
||||
if e > l {
|
||||
e = l
|
||||
}
|
||||
articles = c.Articles[s:e]
|
||||
return
|
||||
}
|
||||
|
||||
76
pkg/cache/store/mongodb.go
vendored
76
pkg/cache/store/mongodb.go
vendored
@@ -16,6 +16,9 @@ import (
|
||||
"go.mongodb.org/mongo-driver/mongo/readpref"
|
||||
)
|
||||
|
||||
// driver: mongodb
|
||||
// source: mongodb://localhost:27017
|
||||
|
||||
const (
|
||||
mongoDBName = "eiblog"
|
||||
collectionAccount = "account"
|
||||
@@ -50,42 +53,6 @@ func (db *mongodb) Init(source string) (Store, error) {
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// LoadInsertAccount 读取或创建账户
|
||||
func (db *mongodb) LoadInsertAccount(ctx context.Context,
|
||||
acct *model.Account) (*model.Account, error) {
|
||||
|
||||
collection := db.Database(mongoDBName).Collection(collectionAccount)
|
||||
|
||||
filter := bson.M{"username": config.Conf.BlogApp.Account.Username}
|
||||
result := collection.FindOne(ctx, filter)
|
||||
err := result.Err()
|
||||
if err != nil {
|
||||
if err != mongo.ErrNoDocuments {
|
||||
return nil, err
|
||||
}
|
||||
_, err = collection.InsertOne(ctx, acct)
|
||||
} else {
|
||||
err = result.Decode(acct)
|
||||
}
|
||||
return acct, err
|
||||
}
|
||||
|
||||
// UpdateAccount 更新账户
|
||||
func (db *mongodb) UpdateAccount(ctx context.Context, name string,
|
||||
fields map[string]interface{}) error {
|
||||
|
||||
collection := db.Database(mongoDBName).Collection(collectionAccount)
|
||||
|
||||
filter := bson.M{"username": name}
|
||||
params := bson.M{}
|
||||
for k, v := range fields {
|
||||
params[k] = v
|
||||
}
|
||||
update := bson.M{"$set": params}
|
||||
_, err := collection.UpdateOne(ctx, filter, update)
|
||||
return err
|
||||
}
|
||||
|
||||
// LoadInsertBlogger 读取或创建博客
|
||||
func (db *mongodb) LoadInsertBlogger(ctx context.Context,
|
||||
blogger *model.Blogger) (*model.Blogger, error) {
|
||||
@@ -122,6 +89,42 @@ func (db *mongodb) UpdateBlogger(ctx context.Context,
|
||||
return err
|
||||
}
|
||||
|
||||
// LoadInsertAccount 读取或创建账户
|
||||
func (db *mongodb) LoadInsertAccount(ctx context.Context,
|
||||
acct *model.Account) (*model.Account, error) {
|
||||
|
||||
collection := db.Database(mongoDBName).Collection(collectionAccount)
|
||||
|
||||
filter := bson.M{"username": config.Conf.BlogApp.Account.Username}
|
||||
result := collection.FindOne(ctx, filter)
|
||||
err := result.Err()
|
||||
if err != nil {
|
||||
if err != mongo.ErrNoDocuments {
|
||||
return nil, err
|
||||
}
|
||||
_, err = collection.InsertOne(ctx, acct)
|
||||
} else {
|
||||
err = result.Decode(acct)
|
||||
}
|
||||
return acct, err
|
||||
}
|
||||
|
||||
// UpdateAccount 更新账户
|
||||
func (db *mongodb) UpdateAccount(ctx context.Context, name string,
|
||||
fields map[string]interface{}) error {
|
||||
|
||||
collection := db.Database(mongoDBName).Collection(collectionAccount)
|
||||
|
||||
filter := bson.M{"username": name}
|
||||
params := bson.M{}
|
||||
for k, v := range fields {
|
||||
params[k] = v
|
||||
}
|
||||
update := bson.M{"$set": params}
|
||||
_, err := collection.UpdateOne(ctx, filter, update)
|
||||
return err
|
||||
}
|
||||
|
||||
// InsertSeries 创建专题
|
||||
func (db *mongodb) InsertSeries(ctx context.Context, series *model.Series) error {
|
||||
collection := db.Database(mongoDBName).Collection(collectionSeries)
|
||||
@@ -222,7 +225,6 @@ func (db *mongodb) CleanArticles(ctx context.Context) error {
|
||||
collection := db.Database(mongoDBName).Collection(collectionArticle)
|
||||
|
||||
exp := time.Now().Add(time.Duration(config.Conf.BlogApp.General.Trash) * time.Hour)
|
||||
fmt.Println(exp)
|
||||
filter := bson.M{"deletetime": bson.M{"$gt": time.Time{}, "$lt": exp}}
|
||||
_, err := collection.DeleteMany(ctx, filter)
|
||||
return err
|
||||
|
||||
1
pkg/cache/store/mongodb_test.go
vendored
1
pkg/cache/store/mongodb_test.go
vendored
@@ -155,7 +155,6 @@ func TestDeleteArticle(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
func TestCleanArticles(t *testing.T) {
|
||||
err := store.CleanArticles(context.Background())
|
||||
if err != nil {
|
||||
|
||||
10
pkg/cache/store/store.go
vendored
10
pkg/cache/store/store.go
vendored
@@ -17,16 +17,16 @@ var (
|
||||
|
||||
// Store 存储后端
|
||||
type Store interface {
|
||||
// LoadInsertAccount 读取或创建账户
|
||||
LoadInsertAccount(ctx context.Context, acct *model.Account) (*model.Account, error)
|
||||
// UpdateAccount 更新账户
|
||||
UpdateAccount(ctx context.Context, name string, fields map[string]interface{}) error
|
||||
|
||||
// LoadInsertBlogger 读取或创建博客
|
||||
LoadInsertBlogger(ctx context.Context, blogger *model.Blogger) (*model.Blogger, error)
|
||||
// UpdateBlogger 更新博客
|
||||
UpdateBlogger(ctx context.Context, fields map[string]interface{}) error
|
||||
|
||||
// LoadInsertAccount 读取或创建账户
|
||||
LoadInsertAccount(ctx context.Context, acct *model.Account) (*model.Account, error)
|
||||
// UpdateAccount 更新账户
|
||||
UpdateAccount(ctx context.Context, name string, fields map[string]interface{}) error
|
||||
|
||||
// InsertSeries 创建专题
|
||||
InsertSeries(ctx context.Context, series *model.Series) error
|
||||
// RemoveSeries 删除专题
|
||||
|
||||
Reference in New Issue
Block a user