lpereira / lwan

Experimental, scalable, high performance HTTP server
https://lwan.ws
GNU General Public License v2.0
5.94k stars 548 forks source link

Custom Paths #183

Closed GuacheSuede closed 7 years ago

GuacheSuede commented 7 years ago

Hi Ipereira, how does one go about writing custom paths such as /hello/John and getting John as a param, and different paths leading to different handlers ?

Thank You

GuacheSuede commented 7 years ago

@lpereira

lpereira commented 7 years ago

For the first question, you just have a URL map handler that matches "/hello/": making a request to "/hello/John" will call the appropriate handler. The URL in the request struct will be modified, having the "/hello/" prefix stripped, leaving only "John" in it.

For the second question, just add more mappings to the URL map.

GuacheSuede commented 7 years ago

Alright Thank you @lpereira , any plans to make a custom url params support eg. user/:id/posts ?

lpereira commented 7 years ago

You can use the rewrite module and rewrite /user/(%a+)/(%a+) to, say, /user_action?action=%2&user=%1. Lwan provides functions to obtain variables passed to the query string.

GuacheSuede commented 7 years ago

@lpereira those are query params though, what about params part of string ?

lpereira commented 7 years ago

If you use the rewrite module like that, requests to, say, "/user/John/display" will be rewritten so that "/user_action?action=display&user=John" is called instead. You could even have different handlers, for instance, so that Lwan will give a 404 for invalid actions:

 rewrite /user/ {
      pattern ^(%a+)/(%a+) { rewrite as = /user_%2?id=%1 }
 }
 &user_display /user_display
 &user_delete /user_delete

(user_display and user_delete are handler functions.)

GuacheSuede commented 7 years ago

@lpereira Alright Thank You, final question if you dont mind, how do i specify diffrent GET/POST handlers for the route ?

lpereira commented 7 years ago

The wiki is slightly outdated but there's a tutorial that might help you: https://github.com/lpereira/lwan/wiki/Using-Lwan-as-a-Library

GuacheSuede commented 7 years ago

@lpereira Yes I saw that, dint say how to specify different handler for routes, different GET/POST/DELETE /etc. handlers

lpereira commented 7 years ago

You have only one handler per path. The handler has to dispatch based on the method (call lwan_request_get_method(), switch on REQUEST_METHOD_GET, ..._POST, ..._DELETE, etc).

lpereira commented 7 years ago

I'm closing this issue as it wasn't really an issue to be fixed; feel free to reopen it, however.

GuacheSuede commented 7 years ago

Aight Sure, its fixed now, Thanks @lpereira