Melkeydev / go-blueprint

Go-blueprint allows users to spin up a quick Go project using a popular framework
MIT License
2.07k stars 141 forks source link

Implement [Fiber Framework] Recovery Middleware #223

Closed H0llyW00dzZ closed 1 month ago

H0llyW00dzZ commented 1 month ago

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

Problem/Feature

Related to this issue:

and it is pretty useful.

Description of Changes:

Checklist

H0llyW00dzZ commented 1 month ago

[!NOTE] Regarding this error for manipulate panic, it will apply to all errors (globally) if there is no other error handling, so another error handling mechanism must be implemented independently to avoid conflicts with this.

For example:

func ReloadConfigHandler(c *fiber.Ctx, db database.Service) error {
    // Log the user activity
    Logger.LogUserActivity(c, "requested configuration reload")
    // Parse the JSON payload into the ConfigPayload struct.
    var payload ConfigPayload
    if err := c.BodyParser(&payload); err != nil {
        return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
            "error": "Invalid JSON payload",
        })
    }

    // Validate and apply the new configuration settings.
    // For example, update the update interval if it's provided in the payload.
    if payload.UpdateInterval != "" {
        // Parse the update interval duration.
        newInterval, err := time.ParseDuration(payload.UpdateInterval)
        if err != nil {
            return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
                "error": "Invalid update interval format",
            })
        }

        // Apply the new update interval to the scheduler.
        // This function should update the configuration in the database and restart the scheduler.
        if err := updateSchedulerWithNewInterval(db, newInterval); err != nil {
            // Handle the error, return a response with status code 500 (Internal Server Error)
            return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
                "error": "Failed to update the configuration",
            })
        }
    }

    // Respond to the request with a success message.
    return c.Status(fiber.StatusOK).JSON(fiber.Map{
        "message": "Configuration reloaded successfully",
    })
}
briancbarrow commented 1 month ago

@H0llyW00dzZ Like I mentioned in the other PR, after discussing it, we have decided that this feature is beyond the scope of what go-blueprint is supposed to be. It is intended to just be a scaffold from which to start projects. Users can add more robust features/middleware as they see fit.

Thanks though for your PRs. We appreciate the contribution even if it ends up not being a fit.