package beego

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/plugins/cors"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/port/beego/middleware"
	_ "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/port/beego/routers"
)

func init() {
	beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
		AllowOrigins:     []string{"*"},
		AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
		AllowHeaders:     []string{"Origin", "Authorization", "Access-Control-Allow-Origin"},
		ExposeHeaders:    []string{"Content-Length", "Access-Control-Allow-Origin"},
		AllowCredentials: true,
	}))

	beego.InsertFilter("/*", beego.BeforeExec, middleware.CreateRequstLogFilter())
	beego.InsertFilter("/*", beego.AfterExec, middleware.CreateResponseLogFilter(), false)

	beego.InsertFilter("/v1/auth/profile", beego.BeforeExec, middleware.CheckAuthorization)
	beego.InsertFilter("/v1/user/*", beego.BeforeExec, middleware.InspectRoleAccess(""))
	beego.InsertFilter("/v1/role/*", beego.BeforeExec, middleware.InspectRoleAccess(""))
	beego.InsertFilter("/v1/project_module/*", beego.BeforeExec, middleware.InspectRoleAccess(""))
	beego.InsertFilter("/v1/client_version/*", beego.BeforeExec, middleware.InspectRoleAccess(""))
	beego.InsertFilter("/v1/project_module_version/*", beego.BeforeExec, middleware.InspectRoleAccess("/project_module/*"))
	beego.InsertFilter("/v1/project_module_files/*", beego.BeforeExec, middleware.InspectRoleAccess("/project_module/*"))
	beego.InsertFilter("/v1/rbac/*", beego.BeforeExec, middleware.InspectRoleAccess("/role/*"))
}