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

REST Methods #5

Closed
tfasano1 opened this issue Apr 23, 2023 · 5 comments
Closed

REST Methods #5

tfasano1 opened this issue Apr 23, 2023 · 5 comments

Comments

@tfasano1
Copy link

tfasano1 commented Apr 23, 2023

From my research of the docs, net/http only handles GET and POST requests. Are there plans to use a mux which can handle PUT or DELETE requests? You can trigger these requests from the templates just like any other form element using htmx.

Using Post for deleting/updating will have consequences, since Post isn't idempotent.

@tfasano1
Copy link
Author

tfasano1 commented Apr 23, 2023

The gorilla mux looks like a solution, but maybe we can get away with enabling CORS requests on the web server? Let me know any input you have.

https://code.whatever.social/questions/46310113/consume-a-delete-endpoint-from-go

@pattmax00
Copy link
Owner

pattmax00 commented Apr 23, 2023

I think you have to make PUT and DELETE requests with fetch
https://stackoverflow.com/a/68529248
I will look into CORS

@tfasano1
Copy link
Author

tfasano1 commented Apr 24, 2023

As far as the backend / middleware goes, should delete routes / controllers be in their own go files? I've tried supporting multiple methods on handles /users/{id} (sending DELETE would delete the user, sending PUT to that same url would update the user's info) but in the current setup of this project each url handle is limited to one http method.

I'm aware you can sort of achieve what I'm trying to do by switching on the request method, like this https://git.sr.ht/~m15o/midnight-pub/tree/master/item/ui/post_remove.go , but in this framework handlers are segregated by http method into separate controller files. Is there an intended work-around for this issue ?

@pattmax00
Copy link
Owner

pattmax00 commented Apr 24, 2023

New controller files should be named and created according to their purpose like userController.go I should change this and make it more clear in the default setup.

For routes, I suggest having a couple big master files, so it's easy to find the logic associated with them (eg. user reports an error on page /url/thing/example).

So for your use case I'd say make a new routes file called multiMethodRoutes.go (or whatever name you find better) and those can be tied to Go functions that are tied to a switch statement like in the example you gave (the controllers these functions exist in should just be grouped by use case, so it's fine to have a controller with functions for GET, POST, PUT, and DELETE requests if needed). It's really all just a naming convention though, you could define these routes in getRoutes.go or postRoutes.go but that would be improper.

Hopefully this all makes sense.

@tfasano1
Copy link
Author

Big help! Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants