diff --git a/cmd/main.go b/cmd/main.go index d28787d..91491d6 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -94,7 +94,6 @@ func main() { app := fiber.New( fiber.Config{ Prefork: *prefork, - GETOnly: true, }, ) @@ -137,7 +136,8 @@ func main() { app.Get("ruleset", handlers.Ruleset) app.Get("raw/*", handlers.Raw) - app.Get("api/*", handlers.Api) + app.Post("/api", handlers.Api) + app.Get("/api/*", handlers.Api) app.Get("/*", handlers.ProxySite(*ruleset)) log.Fatal(app.Listen(":" + *port)) diff --git a/handlers/api.go b/handlers/api.go index 190c8fe..3a5b4ae 100644 --- a/handlers/api.go +++ b/handlers/api.go @@ -7,16 +7,35 @@ import ( "github.com/gofiber/fiber/v2" ) +type JsonRequest struct { + URL string `json:"url"` +} + //nolint:all //go:embed VERSION var version string func Api(c *fiber.Ctx) error { - // Get the url from the URL - urlQuery := c.Params("*") - + var url string queries := c.Queries() - body, req, resp, err := fetchSite(urlQuery, queries) + + // Check content type to determine if it's JSON + contentType := c.Get("Content-Type") + if contentType == "application/json" { + // Parse JSON body + var jsonReq JsonRequest + if err := c.BodyParser(&jsonReq); err != nil { + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ + "error": "Invalid JSON request", + }) + } + url = jsonReq.URL + } else { + // Get the url from the URL params + url = c.Params("*") + } + + body, req, resp, err := fetchSite(url, queries) if err != nil { log.Println("ERROR:", err) c.SendStatus(500)