122 lines
3.1 KiB
Go
122 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"Blog/bootstrap"
|
|
"Blog/internal/client"
|
|
"Blog/internal/consts"
|
|
"Blog/internal/controller"
|
|
"Blog/internal/model"
|
|
"Blog/internal/repository"
|
|
"Blog/internal/service"
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
"github.com/sirupsen/logrus"
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
func main() {
|
|
profile := flag.String("profile", "dev", "启动的配置文件")
|
|
flag.Parse()
|
|
inits(*profile)
|
|
|
|
runServer()
|
|
|
|
defer func() {
|
|
logrus.Info("程序结束,关闭资源")
|
|
// db.Close()
|
|
client.RedisClient().Close()
|
|
}()
|
|
}
|
|
|
|
func inits(profile string) {
|
|
//日志文件位置
|
|
f, err := os.OpenFile("./Iris.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
|
|
if err != nil {
|
|
panic("日志文件错误")
|
|
}
|
|
//控制台和日志文件都输出
|
|
iw := io.MultiWriter(f, os.Stdout)
|
|
//日志设置
|
|
// logrus.SetFormatter(log.Ldate | log.Ltime | log.Lmicroseconds | log.Ltime | log.Llongfile)
|
|
logrus.SetOutput(iw)
|
|
logrus.SetFormatter(&logrus.TextFormatter{
|
|
ForceColors: true,
|
|
FullTimestamp: true,
|
|
TimestampFormat: "2006-01-02 15:04:05.000",
|
|
})
|
|
logrus.SetReportCaller(true)
|
|
logrus.Info("init bootstrap")
|
|
filePath := fmt.Sprintf("conf/bootstrap-%v.yaml", profile)
|
|
logrus.Info("当前Profile文件位置:", filePath)
|
|
b, err := os.ReadFile(filePath)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
err3 := yaml.Unmarshal(b, &bootstrap.Config)
|
|
if err3 != nil {
|
|
panic(err3)
|
|
}
|
|
|
|
// profileName := bootstrap.Config.Profile
|
|
logrus.Info("当前Profile:", profile)
|
|
if profile == "dev" {
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
}
|
|
|
|
//初始化相关数据的配置
|
|
InitData()
|
|
}
|
|
|
|
func runServer() {
|
|
port := bootstrap.Config.Iris.Port
|
|
controller.Router(port)
|
|
}
|
|
|
|
func InitData() {
|
|
initArticleData()
|
|
service.DiaryService.RefreshLatest()
|
|
initContentData()
|
|
initViewRecordData()
|
|
}
|
|
|
|
func initArticleData() {
|
|
var slices []model.BlogArticle = repository.ArticleRepository().FindList(nil, "state = ?", consts.ARTICLE_STATE_PUBLISH)
|
|
logrus.Info("文章初始化数据加载量:", len(slices))
|
|
ctx := context.Background()
|
|
for _, article := range slices {
|
|
// logrus.Info(article)
|
|
publishTime := article.PublishTime
|
|
err := client.RedisClient().ZAdd(ctx, consts.REDIS_BLOG_ARTICLE_LATEST, redis.Z{Score: float64(publishTime), Member: &article}).Err()
|
|
if err != nil {
|
|
logrus.Info(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func initContentData() {
|
|
var slices []model.BlogTextContent = repository.TextContentRepository().FindList(nil, "state = ?", consts.CONTENT_STATE_PUBLISH)
|
|
logrus.Info("大文本初始化数据加载量:", len(slices))
|
|
for _, content := range slices {
|
|
client.RedisClient().Set(context.Background(), consts.REDIS_BLOG_CONTENT+content.RelId, &content, time.Duration(0))
|
|
}
|
|
}
|
|
|
|
func initViewRecordData() {
|
|
type viewRecord struct {
|
|
RelId string
|
|
Counts int
|
|
}
|
|
var slices []viewRecord
|
|
repository.ViewRecordRep().Table(consts.TABLE_BLOG_VIEW_RECORD).Select("rel_id ,count(1) counts").Group("rel_id").Find(&slices)
|
|
logrus.Info("点击量初始化数据加载量:", len(slices))
|
|
for _, val := range slices {
|
|
client.RedisClient().HSet(context.Background(), consts.REDIS_BLOG_VIEW_RECORD, map[string]any{val.RelId: val.Counts})
|
|
}
|
|
}
|