package handlers

import (
	"github.com/gofiber/fiber/v2"
	"proxy-server/gateway/app"
	"proxy-server/gateway/core"
)

type PermitReq []core.PermitDef

func Permit(ctx *fiber.Ctx) (err error) {

	// 安全验证
	var sec core.SecuredReq
	if err := ctx.BodyParser(&sec); err != nil {
		return err
	}

	// 获取请求参数
	req, err := core.Decrypt[PermitReq](&sec, app.PlatformSecret)
	if err != nil {
		return err
	}

	// 保存授权配置
	for _, permit := range *req {
		app.StorePermit(&permit)
	}

	return nil
}
