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

Proposal for enhanced routing mechanism to support multi-method and wildcard-method syntax #105

Open
aofei opened this issue Mar 11, 2024 · 2 comments
Labels
proposal Proposal

Comments

@aofei
Copy link
Member

aofei commented Mar 11, 2024

Introduction

The current routing mechanism in Yap, exemplified by file names like get_p_#id.yap, effectively maps a single HTTP method to a specific route, e.g., GET /p/:id. However, this approach presents limitations when one wishes to register multiple HTTP methods for the same route or utilize a wildcard to match any method for a given route. This proposal aims to extend the current routing syntax to accommodate these use cases.

Proposed enhancements

  1. Support for multiple methods: To allow multiple methods to be associated with a single route, I propose a syntax where methods are concatenated using a , sign. For instance, get,head_p_#id.yap would register the /p/:id route for both GET and HEAD methods.
  2. Wildcard method support: To enable a route to respond to any HTTP method, I propose the use of an * prefix. For example, *_p_#id.yap would match the /p/:id route for any HTTP method. This would replace the existing but less intuitive handle_p_#id.yap, aligning with the more recognized wildcard symbol *. (HANDLE is not a standard HTTP method, but non-standard methods are allowed in the HTTP spec, e.g., WebDAV uses many non-standard methods.)

Conflict resolution

In cases where multiple files might match a request (e.g., *_p_#id.yap and get_p_#id.yap), the router should prioritize the more specific match. Therefore, a GET /p/:id request would be handled by get_p_#id.yap over *_p_#id.yap, given the explicit mention of the GET method in the former.

Rationale

  • Flexibility: This enhancement would offer developers greater flexibility in defining their APIs, allowing for more concise and powerful routing configurations.
  • Convenience: By supporting multiple methods in a single route definition, we reduce redundancy and make route management more straightforward.

Conclusion

I believe these enhancements will make Yap more powerful and flexible, catering to a broader range of routing requirements.

@aofei
Copy link
Member Author

aofei commented Mar 11, 2024

For the support of multi-method syntax, there's a workaround worth mentioning. One can avoid duplicating the same .yap files for different methods by utilizing symbolic links.

For example:

$ gop mod init hello
$ gop get github.com/goplus/yap@latest
$ echo 'html `<html><body>Hello, YAP!</body></html>`' > get.yap
$ ln -s get.yap head.yap
$ gop mod tidy
$ gop run .
$ curl -i localhost:8080
HTTP/1.1 200 OK
Content-Length: 37
Content-Type: text/html
Date: Mon, 11 Mar 2024 07:53:23 GMT

<html><body>Hello, YAP!</body></html>
$ curl -I localhost:8080
HTTP/1.1 200 OK
Content-Length: 37
Content-Type: text/html
Date: Mon, 11 Mar 2024 07:53:26 GMT

@xushiwei xushiwei added the proposal Proposal label Mar 12, 2024
@xushiwei
Copy link
Member

xushiwei commented Mar 12, 2024

#108 - default http HEAD implementation.

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

No branches or pull requests

2 participants