Merge develop into main (#377)
- Extract web server from `DevelopmentService` into a separate class: `HttpServer` - Export more functions in `tt_init.cpp`
This commit is contained in:
committed by
GitHub
parent
d8346998ce
commit
9c5a427a34
@@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
#ifdef ESP_PLATFORM
|
||||
|
||||
#include <esp_http_server.h>
|
||||
#include <Tactility/Mutex.h>
|
||||
#include <Tactility/kernel/SystemEvents.h>
|
||||
|
||||
namespace tt::network {
|
||||
|
||||
class HttpServer {
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* @brief Function for URI matching used by server.
|
||||
*
|
||||
* @param[in] referenceUri URI/template with respect to which the other URI is matched
|
||||
* @param[in] uriToCheck URI/template being matched to the reference URI/template
|
||||
* @param[in] matchUpTo For specifying the actual length of `uri_to_match` up to
|
||||
* which the matching algorithm is to be applied (The maximum
|
||||
* value is `strlen(uri_to_match)`, independent of the length
|
||||
* of `reference_uri`)
|
||||
* @return true on match
|
||||
*/
|
||||
typedef bool (*UriMatchFunction)(const char* referenceUri, const char* uriToCheck, size_t matchUpTo);
|
||||
|
||||
private:
|
||||
|
||||
const uint32_t port;
|
||||
const std::string address;
|
||||
const uint32_t stackSize;
|
||||
const UriMatchFunction matchUri;
|
||||
|
||||
std::vector<httpd_uri_t> handlers;
|
||||
|
||||
Mutex mutex = Mutex(Mutex::Type::Recursive);
|
||||
httpd_handle_t server = nullptr;
|
||||
|
||||
bool startInternal();
|
||||
void stopInternal();
|
||||
|
||||
public:
|
||||
|
||||
HttpServer(
|
||||
uint32_t port,
|
||||
const std::string& address,
|
||||
std::vector<httpd_uri_t> handlers,
|
||||
uint32_t stackSize = 5120,
|
||||
UriMatchFunction matchUri = httpd_uri_match_wildcard
|
||||
) :
|
||||
port(port),
|
||||
address(address),
|
||||
stackSize(stackSize),
|
||||
matchUri(matchUri),
|
||||
handlers(handlers)
|
||||
{}
|
||||
|
||||
void start();
|
||||
|
||||
void stop();
|
||||
|
||||
bool isStarted() const {
|
||||
auto lock = mutex.asScopedLock();
|
||||
lock.lock();
|
||||
return server != nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user