lintel 0.2.0
Loading...
Searching...
No Matches
Classes | Namespaces | Macros
Controller.h File Reference
#include <httplib.h>
#include <cstddef>
#include <exception>
#include <functional>
#include <map>
#include <memory>
#include <optional>
#include <type_traits>
#include <vector>
#include "lintel/core/exceptions/HttpBadRequestException.h"
#include "lintel/core/services/LoggerService.h"
#include "lintel/core/utils/StringUtils.h"
#include "lintel/features/base/provider/GroupProvider.h"
#include "lintel/features/base/services/IAuthService.h"
#include "lintel/features/http/service/ClientIpResolver.h"
#include "lintel/features/http/service/ContentType.h"
#include "lintel/features/http/service/HttpStatusCodes.h"

Go to the source code of this file.

Classes

struct  lintel::PagingParams
 
class  lintel::Controller
 
struct  lintel::Controller::Method
 

Namespaces

namespace  lintel
 

Macros

#define ADD_HANDLER_METHOD(pattern, httpType, name)
 
#define ADD_HANDLER_CONTENT_READER_METHOD(pattern, httpType, name)
 

Macro Definition Documentation

◆ ADD_HANDLER_CONTENT_READER_METHOD

#define ADD_HANDLER_CONTENT_READER_METHOD (   pattern,
  httpType,
  name 
)
Value:
HandlerWithContentReader name##httpType##Function = \
addMethod<std::function<HandlerWithContentReaderArgs>, httpType>(pattern, \
[&](const httplib::Request &request, httplib::Response &response, const httplib::ContentReader &contentReader) { \
const std::string contentTypeString = request.get_header_value("Content-Type"); \
lintel::ContentType contentType(contentTypeString); \
try { \
name##httpType(request, response, contentReader, contentType); \
} catch (const std::exception &e) { \
LOG_ERROR("{}: {}", #name, e.what()); \
response.set_content("internal server error", "text/plain"); \
} \
}); \
void name##httpType(const httplib::Request &request, \
httplib::Response &response, \
const httplib::ContentReader &contentReader, \
const lintel::ContentType &contentType)
Definition ContentType.h:11
@ InternalServerError
Definition HttpStatusCodes.h:69

◆ ADD_HANDLER_METHOD

#define ADD_HANDLER_METHOD (   pattern,
  httpType,
  name 
)
Value:
void name##httpType(const httplib::Request &request, \
httplib::Response &response, \
const lintel::ContentType &contentType, \
const std::optional<lintel::UserToken> &user); \
Handler name##httpType##Function = addMethod<std::function<HandlerArgs>, httpType>( \
pattern, [&](const httplib::Request &request, httplib::Response &response) { \
std::string auth = request.get_header_value("Authorization"); \
std::string contentTypeString = request.get_header_value("Content-Type"); \
if (contentTypeString.empty()) { contentTypeString = "application/json"; } \
lintel::ContentType contentType(contentTypeString); \
std::optional<lintel::UserToken> user = std::nullopt; \
try { \
if (lintel::StringUtils::startsWith(auth, "Bearer ") && auth.size() > 7) { \
std::string id = auth.substr(7); \
lintel::UserTokenLogin userTokenLogin{ clientIpOf(request), id }; \
user = m_authService->onAccessOf(userTokenLogin); \
} \
name##httpType(request, response, contentType, user); \
} catch (const lintel::HttpBadRequestException &e) { \
LOG_ERROR("{}: {}", #name, e.what()); \
response.set_content("bad request", "text/plain"); \
} catch (const std::exception &e) { \
LOG_ERROR("{}: {}", #name, e.what()); \
response.set_content("internal server error", "text/plain"); \
} \
})
Definition HttpBadRequestException.h:9
const char * what() const noexcept override
Definition HttpBadRequestException.h:13
@ BadRequest
Definition HttpStatusCodes.h:36
static constexpr bool startsWith(std::string_view s, std::string_view start)
Definition StringUtils.h:33
Definition IAuthService.h:23