bootstrap.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * @description:web项目启动
  3. * @Author: CP
  4. * @Date: 2020-08-21 11:20:00
  5. * @FilePath: \construction_management\bootstrap\bootstrap.go
  6. */
  7. package bootstrap
  8. import (
  9. "time"
  10. "github.com/iris-contrib/middleware/csrf"
  11. "github.com/kataras/iris/v12"
  12. "github.com/kataras/iris/v12/middleware/logger"
  13. "github.com/kataras/iris/v12/middleware/recover"
  14. "go.mod/conf"
  15. )
  16. //配置器,定制化的配置--方法类型
  17. type Configurator func(*Bootstrapper)
  18. //GCJSXM88CP20200902666A477C084477
  19. // const csrfKey = "GCJSXM88CP20200902666A477C084477"
  20. //结构体扩展iris,类此于继承
  21. type Bootstrapper struct {
  22. *iris.Application
  23. AppName string
  24. AppOwner string
  25. AppSpawnDate time.Time
  26. }
  27. //新建和返回一个Bootstrapper
  28. func New(appName, appOwner string, cfgs ...Configurator) *Bootstrapper {
  29. b := &Bootstrapper{
  30. AppName: appName,
  31. AppOwner: appOwner,
  32. AppSpawnDate: time.Now(),
  33. Application: iris.New(),
  34. }
  35. //数组遍历 cfg方法
  36. for _, cfg := range cfgs {
  37. cfg(b)
  38. }
  39. return b
  40. }
  41. //Bootstrapper的方法 读取模板
  42. func (b *Bootstrapper) SetupViews(viewsDir string) {
  43. htmlEngine := iris.HTML(viewsDir, ".html").Layout("shared/layout.html")
  44. // 每次重新加载模版(线上关闭它)
  45. htmlEngine.Reload(true)
  46. // 给模版内置各种定制的方法
  47. htmlEngine.AddFunc("FromUnixtimeShort", func(t int) string {
  48. dt := time.Unix(int64(t), int64(0))
  49. return dt.Format(conf.SysTimeformShort)
  50. })
  51. htmlEngine.AddFunc("FromUnixtime", func(t int) string {
  52. dt := time.Unix(int64(t), int64(0))
  53. return dt.Format(conf.SysTimeform)
  54. })
  55. b.RegisterView(htmlEngine)
  56. }
  57. func (b *Bootstrapper) SetupCsrfHandlers(csrfKey string) {
  58. protect := csrf.Protect([]byte(csrfKey), csrf.FieldName("csrf"))
  59. csrf.Secure(false)
  60. b.Party("/", protect)
  61. }
  62. //处理异常--设置错误信息展示
  63. func (b *Bootstrapper) SetupErrorHandlers() {
  64. b.OnAnyErrorCode(func(ctx iris.Context) {
  65. err := iris.Map{
  66. "app": b.AppName,
  67. "status": ctx.GetStatusCode(),
  68. "message": ctx.Values().GetString("message"),
  69. }
  70. //如果是json格式,使用json方式输出
  71. if jsonOutput := ctx.URLParamExists("json"); jsonOutput {
  72. ctx.JSON(err)
  73. return
  74. }
  75. ctx.ViewData("Err", err)
  76. ctx.ViewData("Title", "Error")
  77. ctx.View("shared/error.html")
  78. })
  79. }
  80. //配置的方法 Configure accepts configurations and runs them inside the Bootstraper's context.
  81. func (b *Bootstrapper) Configure(cs ...Configurator) {
  82. for _, c := range cs {
  83. c(b)
  84. }
  85. }
  86. // 启动计划任务服务
  87. func (b *Bootstrapper) setupCron() {
  88. // TODO
  89. }
  90. const (
  91. StaticAssets = "./public"
  92. Favicon = "/favicon.ico"
  93. //20200902GCJSXM7C084477AEA06096F5
  94. //9AB0F421E53A477C084477AEA06096F5
  95. CsrfKey = "9AB0F421E53A477C084477AEA06096F5"
  96. )
  97. // 初始化Bootstrap
  98. // Returns itself.
  99. func (b *Bootstrapper) Bootstrap() *Bootstrapper {
  100. //设置模板
  101. b.SetupViews("./views")
  102. // b.SetupSessions(24*time.Hour,
  103. // []byte("the-big-and-secret-fash-key-here"),
  104. // []byte("lot-secret-of-characters-big-too"),
  105. // )
  106. // 设置csrf
  107. b.SetupCsrfHandlers(CsrfKey)
  108. //设置异常信息
  109. b.SetupErrorHandlers()
  110. // static files
  111. b.Favicon(StaticAssets + Favicon)
  112. //b.StaticWeb(StaticAssets[1:], StaticAssets)
  113. b.HandleDir(StaticAssets[1:], iris.Dir(StaticAssets))
  114. // indexHtml, err := ioutil.ReadFile(StaticAssets + "/index.html")
  115. // if err == nil {
  116. // b.StaticContent(StaticAssets[1:]+"/", "text/html",
  117. // indexHtml)
  118. // }
  119. // 不要把目录末尾"/"省略掉
  120. //iris.WithoutPathCorrectionRedirection(b.Application)
  121. // crontab
  122. b.setupCron()
  123. // middleware, after static files
  124. b.Use(recover.New())
  125. b.Use(logger.New())
  126. return b
  127. }
  128. func (b *Bootstrapper) Listen(addr string, cfgs ...iris.Configurator) {
  129. b.Run(iris.Addr(addr), cfgs...)
  130. }