improve yaml content type for display in browser; improve api ergonomics

This commit is contained in:
Kevin Pham
2023-12-07 10:05:54 -06:00
parent 4cf2f8c782
commit 3aad9cf406

View File

@@ -33,7 +33,7 @@ func NewRulesetSiteHandler(opts *ProxyOptions) fiber.Handler {
if err != nil {
return err
}
c.Set("content-type", "application/yaml")
c.Set("content-type", "text/yaml")
return c.Send([]byte(yml))
}
}
@@ -42,12 +42,13 @@ func NewRulesetSiteHandler(opts *ProxyOptions) fiber.Handler {
// return only that particular rule
reqURL, err := extractURLFromContext(c, "api/ruleset/")
if err != nil {
return err
c.SendStatus(404)
return c.SendString(fmt.Sprintf("A rule that matches '%s' was not found in the ruleset. Possible URL formatting issue.", c.Params("*")))
}
rule, exists := opts.Ruleset.GetRule(reqURL)
if !exists {
c.SendStatus(404)
c.SendString(fmt.Sprintf("A rule that matches '%s' was not found in the ruleset.", reqURL))
return c.SendString(fmt.Sprintf("A rule that matches '%s' was not found in the ruleset.", reqURL))
}
switch c.Get("accept") {
@@ -63,7 +64,7 @@ func NewRulesetSiteHandler(opts *ProxyOptions) fiber.Handler {
if err != nil {
return err
}
c.Set("content-type", "application/yaml")
c.Set("content-type", "text/yaml")
return c.Send(yml)
}
}
@@ -74,6 +75,9 @@ func extractURLFromContext(ctx *fiber.Ctx, apiPrefix string) (*url.URL, error) {
reqURL := ctx.Params("*")
reqURL = strings.TrimPrefix(reqURL, apiPrefix)
if !strings.HasPrefix(reqURL, "http") {
reqURL = "https://" + reqURL
}
// sometimes client requests doubleroot '//'
// there is a bug somewhere else, but this is a workaround until we find it