149 lines
4.2 KiB
Go
149 lines
4.2 KiB
Go
package main
|
|
|
|
import (
|
|
"blog/internal/controller"
|
|
"blog/internal/controller/admin"
|
|
"blog/internal/middleware"
|
|
"blog/internal/service"
|
|
"blog/third_party/database"
|
|
"context"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/kataras/iris/v12"
|
|
"github.com/kataras/iris/v12/mvc"
|
|
"github.com/kataras/iris/v12/sessions"
|
|
"github.com/kataras/iris/v12/sessions/sessiondb/redis"
|
|
)
|
|
|
|
// func main() {
|
|
// i := time.Now().UnixMilli()
|
|
// log.Println(i)
|
|
// b, _ := bcrypt.GenerateFromPassword([]byte("123456"), bcrypt.DefaultCost)
|
|
// log.Println(string(b))
|
|
// }
|
|
|
|
func main() {
|
|
profile := os.Getenv("profile")
|
|
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.LUTC | log.Llongfile)
|
|
app := iris.Default()
|
|
app.FallbackView(iris.FallbackView("404.html"))
|
|
app.HandleDir("/assets", iris.Dir("./assets"))
|
|
tmpl := iris.HTML("./template", ".html")
|
|
// Set custom delimeters.
|
|
tmpl.Delims("#{", "}")
|
|
tmpl.Reload(true)
|
|
if profile != "dev" {
|
|
tmpl.Reload(false)
|
|
logFile, err := os.OpenFile("./iris.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
|
|
if err != nil {
|
|
panic("日志初始化失败")
|
|
}
|
|
log.SetOutput(logFile)
|
|
profile = "prod"
|
|
}
|
|
log.Println("Go Iris 启动")
|
|
log.Println("当前环境是:", profile)
|
|
|
|
app.OnErrorCode(http.StatusNotFound, func(ctx iris.Context) {
|
|
// ctx.StatusCode(http.StatusNotFound)
|
|
// s := ctx.Path()
|
|
// log.Println(s)
|
|
ctx.View("404.html")
|
|
})
|
|
app.OnErrorCode(http.StatusInternalServerError, func(ctx iris.Context) {
|
|
err := ctx.GetErr()
|
|
log.Println(err)
|
|
ctx.View("500.html")
|
|
})
|
|
|
|
db := redis.New(redis.Config{
|
|
Network: "tcp",
|
|
Addr: "localhost:6379",
|
|
Timeout: time.Duration(30) * time.Second,
|
|
MaxActive: 10,
|
|
Username: "",
|
|
Database: "0",
|
|
Prefix: "iris:session:",
|
|
Driver: redis.GoRedis(), // defaults to this driver.
|
|
// To set a custom, existing go-redis client, use the "SetClient" method:
|
|
// Driver: redis.GoRedis().SetClient(customGoRedisClient)
|
|
})
|
|
|
|
// sess := sessions.New(sessions.Config{
|
|
// Cookie: "_session_id",
|
|
// Expires: time.Hour * 2,
|
|
// DisableSubdomainPersistence: false,
|
|
// AllowReclaim: true,
|
|
// /*
|
|
// SessionIDGenerator: func(ctx iris.Context) string {
|
|
// id:= ctx.GetHeader("X-Session-Id")
|
|
// if id == "" {
|
|
// id = // [generate ID here and set the header]
|
|
// ctx.Header("X-Session-Id", id)
|
|
// }
|
|
// return id
|
|
// },
|
|
// */
|
|
// })
|
|
|
|
// using the standard `html/template` package.
|
|
sess := sessions.New(sessions.Config{
|
|
Cookie: "session_id_cookie",
|
|
Expires: time.Hour * 10,
|
|
AllowReclaim: true,
|
|
})
|
|
sess.UseDatabase(db)
|
|
app.Use(sess.Handler(), middleware.Record, middleware.SessionViewData)
|
|
app.RegisterView(tmpl)
|
|
|
|
app.Get("/", func(ctx iris.Context) {
|
|
ctx.View("index.html")
|
|
})
|
|
articleApi := app.Party("/article")
|
|
diaryApi := app.Party("/diary")
|
|
fileApi := app.Party("/file")
|
|
|
|
mvc.New(articleApi).Handle(new(controller.ArticleController))
|
|
mvc.New(diaryApi).Handle(new(controller.DiaryController))
|
|
mvc.New(fileApi).Handle(new(controller.FileController))
|
|
|
|
adminApi := app.Party("/admin")
|
|
adminLoginApi := app.Party("/admin/login")
|
|
adminArticleApi := app.Party("/admin/article")
|
|
adminDinaryApi := app.Party("/admin/diary")
|
|
adminVersionApi := app.Party("/admin/version")
|
|
adminUserApi := app.Party("/admin/user")
|
|
adminFileApi := app.Party("/admin/file")
|
|
|
|
adminApi.Use(middleware.Auth)
|
|
adminArticleApi.Use(middleware.Auth)
|
|
adminDinaryApi.Use(middleware.Auth)
|
|
adminVersionApi.Use(middleware.Auth)
|
|
adminUserApi.Use(middleware.Auth)
|
|
adminFileApi.Use(middleware.Auth)
|
|
|
|
mvc.New(adminLoginApi).Handle(new(admin.LoginController))
|
|
mvc.New(adminApi).Handle(new(admin.AdminController))
|
|
mvc.New(adminArticleApi).Handle(new(admin.ArticleController))
|
|
mvc.New(adminDinaryApi).Handle(new(admin.DiaryController))
|
|
mvc.New(adminVersionApi).Handle(new(admin.VersionController))
|
|
mvc.New(adminUserApi).Handle(new(admin.UserController))
|
|
mvc.New(adminFileApi).Handle(new(admin.FileController))
|
|
|
|
ctx := context.Background()
|
|
database.RedisTemplate.FlushAll(ctx)
|
|
go service.ArticleService.InitArticleData()
|
|
go service.DiaryService.InitDiaryData()
|
|
go service.ContentService.InitContentData()
|
|
|
|
app.Listen("localhost:8080")
|
|
|
|
defer func() {
|
|
db.Close()
|
|
database.RedisTemplate.Close()
|
|
}()
|
|
}
|