Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Register custom methods #2107

Merged

Conversation

rafimuhammad01
Copy link
Contributor

Description

This PR is to support the custom method feature. I updated register function so that function won't be giving validation if the method is unsupported yet, so user can add any method that they want to. I also make changes to methodInt function to return the index instead of using hardcoded with switch-case statement.

Fixes #2096

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • For new functionalities I follow the inspiration of the express js framework and built them similar in usage
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation - https://github.com/gofiber/docs for https://docs.gofiber.io/
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • If new dependencies exist, I have checked that they are really necessary and agreed with the maintainers/community (we want to have as few dependencies as possible)
  • I tried to make my code as fast as possible with as few allocations as possible
  • For new code I have written benchmarks so that they can be analyzed and improved

Commit formatting:

Use emojis on commit messages so it provides an easy way of identifying the purpose or intention of a commit. Check out the emoji cheatsheet here: https://gitmoji.carloscuesta.me/

@welcome
Copy link

welcome bot commented Sep 20, 2022

Thanks for opening this pull request! 🎉 Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@efectn
Copy link
Member

efectn commented Sep 20, 2022

There should be 2 ways to allow custom methods:

  • Allow adding custom methods and validate them when to add new routes.
  • Allow unknown methodds without any custom method registration.

Maybe the first one would be overdesign. Current implementation seems OK but i'll review it then. What do you think @ReneWerner87

@ReneWerner87
Copy link
Member

sorry very busy week, will answer tomorrow

@ReneWerner87
Copy link
Member

current implementation seems to be okay

  • please also update the documentation repository

feature will be in the next release

helpers.go Outdated Show resolved Hide resolved
router.go Outdated Show resolved Hide resolved
@ReneWerner87
Copy link
Member

@rafimuhammad01 pls check the last comments

@rafimuhammad01
Copy link
Contributor Author

@ReneWerner87 please check the last comment

@ReneWerner87
Copy link
Member

will do this today

Copy link
Member

@ReneWerner87 ReneWerner87 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after the last adjustments i will merge it

app.go Outdated Show resolved Hide resolved
@rafimuhammad01
Copy link
Contributor Author

@ReneWerner87 Already push the latest changes, please kindly check it. Thank you!

app_test.go Outdated Show resolved Hide resolved
@ReneWerner87
Copy link
Member

current implementation seems to be okay

  • please also update the documentation repository

feature will be in the next release

don´t forget this @rafimuhammad01
we need somewhere a hint in the routing section that we support custom request methods

Copy link
Member

@ReneWerner87 ReneWerner87 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

through the test run i found out that there is one more problem
https://github.com/gofiber/fiber/actions/runs/3198073219/jobs/5222134276
image

because if you add a custom method and run the USE or ALL before, the new method will not be considered there, because it is not inital in the list of those to be considered

should we maybe not approach the feature so dynamically and add the methods on-the-fly and rather maintain a slice inside the fiber.config with the allowed methods ?

so the intMethod slice as config property with the 9 default methods?
then you could also reduce or extend the default if you don't want TRACE or CONNECT to work or LOAD to be added

but then you would have to consider the mounting, because these methods are set per fiber app and would have to be merged then

@efectn @rafimuhammad01 what do you think ?

@efectn
Copy link
Member

efectn commented Oct 6, 2022

through the test run i found out that there is one more problem https://github.com/gofiber/fiber/actions/runs/3198073219/jobs/5222134276 image

because if you add a custom method and run the USE or ALL before, the new method will not be considered there, because it is not inital in the list of those to be considered

should we maybe not approach the feature so dynamically and add the methods on-the-fly and rather maintain a slice inside the fiber.config with the allowed methods ?

so the intMethod slice as config property with the 9 default methods? then you could also reduce or extend the default if you don't want TRACE or CONNECT to work or LOAD to be added

but then you would have to consider the mounting, because these methods are set per fiber app and would have to be merged then

@efectn @rafimuhammad01 what do you think ?

Sounds good idea

@ReneWerner87
Copy link
Member

@rafimuhammad01 okay for you ? should i do this or you want todo this ?

@rafimuhammad01
Copy link
Contributor Author

rafimuhammad01 commented Oct 11, 2022

Hi @ReneWerner87 sorry for the late reply, I've been busy this week. I still didn't understand about the other approach we will implement, could you please elaborate more? Thanks

@ReneWerner87
Copy link
Member

the problem

func main() {
	app := fiber.New(fiber.Config{})

	app.Use(func(ctx *fiber.Ctx) error { // is always called only not at LOAD
		// doing something
		return ctx.Next()
	})
	app.Add("LOAD", "/test", func(ctx *fiber.Ctx) error {
		return ctx.SendString("hello")
	})

	log.Fatal(app.Listen(":3000"))
}

the solution

func main() {
	app := fiber.New(fiber.Config{
		RequestMethods: []string{ // default is the normal list
                      fiber.MethodGet,
                      fiber.MethodHead,
                      fiber.MethodPost,
                      fiber.MethodPut,
                      fiber.MethodDelete,
                      fiber.MethodConnect,
                      fiber.MethodOptions,
                      fiber.MethodTrace,
                      fiber.MethodPatch,
                      "LOAD",
              },
        // with this approach we can reduce the list and only allow what we want
	})

	app.Use(func(ctx *fiber.Ctx) error { // is always called
		// doing something
		return ctx.Next()
	})
	app.Add("LOAD", "/test", func(ctx *fiber.Ctx) error {
		return ctx.SendString("hello")
	})

	log.Fatal(app.Listen(":3000"))
}

@rafimuhammad01
Copy link
Contributor Author

I'm sorry for the late response, have been super busy right now. I see the problem and will try to fix this issue. Thank you!

@ReneWerner87
Copy link
Member

  • don´t forget to update the documentation repository

@ReneWerner87 ReneWerner87 merged commit 878c954 into gofiber:master Nov 11, 2022
@welcome
Copy link

welcome bot commented Nov 11, 2022

Congrats on merging your first pull request! 🎉 We here at Fiber are proud of you! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

efectn added a commit to gofiber/docs that referenced this pull request Nov 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 [Bug]: panic: add: invalid http method LOAD
3 participants