mirror of
https://github.com/eiblog/eiblog.git
synced 2026-02-04 22:02:26 +08:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5e63f5e69d | ||
|
|
33afbd351d | ||
|
|
eaeeaaafb9 | ||
|
|
ae3fb35435 | ||
|
|
c1d73f1a45 |
@@ -2,6 +2,15 @@
|
||||
|
||||
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
||||
|
||||
### [3.0.5](https://github.com/eiblog/eiblog/compare/v3.0.4...v3.0.5) (2025-07-25)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* admin login session ([c1d73f1](https://github.com/eiblog/eiblog/commit/c1d73f1a453af62d15c25a79c382d9cefd8a3d2e))
|
||||
* mongodb uri error ([33afbd3](https://github.com/eiblog/eiblog/commit/33afbd351d2b41f9edf36959908c3f183745d903))
|
||||
* RUN_MODE error ([eaeeaaa](https://github.com/eiblog/eiblog/commit/eaeeaaafb98a4aa0a42dba74b411b1d361faf1d5))
|
||||
|
||||
### [3.0.4](https://github.com/eiblog/eiblog/compare/v3.0.3...v3.0.4) (2025-07-25)
|
||||
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/eiblog/eiblog/cmd/backup/config"
|
||||
"github.com/eiblog/eiblog/pkg/connector/db"
|
||||
pdb "github.com/eiblog/eiblog/pkg/connector/db"
|
||||
)
|
||||
|
||||
// MongoStorage 备份恢复器
|
||||
@@ -46,11 +46,11 @@ func (r MongoStorage) Restore(path string) error {
|
||||
defer cancel()
|
||||
|
||||
// drop database
|
||||
mdb, err := db.NewMDB(config.Conf.Database)
|
||||
database, err := pdb.NewMDB(ctx, config.Conf.Database)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = mdb.Drop(ctx)
|
||||
err = database.Drop(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -6,17 +6,18 @@ import (
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/eiblog/eiblog/cmd/eiblog/config"
|
||||
pdb "github.com/eiblog/eiblog/pkg/connector/db"
|
||||
"github.com/eiblog/eiblog/pkg/model"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/mongo/readpref"
|
||||
)
|
||||
|
||||
// example:
|
||||
// driver: mongodb
|
||||
// source: mongodb://localhost:27017
|
||||
// source: mongodb://localhost:27017/eiblog
|
||||
|
||||
const (
|
||||
mongoDBName = "eiblog"
|
||||
@@ -31,44 +32,39 @@ const (
|
||||
)
|
||||
|
||||
type mongodb struct {
|
||||
*mongo.Client
|
||||
*mongo.Database
|
||||
}
|
||||
|
||||
// Init init mongodb client
|
||||
func (db *mongodb) Init(name, source string) (Store, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
|
||||
defer cancel()
|
||||
|
||||
opts := options.Client().ApplyURI(source)
|
||||
client, err := mongo.Connect(ctx, opts)
|
||||
database, err := pdb.NewMDB(ctx, config.Conf.Database)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = client.Ping(ctx, readpref.Primary())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db.Client = client
|
||||
db.Database = database
|
||||
// create index
|
||||
indexModel := mongo.IndexModel{
|
||||
Keys: bson.D{bson.E{Key: "username", Value: 1}},
|
||||
Options: options.Index().SetUnique(true).SetSparse(true),
|
||||
}
|
||||
db.Database(mongoDBName).Collection(collectionAccount).
|
||||
db.Database.Collection(collectionAccount).
|
||||
Indexes().
|
||||
CreateOne(context.Background(), indexModel)
|
||||
indexModel = mongo.IndexModel{
|
||||
Keys: bson.D{bson.E{Key: "slug", Value: 1}},
|
||||
Options: options.Index().SetUnique(true).SetSparse(true),
|
||||
}
|
||||
db.Database(mongoDBName).Collection(collectionArticle).
|
||||
db.Database.Collection(collectionArticle).
|
||||
Indexes().
|
||||
CreateOne(context.Background(), indexModel)
|
||||
indexModel = mongo.IndexModel{
|
||||
Keys: bson.D{bson.E{Key: "slug", Value: 1}},
|
||||
Options: options.Index().SetUnique(true).SetSparse(true),
|
||||
}
|
||||
db.Database(mongoDBName).Collection(collectionSerie).
|
||||
db.Database.Collection(collectionSerie).
|
||||
Indexes().
|
||||
CreateOne(context.Background(), indexModel)
|
||||
return db, nil
|
||||
@@ -78,7 +74,7 @@ func (db *mongodb) Init(name, source string) (Store, error) {
|
||||
func (db *mongodb) LoadInsertBlogger(ctx context.Context,
|
||||
blogger *model.Blogger) (created bool, err error) {
|
||||
|
||||
collection := db.Database(mongoDBName).Collection(collectionBlogger)
|
||||
collection := db.Database.Collection(collectionBlogger)
|
||||
|
||||
filter := bson.M{}
|
||||
result := collection.FindOne(ctx, filter)
|
||||
@@ -99,7 +95,7 @@ func (db *mongodb) LoadInsertBlogger(ctx context.Context,
|
||||
func (db *mongodb) UpdateBlogger(ctx context.Context,
|
||||
fields map[string]interface{}) error {
|
||||
|
||||
collection := db.Database(mongoDBName).Collection(collectionBlogger)
|
||||
collection := db.Database.Collection(collectionBlogger)
|
||||
|
||||
filter := bson.M{}
|
||||
params := bson.M{}
|
||||
@@ -115,7 +111,7 @@ func (db *mongodb) UpdateBlogger(ctx context.Context,
|
||||
func (db *mongodb) LoadInsertAccount(ctx context.Context,
|
||||
acct *model.Account) (created bool, err error) {
|
||||
|
||||
collection := db.Database(mongoDBName).Collection(collectionAccount)
|
||||
collection := db.Database.Collection(collectionAccount)
|
||||
|
||||
filter := bson.M{"username": acct.Username}
|
||||
result := collection.FindOne(ctx, filter)
|
||||
@@ -136,7 +132,7 @@ func (db *mongodb) LoadInsertAccount(ctx context.Context,
|
||||
func (db *mongodb) UpdateAccount(ctx context.Context, name string,
|
||||
fields map[string]interface{}) error {
|
||||
|
||||
collection := db.Database(mongoDBName).Collection(collectionAccount)
|
||||
collection := db.Database.Collection(collectionAccount)
|
||||
|
||||
filter := bson.M{"username": name}
|
||||
params := bson.M{}
|
||||
@@ -150,7 +146,7 @@ func (db *mongodb) UpdateAccount(ctx context.Context, name string,
|
||||
|
||||
// InsertSerie 创建专题
|
||||
func (db *mongodb) InsertSerie(ctx context.Context, serie *model.Serie) error {
|
||||
collection := db.Database(mongoDBName).Collection(collectionSerie)
|
||||
collection := db.Database.Collection(collectionSerie)
|
||||
|
||||
serie.ID = db.nextValue(ctx, counterNameSerie)
|
||||
_, err := collection.InsertOne(ctx, serie)
|
||||
@@ -159,7 +155,7 @@ func (db *mongodb) InsertSerie(ctx context.Context, serie *model.Serie) error {
|
||||
|
||||
// RemoveSerie 删除专题
|
||||
func (db *mongodb) RemoveSerie(ctx context.Context, id int) error {
|
||||
collection := db.Database(mongoDBName).Collection(collectionSerie)
|
||||
collection := db.Database.Collection(collectionSerie)
|
||||
|
||||
filter := bson.M{"id": id}
|
||||
_, err := collection.DeleteOne(ctx, filter)
|
||||
@@ -170,7 +166,7 @@ func (db *mongodb) RemoveSerie(ctx context.Context, id int) error {
|
||||
func (db *mongodb) UpdateSerie(ctx context.Context, id int,
|
||||
fields map[string]interface{}) error {
|
||||
|
||||
collection := db.Database(mongoDBName).Collection(collectionSerie)
|
||||
collection := db.Database.Collection(collectionSerie)
|
||||
|
||||
filter := bson.M{"id": id}
|
||||
params := bson.M{}
|
||||
@@ -184,7 +180,7 @@ func (db *mongodb) UpdateSerie(ctx context.Context, id int,
|
||||
|
||||
// LoadAllSerie 查询所有专题
|
||||
func (db *mongodb) LoadAllSerie(ctx context.Context) (model.SortedSeries, error) {
|
||||
collection := db.Database(mongoDBName).Collection(collectionSerie)
|
||||
collection := db.Database.Collection(collectionSerie)
|
||||
|
||||
opts := options.Find().SetSort(bson.M{"id": -1})
|
||||
filter := bson.M{}
|
||||
@@ -218,14 +214,14 @@ func (db *mongodb) InsertArticle(ctx context.Context, article *model.Article, st
|
||||
}
|
||||
}
|
||||
|
||||
collection := db.Database(mongoDBName).Collection(collectionArticle)
|
||||
collection := db.Database.Collection(collectionArticle)
|
||||
_, err := collection.InsertOne(ctx, article)
|
||||
return err
|
||||
}
|
||||
|
||||
// RemoveArticle 硬删除文章
|
||||
func (db *mongodb) RemoveArticle(ctx context.Context, id int) error {
|
||||
collection := db.Database(mongoDBName).Collection(collectionArticle)
|
||||
collection := db.Database.Collection(collectionArticle)
|
||||
|
||||
filter := bson.M{"id": id}
|
||||
_, err := collection.DeleteOne(ctx, filter)
|
||||
@@ -234,7 +230,7 @@ func (db *mongodb) RemoveArticle(ctx context.Context, id int) error {
|
||||
|
||||
// CleanArticles 清理回收站文章
|
||||
func (db *mongodb) CleanArticles(ctx context.Context, exp time.Time) error {
|
||||
collection := db.Database(mongoDBName).Collection(collectionArticle)
|
||||
collection := db.Database.Collection(collectionArticle)
|
||||
|
||||
// 超过两天自动删除
|
||||
filter := bson.M{"deleted_at": bson.M{"$gt": time.Time{}, "$lt": exp}}
|
||||
@@ -246,7 +242,7 @@ func (db *mongodb) CleanArticles(ctx context.Context, exp time.Time) error {
|
||||
func (db *mongodb) UpdateArticle(ctx context.Context, id int,
|
||||
fields map[string]interface{}) error {
|
||||
|
||||
collection := db.Database(mongoDBName).Collection(collectionArticle)
|
||||
collection := db.Database.Collection(collectionArticle)
|
||||
|
||||
filter := bson.M{"id": id}
|
||||
params := bson.M{}
|
||||
@@ -260,7 +256,7 @@ func (db *mongodb) UpdateArticle(ctx context.Context, id int,
|
||||
|
||||
// LoadArticle 查找文章
|
||||
func (db *mongodb) LoadArticle(ctx context.Context, id int) (*model.Article, error) {
|
||||
collection := db.Database(mongoDBName).Collection(collectionArticle)
|
||||
collection := db.Database.Collection(collectionArticle)
|
||||
|
||||
filter := bson.M{"id": id}
|
||||
result := collection.FindOne(ctx, filter)
|
||||
@@ -276,7 +272,7 @@ func (db *mongodb) LoadArticle(ctx context.Context, id int) (*model.Article, err
|
||||
// LoadArticleList 获取文章列表
|
||||
func (db *mongodb) LoadArticleList(ctx context.Context, search SearchArticles) (
|
||||
model.SortedArticles, int, error) {
|
||||
collection := db.Database(mongoDBName).Collection(collectionArticle)
|
||||
collection := db.Database.Collection(collectionArticle)
|
||||
|
||||
filter := bson.M{}
|
||||
for k, v := range search.Fields {
|
||||
@@ -334,7 +330,7 @@ type counter struct {
|
||||
|
||||
// nextValue counter value
|
||||
func (db *mongodb) nextValue(ctx context.Context, name string) int {
|
||||
collection := db.Database(mongoDBName).Collection(collectionCounter)
|
||||
collection := db.Database.Collection(collectionCounter)
|
||||
|
||||
opts := options.FindOneAndUpdate().SetUpsert(true).
|
||||
SetReturnDocument(options.After)
|
||||
|
||||
@@ -9,8 +9,8 @@ import (
|
||||
"github.com/eiblog/eiblog/cmd/eiblog/handler/file"
|
||||
"github.com/eiblog/eiblog/cmd/eiblog/handler/pages"
|
||||
"github.com/eiblog/eiblog/cmd/eiblog/handler/swag"
|
||||
|
||||
"github.com/eiblog/eiblog/pkg/middleware"
|
||||
"github.com/eiblog/eiblog/tools"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sirupsen/logrus"
|
||||
@@ -36,7 +36,7 @@ func runHTTPServer(endRun chan error) {
|
||||
middleware.SessionOpts{
|
||||
Name: "su",
|
||||
Secure: config.Conf.RunMode.IsReleaseMode(),
|
||||
Secret: []byte("ZGlzvcmUoMTAsICI="),
|
||||
Secret: tools.CryptoRand(16),
|
||||
}))
|
||||
|
||||
// swag
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
services:
|
||||
mongodb:
|
||||
image: mongo:3.2
|
||||
image: mongo:3.6
|
||||
volumes:
|
||||
- ${PWD}/mgodb:/data/db
|
||||
restart: always
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
const (
|
||||
RunModeLocal RunMode = "local" // 本地环境
|
||||
RunModeDev RunMode = "dev" // 开发环境
|
||||
RunModeProd RunMode = "pro" // 生产环境
|
||||
RunModeProd RunMode = "prod" // 生产环境
|
||||
)
|
||||
|
||||
// RunMode 运行模式
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/eiblog/eiblog/pkg/config"
|
||||
|
||||
@@ -16,7 +15,7 @@ import (
|
||||
// NewMDB new mongodb
|
||||
// https://docs.mongodb.com/manual/reference/connection-string/
|
||||
// mongodb://db0.example.com,db1.example.com,db2.example.com/dbname?replicaSet=myRepl&w=majority&wtimeoutMS=5000
|
||||
func NewMDB(opts config.Database) (*mongo.Database, error) {
|
||||
func NewMDB(ctx context.Context, opts config.Database) (*mongo.Database, error) {
|
||||
if opts.Driver != "mongodb" {
|
||||
return nil, errors.New("db: driver must be mongodb, but " + opts.Driver)
|
||||
}
|
||||
@@ -29,8 +28,6 @@ func NewMDB(opts config.Database) (*mongo.Database, error) {
|
||||
return nil, errors.New("db: please specify a database")
|
||||
}
|
||||
database = database[1:]
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
|
||||
defer cancel()
|
||||
|
||||
// remove database
|
||||
u.Path = ""
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -128,3 +129,14 @@ func IgnoreHTMLTag(src string) string {
|
||||
// 去除换行符
|
||||
return regexpEnter.ReplaceAllString(src, "")
|
||||
}
|
||||
|
||||
// CryptoRand random with crypto/rand
|
||||
func CryptoRand(byteLen int) []byte {
|
||||
buf := make([]byte, byteLen)
|
||||
|
||||
_, err := rand.Read(buf)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("rand: error reading random bytes: %s", err))
|
||||
}
|
||||
return buf
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user