vessel/web/include/web.h
Arija A. bf87b89080
refact: Update macro formats.
Signed-off-by: Arija A. <ari@ari.lt>
2025-06-14 16:20:50 +03:00

78 lines
2.2 KiB
C

#ifndef VESSEL_WEB_WEB_H_
#define VESSEL_WEB_WEB_H_
#include "conf.h"
#include <vessel/hook.h>
#include <vessel/server.h>
#include "path.h"
#include "request.h"
#include <netinet/in.h>
#define VW_WEB_DEFAULT_CONTENT_TYPE "application/octet-stream"
typedef struct {
vw_HTTPRequest req;
vw_HTTPRequestParseError req_error;
vw_Path path;
vs_HMap params;
char ip[INET_ADDRSTRLEN];
vs_SockWorker *worker;
} vw_Proxy;
typedef enum {
vw_RouteType_normal = 0,
vw_RouteType_not_found,
vw_RouteType_before,
vw_RouteType_after,
vw_RouteType_cleanup,
vw_RouteType_error,
} vw_RouteType;
typedef struct {
const char *method;
const char *pattern;
const vw_RouteType type;
bool (*handler)(vw_Proxy *proxy);
vw_PathPattern *_path_pattern;
} vw_Route;
#define GET "GET" /* NOLINT(readability-identifier-naming) */
#define POST "POST" /* NOLINT(readability-identifier-naming) */
#define PUT "PUT" /* NOLINT(readability-identifier-naming) */
#define PATCH "PATCH" /* NOLINT(readability-identifier-naming) */
#define DELETE "DELETE" /* NOLINT(readability-identifier-naming) */
#define HEAD "HEAD" /* NOLINT(readability-identifier-naming) */
#define OPTIONS "OPTIONS" /* NOLINT(readability-identifier-naming) */
#define ANY NULL /* NOLINT(readability-identifier-naming) */
#define VW_NEW_ROUTE(method, pattern, type, handler) \
{ (method), (pattern), (vw_RouteType_##type), (handler), NULL }
typedef enum {
vw_RouteHookType_internal_error = 0,
vw_RouteHookType_bad_request,
vw_RouteHookType_no_route,
vw_RouteHookType_pre_read,
vw_RouteHookType_pre_routing,
} vw_RouteHookType;
VS_DECLARE_HOOKS(vw_Route, vw_RouteHookType, vs_SockWorker *);
typedef struct {
vw_Route *routes;
const vw_RouteHook *hooks;
} vw_Router;
#define VW_ROUTES(...) { __VA_ARGS__, { 0 } }
#define VW_WEBSERVER_HOOKS { vs_SockWorkerHookType_post_open, vw_WebServer_post_open }
bool vw_WebServer_post_open(vs_SockWorker *worker);
bool vw_WebServer_init(vs_SockServer *server, vw_Router *router);
bool vw_WebServer_destroy(vw_Router *router);
#endif /* VESSEL_WEB_WEB_H_ */