136 lines
3.8 KiB
Go
136 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"BlogAdmin/bootstrap"
|
|
"BlogAdmin/cloud"
|
|
"BlogAdmin/internal/controller"
|
|
"BlogAdmin/internal/middleware"
|
|
"BlogAdmin/third_party/database"
|
|
"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() {
|
|
profile := os.Getenv("profile")
|
|
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Ltime | 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("日志初始化失败")
|
|
}
|
|
app.Logger().SetOutput(logFile)
|
|
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")
|
|
})
|
|
|
|
addr := bootstrap.Config.Session.Addr
|
|
redisDatabase := bootstrap.Config.Session.Database
|
|
prefix := bootstrap.Config.Session.Prefix
|
|
expires := bootstrap.Config.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.
|
|
})
|
|
|
|
// 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.Duration(expires) * time.Hour,
|
|
AllowReclaim: true,
|
|
})
|
|
sess.UseDatabase(db)
|
|
app.Use(sess.Handler(), middleware.SessionViewData)
|
|
app.RegisterView(tmpl)
|
|
|
|
app.Get("/", func(ctx iris.Context) {
|
|
ctx.View("index.html")
|
|
})
|
|
|
|
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(controller.LoginController))
|
|
mvc.New(adminApi).Handle(new(controller.AdminController))
|
|
mvc.New(adminArticleApi).Handle(new(controller.ArticleController))
|
|
mvc.New(adminDinaryApi).Handle(new(controller.DiaryController))
|
|
mvc.New(adminVersionApi).Handle(new(controller.VersionController))
|
|
mvc.New(adminUserApi).Handle(new(controller.UserController))
|
|
mvc.New(adminFileApi).Handle(new(controller.FileController))
|
|
|
|
go cloud.DistributedService.ServiceUp()
|
|
|
|
host := bootstrap.Config.Cloud.Host
|
|
app.Listen(host)
|
|
|
|
defer func() {
|
|
log.Println("程序结束,关闭资源")
|
|
cloud.DistributedService.ServiceDown()
|
|
db.Close()
|
|
database.RedisTemplate.Close()
|
|
}()
|
|
}
|