Esempio n. 1
0
func main() {
	defer log.Logger.Close()
	defer db.Close()

	log.Logger.Info("starting server...")

	//==================================   nav top sidebar  ======================================
	{
		g.Any("/sidebar.html", nav.SidebarTemplateHandler)
		g.Any("/header.html", nav.HeaderTemplateHandler)
		g.Any("/footer.html", nav.FooterTemplateHandler)
	}

	//==================================   404, index  ======================================
	{
		//404
		g.NoRoute(func(c *gin.Context) {
			log.Logger.Debug("page [%v] not found, redirect to 404.html", c.Request.URL.Path)
			if strings.HasSuffix(c.Request.URL.Path, ".html") {
				c.HTML(http.StatusNotFound, "404.tmpl", nil)
			} else {
				r := render.New(c)
				r.JSON(util.JsonResult{Msg: "请求资源不存在"})
			}
		})

		//首页
		g.Any("/", index.IndexHandler)
	}

	log.Logger.Info("server started ")
	g.Run(":8085")
}
Esempio n. 2
0
func HandlerError() gin.HandlerFunc {
	return func(c *gin.Context) {
		defer func() {
			if err := recover(); err != nil {
				trace := make([]byte, 10240)
				runtime.Stack(trace, true)
				log.Logger.Error("%s, \n%s", err, trace)

				if strings.HasSuffix(c.Request.URL.Path, ".html") {
					c.HTML(http.StatusInternalServerError, "500.tmpl", nil)
				} else {
					r := render.New(c)
					r.JSON(util.JsonResult{Msg: "系统错误"})
				}
			}
		}()
		c.Next()
	}
}