Add convenience wrapper App (again)
This commit is contained in:
@@ -27,7 +27,8 @@ HEADERS += \
|
|||||||
src/LoopData.h \
|
src/LoopData.h \
|
||||||
src/AsyncSocket.h \
|
src/AsyncSocket.h \
|
||||||
src/AsyncSocketData.h \
|
src/AsyncSocketData.h \
|
||||||
src/Loop.h
|
src/Loop.h \
|
||||||
|
src/App.h
|
||||||
|
|
||||||
INCLUDEPATH += uSockets/src src
|
INCLUDEPATH += uSockets/src src
|
||||||
QMAKE_CXXFLAGS += -fsanitize=address
|
QMAKE_CXXFLAGS += -fsanitize=address
|
||||||
|
|||||||
@@ -1,22 +1,12 @@
|
|||||||
|
#include "App.h"
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <cstring>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
std::string buffer;
|
// should probably fix this one up a bit some time
|
||||||
int connections = 0;
|
|
||||||
|
|
||||||
template<int SIZE, class T>
|
|
||||||
void respond(T *s) {
|
|
||||||
s->writeStatus("200 OK")->write([](int offset) {
|
|
||||||
return std::string_view(buffer.data() + offset, SIZE - offset);
|
|
||||||
}, SIZE);
|
|
||||||
}
|
|
||||||
|
|
||||||
#define USE_SSL
|
|
||||||
|
|
||||||
#include <map>
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
std::string_view getFile(std::string_view file) {
|
std::string_view getFile(std::string_view file) {
|
||||||
static std::map<std::string_view, std::string_view> cache;
|
static std::map<std::string_view, std::string_view> cache;
|
||||||
|
|
||||||
@@ -45,155 +35,22 @@ std::string_view getFile(std::string_view file) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "HttpContext.h"
|
|
||||||
#include "HttpResponse.h"
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
|
|
||||||
// test per-thread loopery
|
// this part is a bit too C-ish?
|
||||||
uWS::Loop::defaultLoop();
|
us_ssl_socket_context_options sslOptions;
|
||||||
new std::thread([]() {
|
sslOptions.key_file_name = "/home/alexhultman/uWebSockets/misc/ssl/key.pem";
|
||||||
uWS::Loop::defaultLoop();
|
sslOptions.cert_file_name = "/home/alexhultman/uWebSockets/misc/ssl/cert.pem";
|
||||||
uWS::Loop::defaultLoop();
|
sslOptions.passphrase = "1234";
|
||||||
});
|
|
||||||
uWS::Loop::defaultLoop();
|
|
||||||
|
|
||||||
us_ssl_socket_context_options ssl_options;
|
uWS::SSLApp(sslOptions).get("/hello", [](auto *res, auto *req) {
|
||||||
ssl_options.key_file_name = "/home/alexhultman/uWebSockets/misc/ssl/key.pem";
|
res->writeStatus(uWS::HTTP_200_OK)->write("Hello world!");
|
||||||
ssl_options.cert_file_name = "/home/alexhultman/uWebSockets/misc/ssl/cert.pem";
|
}).get("/:folder/:file", [](auto *res, auto *req) {
|
||||||
ssl_options.passphrase = "1234";
|
res->writeStatus(uWS::HTTP_200_OK)->write(getFile((req->getUrl() == "/" ? "/rocket_files/rocket.html" : req->getUrl()).substr(1)));
|
||||||
|
}).listen(3000, [](auto *token) {
|
||||||
uWS::HttpContext<true> *httpContext = uWS::HttpContext<true>::create(uWS::Loop::defaultLoop(), &ssl_options);
|
if (token) {
|
||||||
|
std::cout << "Listening on port " << 3000 << std::endl;
|
||||||
// req, res?
|
|
||||||
httpContext->onGet("/:folder/:file", [](auto *res, auto *req) {
|
|
||||||
// what file are we serving?
|
|
||||||
std::string_view fileName = req->getUrl();
|
|
||||||
if (fileName == "/") {
|
|
||||||
// this is our index
|
|
||||||
fileName = "/rocket_files/rocket.html";
|
|
||||||
}
|
}
|
||||||
|
}).run();
|
||||||
|
|
||||||
// load the file from cache and stream it as response
|
|
||||||
std::string_view file = getFile(fileName.substr(1));
|
|
||||||
res->writeStatus(uWS::HTTP_200_OK)->write([file](int offset) {
|
|
||||||
return file.substr(offset);
|
|
||||||
}, file.length());
|
|
||||||
});
|
|
||||||
|
|
||||||
httpContext->onGet("/yolo", [](auto *res, auto *req) {
|
|
||||||
std::cout << "URL (/yolo route): <" << req->getUrl() << ">" << std::endl;
|
|
||||||
std::cout << "Query: <" << req->getQuery() << ">" << std::endl;
|
|
||||||
std::cout << "User-Agent: <" << req->getHeader("user-agent") << ">" << std::endl;
|
|
||||||
});
|
|
||||||
|
|
||||||
httpContext->listen(nullptr, 3000, 0);
|
|
||||||
|
|
||||||
uWS::run();
|
|
||||||
|
|
||||||
httpContext->free();
|
|
||||||
uWS::Loop::defaultLoop()->free();
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
|
|
||||||
// 50 mb for huge
|
|
||||||
/* buffer.resize(52428800);
|
|
||||||
|
|
||||||
//uWS::init();
|
|
||||||
//uWS::Loop loop();
|
|
||||||
|
|
||||||
#ifdef USE_SSL
|
|
||||||
uWS::SSLApp app(uWS::SSLOptions()
|
|
||||||
.keyFileName("/home/alexhultman/uWebSockets/misc/ssl/key.pem")
|
|
||||||
.certFileName("/home/alexhultman/uWebSockets/misc/ssl/cert.pem")
|
|
||||||
.passphrase("1234"));
|
|
||||||
#else
|
|
||||||
uWS::App app;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
struct UserData {
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
// serve a chat page with pub/sub and index.html and get (should be the main app of use)
|
|
||||||
// basically take the old web chat page and upgrade it
|
|
||||||
|
|
||||||
uWS::App a; // or uWS::SSLApp(options) for SSL
|
|
||||||
|
|
||||||
a.onGet("/", [](auto *s, auto *req, auto *args) {
|
|
||||||
|
|
||||||
std::cout << "URL: <" << req->getUrl() << ">" << std::endl;
|
|
||||||
std::cout << "Query: <" << req->getQuery() << ">" << std::endl;
|
|
||||||
std::cout << "User-Agent: <" << req->getHeader("user-agent") << ">" << std::endl;
|
|
||||||
|
|
||||||
}).onWebSocket<UserData>("/ws", [](auto *ws, auto *req, auto *args) {
|
|
||||||
|
|
||||||
std::cout << "WebSocket connected to /wsApi" << std::endl;
|
|
||||||
|
|
||||||
}).onMessage<true>([](auto *ws, auto message) {
|
|
||||||
|
|
||||||
std::cout << "WebSocket data: " << message << std::endl;
|
|
||||||
|
|
||||||
//ws->send(message, opCode);
|
|
||||||
|
|
||||||
}).onClose([]() {
|
|
||||||
|
|
||||||
//std::cout << "WebSocket disconnected from /wsApi" << std::endl;
|
|
||||||
|
|
||||||
}).listen("localhost", 3000, 0);
|
|
||||||
|
|
||||||
/*auto serve = [](auto *s, auto *req, auto *args) {
|
|
||||||
|
|
||||||
//std::cout << "URL: " << req->getUrl() << std::endl;
|
|
||||||
|
|
||||||
s->writeStatus("200 OK");
|
|
||||||
std::string_view file;
|
|
||||||
if (args->size() != 2) {
|
|
||||||
file = getFile("rocket.html");
|
|
||||||
} else {
|
|
||||||
file = getFile((*args)[1]);
|
|
||||||
|
|
||||||
std::string_view name = (*args)[1];
|
|
||||||
|
|
||||||
if (name.length() > 4 && name.substr(name.length() - 4) == ".svg") {
|
|
||||||
s->writeHeader("Content-type", "image/svg+xml");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
s->write([file](int offset) {
|
|
||||||
return std::string_view(file.data() + offset, file.size() - offset);
|
|
||||||
}, file.size());
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
app.onGet("/", serve).onGet("/:folder/:file", serve).onGet("/tiny", [](auto *s, auto *req, auto *args) {
|
|
||||||
respond<512>(s);
|
|
||||||
}).onGet("/small", [](auto *s, auto *req, auto *args) {
|
|
||||||
respond<4096>(s);
|
|
||||||
}).onGet("/medium", [](auto *s, auto *req, auto *args) {
|
|
||||||
respond<16384>(s);
|
|
||||||
}).onGet("/large", [](auto *s, auto *req, auto *args) {
|
|
||||||
respond<51200>(s);
|
|
||||||
}).onGet("/huge", [](auto *s, auto *req, auto *args) {
|
|
||||||
respond<52428800>(s);
|
|
||||||
}).onPost("/upload", [](auto *s, auto *req, auto *args) {
|
|
||||||
|
|
||||||
s->read([s](std::string_view chunk) {
|
|
||||||
std::cout << "Received chunk on URL /upload: <" << chunk << ">" << std::endl;
|
|
||||||
|
|
||||||
s->writeStatus("200 OK")->end("Thanks for posting!");
|
|
||||||
});
|
|
||||||
|
|
||||||
}).onWebSocket("/wsApi", []() {
|
|
||||||
|
|
||||||
}).onHttpConnection([](auto *s) {
|
|
||||||
std::cout << "Connections: " << ++connections << std::endl;
|
|
||||||
}).onHttpDisconnection([](auto *s) {
|
|
||||||
std::cout << "Connections: " << --connections << std::endl;
|
|
||||||
}).listen(nullptr, 3000, 0);*/
|
|
||||||
|
|
||||||
// loop.run();
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
#ifndef APP_H
|
||||||
|
#define APP_H
|
||||||
|
|
||||||
|
/* An app is a convenience wrapper of some of the most used fuctionalities and allows a
|
||||||
|
* builder-pattern kind of init. Apps operate on the implicit thread local Loop */
|
||||||
|
|
||||||
|
#include "HttpContext.h"
|
||||||
|
#include "HttpResponse.h"
|
||||||
|
|
||||||
|
namespace uWS {
|
||||||
|
template <bool SSL>
|
||||||
|
struct TemplatedApp {
|
||||||
|
private:
|
||||||
|
HttpContext<SSL> *httpContext;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
~TemplatedApp() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
TemplatedApp(const TemplatedApp &other) {
|
||||||
|
httpContext = other.httpContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
TemplatedApp(us_ssl_socket_context_options sslOptions) {
|
||||||
|
httpContext = uWS::HttpContext<SSL>::create(uWS::Loop::defaultLoop(), &sslOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
TemplatedApp &get(std::string pattern, std::function<void(HttpResponse<SSL> *, HttpRequest *)> handler) {
|
||||||
|
httpContext->onGet(pattern, handler);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
TemplatedApp &listen(int port, std::function<void(us_listen_socket *)> handler) {
|
||||||
|
handler(httpContext->listen(nullptr, port, 0));
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
TemplatedApp &run() {
|
||||||
|
uWS::run();
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef TemplatedApp<false> App;
|
||||||
|
typedef TemplatedApp<true> SSLApp;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // APP_H
|
||||||
@@ -137,6 +137,8 @@ public:
|
|||||||
/* We should only return with new writes, not things written to cork already */
|
/* We should only return with new writes, not things written to cork already */
|
||||||
return write(src, length, optionally, 0);
|
return write(src, length, optionally, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Drain any socket-buffer while also optionally sending a chunk */
|
/* Drain any socket-buffer while also optionally sending a chunk */
|
||||||
|
|||||||
+2
-2
@@ -199,8 +199,8 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Listen to port using this HttpContext */
|
/* Listen to port using this HttpContext */
|
||||||
void listen(const char *host, int port, int options) {
|
us_listen_socket *listen(const char *host, int port, int options) {
|
||||||
static_dispatch(us_ssl_socket_context_listen, us_socket_context_listen)(getSocketContext(), host, port, options, sizeof(HttpResponseData<SSL>));
|
return static_dispatch(us_ssl_socket_context_listen, us_socket_context_listen)(getSocketContext(), host, port, options, sizeof(HttpResponseData<SSL>));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -75,6 +75,13 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Convenience function for static data (I don't like this one!) */
|
||||||
|
void write(std::string_view data) {
|
||||||
|
write([data](int offset) {
|
||||||
|
return data.substr(offset);
|
||||||
|
}, data.length());
|
||||||
|
}
|
||||||
|
|
||||||
/* Attach a read handler for data sent. Will be called with a chunk of size 0 when FIN */
|
/* Attach a read handler for data sent. Will be called with a chunk of size 0 when FIN */
|
||||||
void read(std::function<void(std::string_view)> handler) {
|
void read(std::function<void(std::string_view)> handler) {
|
||||||
HttpResponseData<SSL> *data = getHttpResponseData();
|
HttpResponseData<SSL> *data = getHttpResponseData();
|
||||||
|
|||||||
@@ -44,10 +44,8 @@ public:
|
|||||||
if (!defaultLoop) {
|
if (!defaultLoop) {
|
||||||
ownsDefaultLoop = true;
|
ownsDefaultLoop = true;
|
||||||
defaultLoop = create(true);
|
defaultLoop = create(true);
|
||||||
std::cout << "Created default loop " << defaultLoop << " for thread " << std::this_thread::get_id() << std::endl;
|
|
||||||
return defaultLoop;
|
return defaultLoop;
|
||||||
} else if (ownsDefaultLoop) {
|
} else if (ownsDefaultLoop) {
|
||||||
std::cout << "Returned default loop " << defaultLoop << " for thread " << std::this_thread::get_id() << std::endl;
|
|
||||||
return defaultLoop;
|
return defaultLoop;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,10 +53,8 @@ public:
|
|||||||
static thread_local Loop *threadLocalLoop;
|
static thread_local Loop *threadLocalLoop;
|
||||||
if (!threadLocalLoop) {
|
if (!threadLocalLoop) {
|
||||||
threadLocalLoop = create(false);
|
threadLocalLoop = create(false);
|
||||||
std::cout << "Created non-default loop " << threadLocalLoop << " for thread " << std::this_thread::get_id() << std::endl;
|
|
||||||
return threadLocalLoop;
|
return threadLocalLoop;
|
||||||
}
|
}
|
||||||
std::cout << "Returned non-default loop " << threadLocalLoop << " for thread " << std::this_thread::get_id() << std::endl;
|
|
||||||
return threadLocalLoop;
|
return threadLocalLoop;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user