oc-catalog/routers/uglyfix.go

43 lines
1.1 KiB
Go

package routers
import (
"strings"
"github.com/beego/beego/v2/server/web/filter/cors"
"github.com/beego/beego/v2/core/logs"
beego "github.com/beego/beego/v2/server/web"
"github.com/beego/beego/v2/server/web/context"
)
//TODO: This file should never exist. Solve the bugs and avoid using this
// [ id id2 id3 ]
// id,id2,id3
// Some Beego bugs? that we want to fix
func initUglyFixes() {
var FixParameterArrays = func(ctx *context.Context) {
for k, v := range ctx.Input.Params() {
if strings.HasPrefix(v, "[") && strings.HasSuffix(v, "]") {
logs.Warn("BUGFIX: Fixing array interpretation. Should not be done like this")
newParam := strings.NewReplacer("[", "", " ", ",", "]", "").Replace(v)
ctx.Input.SetParam(k, newParam)
}
}
}
beego.InsertFilter("/*/multi/:IDs", beego.BeforeExec, FixParameterArrays, beego.WithReturnOnOutput(true))
// FIXME: We should define CORS properly (maybe more permissive in dev mode?)
beego.InsertFilter("*", beego.BeforeStatic, cors.Allow(&cors.Options{
AllowAllOrigins: true,
AllowMethods: []string{
"HEAD",
"GET",
"POST",
"PUT",
"DELETE",
},
}))
}