2019-12-08 03:59:58 -05:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"time"
|
|
|
|
|
2019-12-09 05:14:31 -05:00
|
|
|
"code.cloudfoundry.org/bytefmt"
|
2019-12-08 03:59:58 -05:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
"github.com/p14yground/nezha/pkg/mygin"
|
|
|
|
"github.com/p14yground/nezha/service/dao"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ServeWeb ..
|
|
|
|
func ServeWeb() {
|
2019-12-08 10:18:29 -05:00
|
|
|
gin.SetMode(gin.ReleaseMode)
|
|
|
|
if dao.Conf.Debug {
|
|
|
|
gin.SetMode(gin.DebugMode)
|
|
|
|
}
|
2019-12-08 03:59:58 -05:00
|
|
|
r := gin.Default()
|
|
|
|
r.Use(mygin.RecordPath)
|
|
|
|
r.SetFuncMap(template.FuncMap{
|
|
|
|
"tf": func(t time.Time) string {
|
|
|
|
return t.Format("2006年1月2号")
|
|
|
|
},
|
2019-12-09 10:45:23 -05:00
|
|
|
"stf": func(s uint64) string {
|
|
|
|
return time.Unix(int64(s), 0).Format("2006年1月2号 15:04")
|
|
|
|
},
|
2019-12-08 03:59:58 -05:00
|
|
|
"fs": func() string {
|
|
|
|
if !dao.Conf.Debug {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%d", time.Now().UnixNano())
|
|
|
|
},
|
2019-12-09 10:45:23 -05:00
|
|
|
"sf": func(duration uint64) string {
|
|
|
|
return time.Duration(time.Duration(duration) * time.Second).String()
|
|
|
|
},
|
2019-12-09 05:14:31 -05:00
|
|
|
"bf": func(b uint64) string {
|
|
|
|
return bytefmt.ByteSize(b)
|
|
|
|
},
|
2019-12-08 03:59:58 -05:00
|
|
|
})
|
|
|
|
r.Static("/static", "resource/static")
|
|
|
|
r.LoadHTMLGlob("resource/template/**/*")
|
|
|
|
routers(r)
|
|
|
|
r.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func routers(r *gin.Engine) {
|
|
|
|
// 通用页面
|
|
|
|
cp := commonPage{r}
|
|
|
|
cp.serve()
|
|
|
|
// 游客页面
|
|
|
|
gp := guestPage{r}
|
|
|
|
gp.serve()
|
|
|
|
// 会员页面
|
|
|
|
mp := &memberPage{r}
|
|
|
|
mp.serve()
|
|
|
|
// API
|
|
|
|
api := r.Group("api")
|
|
|
|
{
|
|
|
|
ma := &memberAPI{api}
|
|
|
|
ma.serve()
|
|
|
|
}
|
|
|
|
}
|