add list modifiers api endpoint

This commit is contained in:
Kevin Pham
2023-12-07 16:17:45 -06:00
parent c9bb0f6c25
commit c870cd6ba8
7 changed files with 782 additions and 59 deletions

View File

@@ -1,64 +1,28 @@
package handlers
// TODO: this is a work in progress.
// Needs codegen to populate the rqm and rsm structs from available modifiers in ladder/proxychain/*modifers/*.go
import (
"encoding/json"
"github.com/gofiber/fiber/v2"
"ladder/proxychain/responsemodifiers/api"
)
type ModifiersAPIResponse struct {
Success bool `json:"success"`
Error api.Error `json:"error"`
Result Modifiers `json:"result"`
}
type Modifiers struct {
RequestModifiers []Modifier `json:"requestmodifiers"`
ResponseModifiers []Modifier `json:"responsemodifiers"`
}
type Modifier struct {
Name string `json:"name"`
Description string `json:"description"`
Params []Param `json:"params"`
}
type Param struct {
Name string `json:"name"`
Type string `json:"type"`
}
func init() {
rqm := []Modifier{}
// ========= loop
rqm = append(rqm, Modifier{
Name: "codegen_name",
Description: "codegen_description",
Params: []Param{
{Name: "codegen_name1", Type: "codegen_type1"},
{Name: "codegen_name2", Type: "codegen_type2"},
},
})
// ========= loop end
rsm := []Modifier{}
// ========= loop
rsm = append(rsm, Modifier{
Name: "codegen_name",
Description: "codegen_description",
Params: []Param{
{Name: "codegen_name1", Type: "codegen_type1"},
{Name: "codegen_name2", Type: "codegen_type2"},
},
})
// ========= loop end
m := &ModifiersAPIResponse{Success: true}
m.Result.RequestModifiers = rqm
m.Result.ResponseModifiers = rsm
func NewAPIModifersListHandler(opts *ProxyOptions) fiber.Handler {
payload := ModifiersAPIResponse{
Success: true,
Result: AllMods,
}
body, err := json.MarshalIndent(payload, "", " ")
if err != nil {
panic(err)
}
return func(c *fiber.Ctx) error {
c.Set("content-type", "application/json")
if err != nil {
c.SendStatus(500)
return c.SendStream(api.CreateAPIErrReader(err))
}
return c.Send(body)
}
}