You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
EasyDarwin/routers/routers.go

142 lines
3.6 KiB

6 years ago
package routers
import (
"fmt"
"log"
"mime"
"net/http"
"path/filepath"
"github.com/penggy/EasyGoLib/db"
"github.com/gin-contrib/pprof"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/penggy/EasyGoLib/utils"
"github.com/penggy/cors"
"github.com/penggy/sessions"
validator "gopkg.in/go-playground/validator.v8"
)
/**
* @apiDefine simpleSuccess
* @apiSuccessExample
* HTTP/1.1 200 OK
*/
/**
* @apiDefine authError
* @apiErrorExample
* HTTP/1.1 401 access denied
*/
/**
* @apiDefine pageParam
* @apiParam {Number} start ,
* @apiParam {Number} limit
* @apiParam {String} [sort]
* @apiParam {String=ascending,descending} [order]
* @apiParam {String} [q]
*/
/**
* @apiDefine pageSuccess
* @apiSuccess (200) {Number} total
* @apiSuccess (200) {Array} rows
*/
/**
* @apiDefine timeInfo
* @apiSuccess (200) {String} rows.createAt , YYYY-MM-DD HH:mm:ss
* @apiSuccess (200) {String} rows.updateAt , YYYY-MM-DD HH:mm:ss
*/
var Router *gin.Engine
func init() {
mime.AddExtensionType(".svg", "image/svg+xml")
mime.AddExtensionType(".m3u8", "application/vnd.apple.mpegurl")
// mime.AddExtensionType(".m3u8", "application/x-mpegurl")
mime.AddExtensionType(".ts", "video/mp2t")
// prevent on Windows with Dreamware installed, modified registry .css -> application/x-css
// see https://stackoverflow.com/questions/22839278/python-built-in-server-not-loading-css
mime.AddExtensionType(".css", "text/css; charset=utf-8")
gin.DisableConsoleColor()
gin.SetMode(gin.ReleaseMode)
gin.DefaultWriter = utils.GetLogWriter()
}
func Errors() gin.HandlerFunc {
return func(c *gin.Context) {
c.Next()
for _, err := range c.Errors {
switch err.Type {
case gin.ErrorTypeBind:
switch err.Err.(type) {
case validator.ValidationErrors:
errs := err.Err.(validator.ValidationErrors)
for _, err := range errs {
sec := utils.Conf().Section("localize")
field := sec.Key(err.Field).MustString(err.Field)
tag := sec.Key(err.Tag).MustString(err.Tag)
c.AbortWithStatusJSON(http.StatusBadRequest, fmt.Sprintf("%s %s", field, tag))
return
}
default:
log.Println(err.Err.Error())
c.AbortWithStatusJSON(http.StatusBadRequest, "Inner Error")
return
}
}
}
}
}
func NeedLogin() gin.HandlerFunc {
return func(c *gin.Context) {
if sessions.Default(c).Get("uid") == nil {
c.AbortWithStatusJSON(http.StatusUnauthorized, "Unauthorized")
return
}
c.Next()
}
}
func Init() (err error) {
Router = gin.New()
pprof.Register(Router)
// Router.Use(gin.Logger())
Router.Use(gin.Recovery())
Router.Use(Errors())
Router.Use(cors.Default())
store := sessions.NewGormStoreWithOptions(db.SQLite, sessions.GormStoreOptions{
TableName: "t_sessions",
}, []byte("EasyDarwin@2018"))
tokenTimeout := utils.Conf().Section("http").Key("token_timeout").MustInt(7 * 86400)
store.Options(sessions.Options{HttpOnly: true, MaxAge: tokenTimeout, Path: "/"})
sessionHandle := sessions.Sessions("token", store)
{
wwwDir := filepath.Join(utils.DataDir(), "www")
Router.Use(static.Serve("/", static.LocalFile(wwwDir, true)))
}
{
api := Router.Group("/api/v1").Use(sessionHandle)
api.GET("/login", API.Login)
api.GET("/userinfo", API.UserInfo)
api.GET("/logout", API.Logout)
api.GET("/defaultlogininfo", API.DefaultLoginInfo)
api.GET("/modifypassword", NeedLogin(), API.ModifyPassword)
api.GET("/serverinfo", API.GetServerInfo)
api.GET("/restart", API.Restart)
api.GET("/pushers", API.Pushers)
api.GET("/players", API.Players)
}
return
}