Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Provide a migration path from ESPAsyncWebServer (PoC) #115

Draft
wants to merge 1 commit into
base: v2-dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions src/PsychicESPAsyncWebServer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#pragma once

#include "PsychicEndpoint.h"
#include "PsychicEventSource.h"
#include "PsychicFileResponse.h"
#include "PsychicHandler.h"
#include "PsychicHttpServer.h"
#include "PsychicJson.h"
#include "PsychicRequest.h"
#include "PsychicResponse.h"
#include "PsychicStaticFileHandler.h"
#include "PsychicStreamResponse.h"
#include "PsychicUploadHandler.h"
#include "PsychicVersion.h"
#include "PsychicWebSocket.h"
#include <esp_err.h>
#include <http_status.h>

using ArRequestHandlerFunction = PsychicHttpRequestCallback;
using AsyncCallbackWebHandler = PsychicEndpoint;
using AsyncJsonResponse = PsychicJsonResponse;
using AsyncWebHandler = PsychicHandler;
using AsyncWebRewrite = PsychicRewrite;
using AsyncWebServerRequest = PsychicRequest;
using AsyncWebServerResponse = PsychicResponse;
using WebRequestMethodComposite = int;

#define PSYCHIC_OK ESP_OK
#define PSYCHIC_REQ request,

class AsyncWebServer
{
private:
PsychicHttpServer* _server;

public:
AsyncWebServer(uint16_t port = 80) : _server(new PsychicHttpServer(port)) {};
~AsyncWebServer() { delete _server; };

void begin() { _server->begin(); };
void end() { _server->end(); };

void reset() { _server->reset(); };

void onNotFound(ArRequestHandlerFunction fn) { _server->onNotFound(fn); };

AsyncCallbackWebHandler& on(const char* uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest) { return *_server->on(uri, method, onRequest); };

bool removeHandler(AsyncCallbackWebHandler* handler) { return _server->removeEndpoint(handler); };

AsyncWebHandler& addHandler(AsyncWebHandler* handler) { return *_server->addHandler(handler); };
bool removeHandler(AsyncWebHandler* handler)
{
_server->removeHandler(handler);
return true;
};

AsyncWebRewrite& addRewrite(AsyncWebRewrite* rewrite) { return *_server->addRewrite(rewrite); };
AsyncWebRewrite& rewrite(const char* from, const char* to) { return *_server->rewrite(from, to); };

// AsyncCallbackWebHandler& on(const char* uri, ArRequestHandlerFunction onRequest);
// AsyncCallbackWebHandler& on(const char* uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest, ArUploadHandlerFunction onUpload);
// AsyncCallbackWebHandler& on(const char* uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest, ArUploadHandlerFunction onUpload, ArBodyHandlerFunction onBody);

// AsyncStaticWebHandler& serveStatic(const char* uri, fs::FS& fs, const char* path, const char* cache_control = NULL);

// void onFileUpload(ArUploadHandlerFunction fn);
// void onRequestBody(ArBodyHandlerFunction fn);
// void reset();
};
4 changes: 4 additions & 0 deletions src/PsychicHttp.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@
#include "async_worker.h"
#endif

#ifdef PSYCHIC_TO_ESPASYNCWS
#include "PsychicESPAsyncWebServer.h"
#endif

#endif /* PsychicHttp_h */
4 changes: 4 additions & 0 deletions src/PsychicJson.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class PsychicJsonResponse : public PsychicResponseDelegate
size_t getLength();

virtual esp_err_t send() override;

#ifdef PSYCHIC_TO_ESPASYNCWS
void setLength() { setContentLength(measureJson(_root)); }
#endif
};

class PsychicJsonHandler : public PsychicWebHandler
Expand Down
10 changes: 10 additions & 0 deletions src/PsychicRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ class PsychicRequest
PsychicResponse* beginReply(int code, const char* contentType);
PsychicResponse* beginReply(int code, const char* contentType, const char* content);
PsychicResponse* beginReply(int code, const char* contentType, const uint8_t* content, size_t len);

#ifdef PSYCHIC_TO_ESPASYNCWS
esp_err_t send(int code) { return reply(code); }
esp_err_t send(int code, const char *contentType, const char *content) { return reply(code, contentType, content); }
esp_err_t send(int code, const char *contentType, const uint8_t *content, size_t len) { return reply(code, contentType, content, len); }
esp_err_t send(PsychicResponse* response) { return reply(response); }
PsychicResponse* beginResponse(int code) { return beginReply(code); }
PsychicResponse* beginResponse(int code, const char* contentType, const char* content) { return beginReply(code, contentType, content); }
PsychicResponse* beginResponse(int code, const char* contentType, const uint8_t* content, size_t len) { return beginReply(code, contentType, content, len); }
#endif
};

#endif // PsychicRequest_h
Loading