Skip to content

Commit

Permalink
qq
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Aug 13, 2024
1 parent d47a216 commit f3b9730
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
57 changes: 57 additions & 0 deletions src/PsychicESPAsyncWebServer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#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 AsyncCallbackWebHandler = PsychicEndpoint;
using AsyncWebServerRequest = PsychicRequest;
using AsyncWebServerResponse = PsychicResponse;
using AsyncJsonResponse = PsychicJsonResponse;
using ArRequestHandlerFunction = PsychicHttpRequestCallback;
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(); };
AsyncCallbackWebHandler& on(const char* uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest) { return *_server->on(uri, method, onRequest); };
void onNotFound(ArRequestHandlerFunction fn) { _server->onNotFound(fn); };
bool removeHandler(AsyncWebHandler* handler) { return _server->removeHandler(handler); return true; };

// 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);
// AsyncWebRewrite& addRewrite(AsyncWebRewrite* rewrite);
// AsyncWebRewrite& rewrite(const char* from, const char* to);
// bool removeRewrite(AsyncWebRewrite* rewrite);
// bool removeRewrite(const char* from, const char* to);
// AsyncWebHandler& addHandler(AsyncWebHandler* handler);

// 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();
};
10 changes: 8 additions & 2 deletions src/PsychicHttp.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

// #define ENABLE_ASYNC // This is something added in ESP-IDF 5.1.x where each request can be handled in its own thread

#include "PsychicVersion.h"
#include "PsychicEndpoint.h"
#include "PsychicEventSource.h"
#include "PsychicFileResponse.h"
Expand All @@ -15,11 +14,18 @@
#include "PsychicStaticFileHandler.h"
#include "PsychicStreamResponse.h"
#include "PsychicUploadHandler.h"
#include "PsychicVersion.h"
#include "PsychicWebSocket.h"
#include <http_status.h>

#ifdef ENABLE_ASYNC
#include "async_worker.h"
#endif

#endif /* PsychicHttp_h */
#ifdef PSYCHIC_TO_ESPASYNCWS
#include "PsychicESPAsyncWebServer.h"
#endif

#endif /* PsychicHttp_h */

// assert(a >= b && "a was less than b");
5 changes: 5 additions & 0 deletions src/PsychicHttpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class PsychicHttpServer
PsychicEndpoint* on(const char* uri, int method, PsychicHttpRequestCallback onRequest);
PsychicEndpoint* on(const char* uri, PsychicJsonRequestCallback onRequest);
PsychicEndpoint* on(const char* uri, int method, PsychicJsonRequestCallback onRequest);
void removeEndpoint(PsychicEndpoint* endpoint);

bool removeEndpoint(const char* uri, int method);
bool removeEndpoint(PsychicEndpoint* endpoint);
Expand All @@ -112,6 +113,10 @@ class PsychicHttpServer
void onClose(PsychicClientCallback handler);

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

#ifdef PSYCHIC_TO_ESPASYNCWS
void removeHandler(PsychicEndpoint* endpoint) { removeEndpoint(endpoint); }
#endif
};

bool ON_STA_FILTER(PsychicRequest* request);
Expand Down
4 changes: 4 additions & 0 deletions src/PsychicJson.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ class PsychicJsonResponse : public PsychicResponse
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 @@ -149,6 +149,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

0 comments on commit f3b9730

Please sign in to comment.