98 lines
2.5 KiB
Go
98 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"BlogFile/bootstrap"
|
|
"BlogFile/cloud"
|
|
"BlogFile/internal/controller"
|
|
"BlogFile/internal/controller/admin"
|
|
"BlogFile/internal/middleware"
|
|
"BlogFile/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"
|
|
redisSession "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"))
|
|
// Set custom delimeters.
|
|
if profile != "dev" {
|
|
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 := redisSession.New(redisSession.Config{
|
|
Network: "tcp",
|
|
Addr: addr,
|
|
Timeout: time.Duration(10) * time.Second,
|
|
MaxActive: 10,
|
|
Username: "",
|
|
Database: redisDatabase,
|
|
Prefix: prefix,
|
|
Driver: redisSession.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)
|
|
|
|
app.Get("/", func(ctx iris.Context) {
|
|
ctx.View("index.html")
|
|
})
|
|
fileApi := app.Party("/file")
|
|
|
|
adminFileApi := app.Party("/admin/file")
|
|
adminFileApi.Use(middleware.Auth)
|
|
|
|
mvc.New(fileApi).Handle(new(controller.FileController))
|
|
mvc.New(adminFileApi).Handle(new(admin.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()
|
|
}()
|
|
}
|