http_listener multiple routes

Mar 21, 2014 at 10:01 AM
Hello,
Aside from having the single endpoint working, for example
http_listener listenerVoices(L"http://localhost:8080/hello");
listenerVoices.open().wait();

listenerVoices.support(web::http::methods::GET, [](web::http::http_request request)
  {
     request.reply(web::http::status_codes::OK, L"Hello world");
  }
);

std::string line;
std::cout << "Press enter to exit" << std::endl;
std::getline(std::cin, line);

listenerVoices.close().wait();
what is the best practice when having multiple endpoints (i.e. http://localhost:8080/hello, http://localhost:8080/helloagain)?

I think the API would be a bit more usable if it was exposed like this:
http_listener listenerVoices(L"http://localhost:8080");
listenerVoices.open().wait();

listenerVoices.support(L"/hello", web::http::methods::GET, [](web::http::http_request request)
  {
     request.reply(web::http::status_codes::OK, L"Hello world");
  }
);

listenerVoices.support(L"/helloagain", web::http::methods::GET, [](web::http::http_request request)
  {
     request.reply(web::http::status_codes::OK, L"Hello again world");
  }
);

std::string line;
std::cout << "Press enter to exit" << std::endl;
std::getline(std::cin, line);

listenerVoices.close().wait();
what are your thoughts on this?

Thanks,
Pedro
Coordinator
Mar 25, 2014 at 3:04 AM
Edited Mar 25, 2014 at 6:43 PM
Hi Pedro,

You can create multiple listeners to listen on multiple endpoints.
http_listener listenerVoices_1(L"http://localhost:8080/hello");
listenerVoices_1.support(web::http::methods::GET, [](web::http::http_request request)
  {
     request.reply(web::http::status_codes::OK, L"Hello world");
  }
);
listenerVoices_1.open().wait();

http_listener listenerVoices_2(L"http://localhost:8080/hello/helloagain"); 
listenerVoices_2.support( web::http::methods::GET, [](web::http::http_request request)
  {
     request.reply(web::http::status_codes::OK, L"Hello again world");
  }
);
listenerVoices_2.open().wait();

std::string line;
std::cout << "Press enter to exit" << std::endl;
std::getline(std::cin, line);

listenerVoices_1.close().wait();
listenerVoices_2.close().wait();
We consider a single listener can only listen on a single uri,, every listener will register on a shared http server and when the server receives requests, it will dispatch to the corresponding listener.
Mar 21, 2015 at 2:11 PM
But what do you do when you do not know in advance how many end points to listen against

Where you are reading from a database the addresses - how do you set up multiple listeners. The above code works if you know in advance how many listeners but I cannot see how this works if the number is unknown by the developer.

If you take C# where you have the http_listener - you can set up multiple prefixes, or for that matter set up one prefix and then retrieve the full url - thereby supporting multiple end points.

At the moment I am setting up a prototype in C# but would rather C++
Coordinator
Mar 23, 2015 at 5:37 PM
Hi paulsc,

Yes we don't have any ability with the library to add URIs for listening to an http_listener after it is constructed. However there isn't any reason why you can't dynamically construct an http_listener with a URI you get at runtime from some location like a database. Just do something like the following:
const auto &uriToListen = GetUriFromDatabase();
http_listener listener(uriToListen);
...
Steve
Oct 13, 2015 at 9:27 PM
Edited Oct 13, 2015 at 9:56 PM
So multiple http_listener would be the correct way to handle routes?

for doing things similar to C#'s ASP.NET MVC


url.com/controller/action/data
url/controller/data


... etc?
Coordinator
Oct 13, 2015 at 11:24 PM
On Windows, yes you can use multiple http_listeners to handle routes this way.

However, on non-Windows systems, this probably will not work. You will need to manually dispatch inside your support callback.