Http server: add options to ws https to redirect all requests to a given url.

This commit is contained in:
Benjamin Sergeant
2019-09-26 09:10:30 -07:00
parent 3cd7c0194f
commit 8a662b35e1
6 changed files with 40 additions and 5 deletions

View File

@ -13,12 +13,32 @@
namespace ix
{
int ws_httpd_main(int port, const std::string& hostname)
int ws_httpd_main(int port,
const std::string& hostname,
bool redirect,
const std::string& redirectUrl)
{
spdlog::info("Listening on {}:{}", hostname, port);
ix::HttpServer server(port, hostname);
if (redirect)
{
//
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections
//
server.setOnConnectionCallback(
[redirectUrl](
HttpRequestPtr request,
std::shared_ptr<ConnectionState> /*connectionState*/) -> HttpResponsePtr {
WebSocketHttpHeaders headers;
headers["Location"] = redirectUrl;
return std::make_shared<HttpResponse>(
301, "OK", HttpErrorCode::Ok, headers, std::string());
});
}
auto res = server.listen();
if (!res.first)
{