Add convenience wrapper App (again)
This commit is contained in:
@@ -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 */
|
||||
return write(src, length, optionally, 0);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Drain any socket-buffer while also optionally sending a chunk */
|
||||
|
||||
+2
-2
@@ -199,8 +199,8 @@ public:
|
||||
}
|
||||
|
||||
/* Listen to port using this HttpContext */
|
||||
void 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>));
|
||||
us_listen_socket *listen(const char *host, int port, int options) {
|
||||
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 */
|
||||
void read(std::function<void(std::string_view)> handler) {
|
||||
HttpResponseData<SSL> *data = getHttpResponseData();
|
||||
|
||||
@@ -44,10 +44,8 @@ public:
|
||||
if (!defaultLoop) {
|
||||
ownsDefaultLoop = true;
|
||||
defaultLoop = create(true);
|
||||
std::cout << "Created default loop " << defaultLoop << " for thread " << std::this_thread::get_id() << std::endl;
|
||||
return defaultLoop;
|
||||
} else if (ownsDefaultLoop) {
|
||||
std::cout << "Returned default loop " << defaultLoop << " for thread " << std::this_thread::get_id() << std::endl;
|
||||
return defaultLoop;
|
||||
}
|
||||
|
||||
@@ -55,10 +53,8 @@ public:
|
||||
static thread_local Loop *threadLocalLoop;
|
||||
if (!threadLocalLoop) {
|
||||
threadLocalLoop = create(false);
|
||||
std::cout << "Created non-default loop " << threadLocalLoop << " for thread " << std::this_thread::get_id() << std::endl;
|
||||
return threadLocalLoop;
|
||||
}
|
||||
std::cout << "Returned non-default loop " << threadLocalLoop << " for thread " << std::this_thread::get_id() << std::endl;
|
||||
return threadLocalLoop;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user