package routers

import (
	"eta/eta_mini_ht_api/controllers/media"
	"eta/eta_mini_ht_api/controllers/report"
	"eta/eta_mini_ht_api/controllers/user"
	"eta/eta_mini_ht_api/middleware"
	"github.com/beego/beego/v2/server/web"
	"github.com/beego/beego/v2/server/web/filter/cors"
	"time"
)

func init() {
	web.InsertFilter("*", web.BeforeRouter, cors.Allow(&cors.Options{
		AllowAllOrigins:  true, // 允许所有来源的请求
		AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
		AllowHeaders:     []string{"Origin", "Authorization", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: true,
		MaxAge:           12 * time.Hour,
	}))
	//增加授权拦截

	web.InsertFilter("*", web.BeforeRouter, middleware.AuthMiddleware())

	ns := web.NewNamespace("/htapi",
		web.NSNamespace("/user",
			web.NSInclude(
				&user.UserController{},
			),
		),
		web.NSNamespace("/auth",
			web.NSInclude(
				&user.AuthController{},
			),
		),
		web.NSNamespace("/report",
			web.NSInclude(
				&report.ReportController{},
			),
		),
		web.NSNamespace("/media",
			web.NSInclude(
				&media.MediaController{},
			),
		),
		web.NSNamespace("/analyst",
			web.NSInclude(
				&user.AnalystController{},
			),
		),
	)
	web.AddNamespace(ns)
}
