mirror of
https://github.com/eiblog/eiblog.git
synced 2026-03-01 00:34:58 +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 删除专题
|
||||
|
||||
@@ -26,6 +26,9 @@ func init() {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
generateOpensearch()
|
||||
generateRobots()
|
||||
generateCrossdomain()
|
||||
go timerFeed()
|
||||
go timerSitemap()
|
||||
}
|
||||
@@ -38,29 +41,28 @@ func timerFeed() {
|
||||
return
|
||||
}
|
||||
|
||||
t := time.NewTicker(time.Hour * 4)
|
||||
for now := range t.C {
|
||||
_, _, articles := cache.Ei.PageArticles(1, 20)
|
||||
params := map[string]interface{}{
|
||||
"Titile": cache.Ei.Blogger.BTitle,
|
||||
"SubTitle": cache.Ei.Blogger.SubTitle,
|
||||
"Host": config.Conf.BlogApp.Host,
|
||||
"FeedrURL": config.Conf.BlogApp.FeedRPC.FeedrURL,
|
||||
"BuildDate": now.Format(time.RFC1123Z),
|
||||
"Articles": articles,
|
||||
}
|
||||
f, err := os.OpenFile("assets/feed.xml", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
|
||||
if err != nil {
|
||||
logrus.Error("file: timerFeed.OpenFile: ", err)
|
||||
continue
|
||||
}
|
||||
defer f.Close()
|
||||
err = tpl.Execute(f, params)
|
||||
if err != nil {
|
||||
logrus.Error("file: timerFeed.Execute: ", err)
|
||||
continue
|
||||
}
|
||||
now := time.Now()
|
||||
_, _, articles := cache.Ei.PageArticles(1, 20)
|
||||
params := map[string]interface{}{
|
||||
"Titile": cache.Ei.Blogger.BTitle,
|
||||
"SubTitle": cache.Ei.Blogger.SubTitle,
|
||||
"Host": config.Conf.BlogApp.Host,
|
||||
"FeedrURL": config.Conf.BlogApp.FeedRPC.FeedrURL,
|
||||
"BuildDate": now.Format(time.RFC1123Z),
|
||||
"Articles": articles,
|
||||
}
|
||||
f, err := os.OpenFile("assets/feed.xml", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
|
||||
if err != nil {
|
||||
logrus.Error("file: timerFeed.OpenFile: ", err)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
err = tpl.Execute(f, params)
|
||||
if err != nil {
|
||||
logrus.Error("file: timerFeed.Execute: ", err)
|
||||
return
|
||||
}
|
||||
time.AfterFunc(time.Hour*4, timerFeed)
|
||||
}
|
||||
|
||||
// timerSitemap 定时刷新sitemap
|
||||
@@ -71,24 +73,22 @@ func timerSitemap() {
|
||||
return
|
||||
}
|
||||
|
||||
t := time.NewTicker(time.Hour * 4)
|
||||
for range t.C {
|
||||
params := map[string]interface{}{
|
||||
"Articles": cache.Ei.Articles,
|
||||
"Host": config.Conf.BlogApp.Host,
|
||||
}
|
||||
f, err := os.OpenFile("assets/sitemap.xml", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
|
||||
if err != nil {
|
||||
logrus.Error("file: timerSitemap.OpenFile: ", err)
|
||||
continue
|
||||
}
|
||||
defer f.Close()
|
||||
err = tpl.Execute(f, params)
|
||||
if err != nil {
|
||||
logrus.Error("file: timerSitemap.Execute: ", err)
|
||||
continue
|
||||
}
|
||||
params := map[string]interface{}{
|
||||
"Articles": cache.Ei.Articles,
|
||||
"Host": config.Conf.BlogApp.Host,
|
||||
}
|
||||
f, err := os.OpenFile("assets/sitemap.xml", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
|
||||
if err != nil {
|
||||
logrus.Error("file: timerSitemap.OpenFile: ", err)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
err = tpl.Execute(f, params)
|
||||
if err != nil {
|
||||
logrus.Error("file: timerSitemap.Execute: ", err)
|
||||
return
|
||||
}
|
||||
time.AfterFunc(time.Hour*24, timerSitemap)
|
||||
}
|
||||
|
||||
// generateOpensearch 生成opensearch.xml
|
||||
@@ -103,7 +103,7 @@ func generateOpensearch() {
|
||||
"SubTitle": cache.Ei.Blogger.SubTitle,
|
||||
"Host": config.Conf.BlogApp.Host,
|
||||
}
|
||||
f, err := os.OpenFile("static/opensearch.xml", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
|
||||
f, err := os.OpenFile("assets/opensearch.xml", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
|
||||
if err != nil {
|
||||
logrus.Error("file: generateOpensearch.OpenFile: ", err)
|
||||
return
|
||||
@@ -126,7 +126,7 @@ func generateRobots() {
|
||||
params := map[string]string{
|
||||
"Host": config.Conf.BlogApp.Host,
|
||||
}
|
||||
f, err := os.OpenFile("static/robots.txt", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
|
||||
f, err := os.OpenFile("assets/robots.txt", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
|
||||
if err != nil {
|
||||
logrus.Error("file: generateRobots.OpenFile: ", err)
|
||||
return
|
||||
@@ -149,7 +149,7 @@ func generateCrossdomain() {
|
||||
params := map[string]string{
|
||||
"Host": config.Conf.BlogApp.Host,
|
||||
}
|
||||
f, err := os.OpenFile("static/crossdomain.xml", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
|
||||
f, err := os.OpenFile("assets/crossdomain.xml", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
|
||||
if err != nil {
|
||||
logrus.Error("file: generateCrossdomain.OpenFile: ", err)
|
||||
return
|
||||
|
||||
@@ -9,8 +9,8 @@ import (
|
||||
|
||||
"github.com/eiblog/eiblog/pkg/config"
|
||||
|
||||
"github.com/qiniu/api.v7/v7/auth/qbox"
|
||||
"github.com/qiniu/api.v7/v7/storage"
|
||||
"github.com/qiniu/go-sdk/v7/auth/qbox"
|
||||
"github.com/qiniu/go-sdk/v7/storage"
|
||||
)
|
||||
|
||||
// QiniuUpload 上传文件
|
||||
|
||||
Reference in New Issue
Block a user