113 lines
3.1 KiB
Go
113 lines
3.1 KiB
Go
package controller
|
|
|
|
import (
|
|
"Blog/bootstrap"
|
|
"Blog/internal/controller/adm_controller"
|
|
"Blog/internal/controller/cli_controller"
|
|
"Blog/internal/middleware"
|
|
"Blog/internal/utils"
|
|
"net/http"
|
|
"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"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func Router() {
|
|
profile := bootstrap.Config.Profile
|
|
//
|
|
app := iris.Default()
|
|
//
|
|
app.FallbackView(iris.FallbackView("404.html"))
|
|
//配置资源文件位置
|
|
app.HandleDir("/assets", iris.Dir("./assets"))
|
|
//配置模板文件位置
|
|
tmpl := iris.HTML("./template", ".html")
|
|
//模板占位符
|
|
tmpl.Delims("#{", "}")
|
|
//是否每次都加载模板文件,默认否
|
|
if profile == "dev" {
|
|
tmpl.Reload(true)
|
|
}
|
|
app.RegisterView(tmpl)
|
|
|
|
app.OnErrorCode(http.StatusNotFound, func(ctx iris.Context) {
|
|
ctx.View("404.html")
|
|
})
|
|
app.OnErrorCode(http.StatusInternalServerError, func(ctx iris.Context) {
|
|
err := ctx.GetErr()
|
|
logrus.Info(err)
|
|
ctx.View("500.html")
|
|
})
|
|
|
|
configSession(app)
|
|
|
|
// app.Get("/", func(ctx iris.Context) {
|
|
// ctx.View("index.html")
|
|
// })
|
|
|
|
mvc.Configure(app.Party("/"), func(m *mvc.Application) {
|
|
m.Party("/").Handle(new(cli_controller.IndexController))
|
|
m.Party("/article").Handle(new(cli_controller.ArticleController))
|
|
m.Party("/diary").Handle(new(cli_controller.DiaryController))
|
|
m.Party("/file").Handle(new(cli_controller.FileController))
|
|
m.Party("/admin/login").Handle(new(adm_controller.LoginController))
|
|
m.Router.Get("/logout", func(ctx iris.Context) {
|
|
session := sessions.Get(ctx)
|
|
user := utils.SessionUtil.GetUser(session)
|
|
if user.Id != "" {
|
|
logrus.Info("[", user.Username, "]退出登录,清除Session")
|
|
session.Man.Destroy(ctx)
|
|
ctx.Redirect("")
|
|
return
|
|
}
|
|
logrus.Error("当前用户没有登录状态,无法退出登录")
|
|
})
|
|
|
|
})
|
|
|
|
mvc.Configure(app.Party("/admin"), func(m *mvc.Application) {
|
|
m.Router.Use(middleware.Auth)
|
|
m.Party("/").Handle(new(adm_controller.AdminController))
|
|
m.Party("/article").Handle(new(adm_controller.ArticleController))
|
|
m.Party("/diary").Handle(new(adm_controller.DiaryController))
|
|
m.Party("/version").Handle(new(adm_controller.VersionController))
|
|
m.Party("/file").Handle(new(adm_controller.FileController))
|
|
m.Party("/user").Handle(new(adm_controller.UserController))
|
|
|
|
})
|
|
|
|
app.Listen("localhost:8080")
|
|
|
|
}
|
|
|
|
func configSession(app *iris.Application) {
|
|
addr := bootstrap.Config.Iris.Session.Address
|
|
redisDatabase := bootstrap.Config.Iris.Session.Db
|
|
prefix := bootstrap.Config.Iris.Session.Prefix
|
|
expires := bootstrap.Config.Iris.Session.Expires
|
|
db := redis.New(redis.Config{
|
|
Network: "tcp",
|
|
Addr: addr,
|
|
Timeout: time.Duration(10) * time.Second,
|
|
MaxActive: 10,
|
|
Username: "",
|
|
Database: redisDatabase,
|
|
Prefix: prefix,
|
|
Driver: redis.GoRedis(), // defaults to this driver.
|
|
})
|
|
|
|
// using the standard `html/template` package.
|
|
sess := sessions.New(sessions.Config{
|
|
Cookie: "session_id_cookie",
|
|
Expires: time.Duration(expires) * time.Hour,
|
|
AllowReclaim: true,
|
|
})
|
|
|
|
sess.UseDatabase(db)
|
|
app.Use(sess.Handler(), middleware.SessionViewData)
|
|
}
|