mirror of
https://github.com/yv1ing/MollyBlog.git
synced 2025-09-16 14:53:45 +08:00
new: Improve the automatic update module, add authentication, and fix some bugs
This commit is contained in:
@@ -1,6 +1,13 @@
|
|||||||
# app config #
|
# app config #
|
||||||
host: 0.0.0.0
|
host: 0.0.0.0
|
||||||
port: 8080
|
port: 8080
|
||||||
|
cache_path: tmp
|
||||||
|
|
||||||
|
# use this secret to update your posts
|
||||||
|
update_endpoint: /update
|
||||||
|
update_secret: 123456
|
||||||
|
|
||||||
|
# configure object storage to automatically obtain article updates
|
||||||
storage:
|
storage:
|
||||||
src: _post/src
|
src: _post/src
|
||||||
dst: _post/dst
|
dst: _post/dst
|
||||||
@@ -12,6 +19,8 @@ storage:
|
|||||||
secret_id:
|
secret_id:
|
||||||
secret_key:
|
secret_key:
|
||||||
save_path:
|
save_path:
|
||||||
|
|
||||||
|
# website template directory
|
||||||
template: templates/default
|
template: templates/default
|
||||||
|
|
||||||
# site config #
|
# site config #
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ type MConfig struct {
|
|||||||
Port int `yaml:"port"`
|
Port int `yaml:"port"`
|
||||||
Storage mStorage `yaml:"storage"`
|
Storage mStorage `yaml:"storage"`
|
||||||
Template string `yaml:"template"`
|
Template string `yaml:"template"`
|
||||||
|
UpdateEndpoint string `yaml:"update_endpoint"`
|
||||||
|
UpdateSecret string `yaml:"update_secret"`
|
||||||
|
CachePath string `yaml:"cache_path"`
|
||||||
|
|
||||||
MSite mSite `yaml:"site"`
|
MSite mSite `yaml:"site"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
package mApp
|
package mApp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"MollyBlog/config"
|
|
||||||
"MollyBlog/internal/model"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"MollyBlog/config"
|
||||||
|
"MollyBlog/internal/model"
|
||||||
|
|
||||||
"github.com/88250/lute"
|
"github.com/88250/lute"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -88,7 +90,7 @@ func NewMApp(cfg *config.MConfig) *MApp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// resetStorage before each update, delete the cache
|
// resetStorage before each update, delete the cache
|
||||||
func (ma *MApp) resetStorage() {
|
func (ma *MApp) resetStorage() error {
|
||||||
ma.Posts = nil
|
ma.Posts = nil
|
||||||
ma.Tags = nil
|
ma.Tags = nil
|
||||||
ma.Categories = nil
|
ma.Categories = nil
|
||||||
@@ -103,4 +105,42 @@ func (ma *MApp) resetStorage() {
|
|||||||
ma.CategoriesCount = make(map[string]int)
|
ma.CategoriesCount = make(map[string]int)
|
||||||
ma.TaggedPosts = make(map[string][]*model.MPost)
|
ma.TaggedPosts = make(map[string][]*model.MPost)
|
||||||
ma.CategorizedPosts = make(map[string][]*model.MPost)
|
ma.CategorizedPosts = make(map[string][]*model.MPost)
|
||||||
|
|
||||||
|
if ma.searcher != nil {
|
||||||
|
ma.searcher.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
err = os.RemoveAll(ma.Config.Storage.SRC)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = os.Stat(ma.Config.Storage.SRC)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
err = os.MkdirAll(ma.Config.Storage.SRC, os.ModePerm)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.RemoveAll(ma.Config.Storage.DST)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = os.Stat(ma.Config.Storage.DST)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
err = os.MkdirAll(ma.Config.Storage.DST, os.ModePerm)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.RemoveAll(ma.Config.CachePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -404,7 +404,12 @@ func (ma *MApp) SearchHandler(ctx *gin.Context) {
|
|||||||
|
|
||||||
func (ma *MApp) UpdateBlogHandler(ctx *gin.Context) {
|
func (ma *MApp) UpdateBlogHandler(ctx *gin.Context) {
|
||||||
var err error
|
var err error
|
||||||
ma.resetStorage()
|
err = ma.resetStorage()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("reset storage error: %v\n", err)
|
||||||
|
_ = ctx.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
err = ma.loadMarkdownFiles()
|
err = ma.loadMarkdownFiles()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package mApp
|
package mApp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"MollyBlog/internal/storage"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -9,6 +8,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"MollyBlog/internal/model"
|
"MollyBlog/internal/model"
|
||||||
|
"MollyBlog/internal/storage"
|
||||||
"MollyBlog/utils"
|
"MollyBlog/utils"
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
|
|||||||
21
internal/mApp/mMiddleware.go
Normal file
21
internal/mApp/mMiddleware.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package mApp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"MollyBlog/config"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (ma *MApp) AuthMiddleware(ctx *gin.Context) {
|
||||||
|
if ctx.Request.URL.Path == config.MConfigInstance.UpdateEndpoint {
|
||||||
|
secret := ctx.GetHeader("molly-secret")
|
||||||
|
if secret != config.MConfigInstance.UpdateSecret {
|
||||||
|
ctx.AbortWithStatus(http.StatusForbidden)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.Next()
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
package mApp
|
package mApp
|
||||||
|
|
||||||
func (ma *MApp) loadRoutes() {
|
func (ma *MApp) loadRoutes() {
|
||||||
|
ma.engine.Use(ma.AuthMiddleware)
|
||||||
|
|
||||||
ma.engine.GET("/", ma.IndexHandler)
|
ma.engine.GET("/", ma.IndexHandler)
|
||||||
ma.engine.GET("/search", ma.SearchHandler)
|
ma.engine.GET("/search", ma.SearchHandler)
|
||||||
ma.engine.GET("/archive", ma.ArchiveHandler)
|
ma.engine.GET("/archive", ma.ArchiveHandler)
|
||||||
@@ -8,5 +10,6 @@ func (ma *MApp) loadRoutes() {
|
|||||||
ma.engine.GET("/tag/:hash", ma.TagHandler)
|
ma.engine.GET("/tag/:hash", ma.TagHandler)
|
||||||
ma.engine.GET("/category/:hash", ma.CategoryHandler)
|
ma.engine.GET("/category/:hash", ma.CategoryHandler)
|
||||||
|
|
||||||
ma.engine.PUT("/update", ma.UpdateBlogHandler)
|
// update's endpoint
|
||||||
|
ma.engine.POST(ma.Config.UpdateEndpoint, ma.UpdateBlogHandler)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package mApp
|
package mApp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
@@ -9,13 +10,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (ma *MApp) loadPostIndex() {
|
func (ma *MApp) loadPostIndex() {
|
||||||
|
cachePath := fmt.Sprintf("%s/persistent", ma.Config.CachePath)
|
||||||
|
ma.searcher = nil
|
||||||
ma.searcher = &engine.Engine{}
|
ma.searcher = &engine.Engine{}
|
||||||
|
|
||||||
ma.searcher.Init(types.EngineInitOptions{
|
ma.searcher.Init(types.EngineInitOptions{
|
||||||
UsePersistentStorage: true,
|
UsePersistentStorage: true,
|
||||||
PersistentStorageFolder: "tmp",
|
PersistentStorageFolder: cachePath,
|
||||||
StopTokenFile: "data/stop_tokens.txt",
|
StopTokenFile: "data/search/stop_tokens.txt",
|
||||||
SegmenterDictionaries: "data/dictionary.txt",
|
SegmenterDictionaries: "data/search/dictionary.txt",
|
||||||
IndexerInitOptions: &types.IndexerInitOptions{
|
IndexerInitOptions: &types.IndexerInitOptions{
|
||||||
IndexType: types.LocationsIndex,
|
IndexType: types.LocationsIndex,
|
||||||
},
|
},
|
||||||
@@ -26,6 +29,7 @@ func (ma *MApp) loadPostIndex() {
|
|||||||
postData, _ := io.ReadAll(postFile)
|
postData, _ := io.ReadAll(postFile)
|
||||||
|
|
||||||
ma.searcher.IndexDocument(post.Index, types.DocumentIndexData{Content: string(postData)}, false)
|
ma.searcher.IndexDocument(post.Index, types.DocumentIndexData{Content: string(postData)}, false)
|
||||||
|
_ = postFile.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
ma.searcher.FlushIndex()
|
ma.searcher.FlushIndex()
|
||||||
|
|||||||
@@ -53,7 +53,6 @@ func CosLoadMarkdowns(config config.MConfig, localDir string) error {
|
|||||||
prefix := config.Storage.COS.SavePath
|
prefix := config.Storage.COS.SavePath
|
||||||
encodingType := "url"
|
encodingType := "url"
|
||||||
|
|
||||||
log.Println("start load markdown files")
|
|
||||||
isTruncated := true
|
isTruncated := true
|
||||||
for isTruncated {
|
for isTruncated {
|
||||||
opt := &cos.BucketGetOptions{
|
opt := &cos.BucketGetOptions{
|
||||||
|
|||||||
11
main.go
11
main.go
@@ -9,17 +9,20 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
_, err := os.Stat(mApp.SRC)
|
src := config.MConfigInstance.Storage.SRC
|
||||||
|
dst := config.MConfigInstance.Storage.DST
|
||||||
|
|
||||||
|
_, err := os.Stat(src)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
err = os.MkdirAll(mApp.SRC, os.ModePerm)
|
err = os.MkdirAll(src, os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = os.Stat(mApp.DST)
|
_, err = os.Stat(dst)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
err = os.MkdirAll(mApp.DST, os.ModePerm)
|
err = os.MkdirAll(dst, os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user