Mix of URL handlers (serve html pages and REST interface)

40 views
Skip to first unread message

gregoire...@gmail.com

unread,
May 24, 2016, 8:27:46 AM5/24/16
to onion-dev
Hi,

I'm starting with the onion library and trying to create a HTTP server that will send me the html files located in the directory "pages", and also be a REST server for all URL starting with "config/".
Looking at the examples, I was able to create a small REST server like this:
    onion_url *urls=onion_root_url(o); 
    onion_url_add_static(urls, "config/version", "0.0.1\r\n", HTTP_OK);
    onion_url_add(urls, "config/hello", hello);


I can also be a simple webserver for all my html files located in "pages":
    onion_handler *dir=onion_handler_export_local_new("pages");
    onion_handler_add(dir, onion_handler_static("<h1>404 - File not found.</h1>", 404) );
    onion_set_root_handler(o, dir);

Now I don't know how to combine both. I mean, I want to get pages/index.html if I request http://myhost/index.html, and I want to get "0.0.1" when I request http://myhost/config/version.
Maybe I have to create a root handler and then redirect the requests? I feel like I'm missing something very basic with the onion... Could someone here help me and provide me some code?

Thank you,
Greg

David Moreno Montero

unread,
May 24, 2016, 11:21:45 AM5/24/16
to gregoire...@gmail.com, onion-dev
I think you have two options:

1. `onion_url_add_handler(urls, "/", dir)`  to set a path, if the last it works a fall through
2. `onion_handler_add(onion_url_to_handler(urls), dirs)` a real fall through from any non yet matched url, should have a little bit better performance

Regards,
David.

--
You received this message because you are subscribed to the Google Groups "onion-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to onion-dev+...@coralbits.com.
To post to this group, send email to onio...@coralbits.com.
Visit this group at https://groups.google.com/a/coralbits.com/group/onion-dev/.
For more options, visit https://groups.google.com/a/coralbits.com/d/optout.



--

gregoire...@gmail.com

unread,
May 25, 2016, 3:27:15 AM5/25/16
to onion-dev, gregoire...@gmail.com
Thank you!
I was able to make it work with second solution:
    onion_url *urls=onion_root_url(o);
   
    /* REST URLs */
    onion_url_add_static(urls, "", "SBC HTTP Web Server\r\n", HTTP_OK); /* root page */

    onion_url_add_static(urls, "config/version", "0.0.1\r\n", HTTP_OK);
    onion_url_add(urls, "config/hello", hello);
    onion_url_add(urls, "config/number", number);
   
    /* HTML pages are in this subfolder */
    onion_handler *dir=onion_handler_export_local_new("pages");
    onion_handler_add(onion_url_to_handler(urls), dir); /* fall through from any non yet matched url */


Best Regards,
Greg
Reply all
Reply to author
Forward
0 new messages