-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathController.h
More file actions
158 lines (135 loc) · 4.85 KB
/
Controller.h
File metadata and controls
158 lines (135 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifndef _MONGOOSE_CONTROLLER_H
#define _MONGOOSE_CONTROLLER_H
#include <map>
#include <iostream>
#include "Request.h"
#include "RequestHandler.h"
#include "StreamResponse.h"
#include "WebSocket.h"
#include "Sessions.h"
using namespace std;
#define addRoute(httpMethod, url, controllerType, method) \
registerRoute(httpMethod, url, new RequestHandler<controllerType, StreamResponse>(this, &controllerType::method ));
#define addRouteResponse(httpMethod, url, controllerType, method, responseType) \
registerRoute(httpMethod, url, new RequestHandler<controllerType, responseType>(this, &controllerType::method ));
/**
* A controller is a module that respond to requests
*
* You can override the preProcess, process and postProcess to answer to
* the requests
*/
namespace Mongoose
{
class Server;
class Controller
{
public:
Controller();
virtual ~Controller();
/**
* Sets the reference to the server hosting this controller
*
* @param Server the hosting server
*/
virtual void setServer(Server *server);
/**
* Called before a request is processed
*
* @param Request the request
* @param Response the response
*/
virtual void preProcess(Request &request, Response &response);
/**
* Called to process a request
*
* @param Request the request
*
* @return Response the created response, or NULL if the controller
* does not handle this request
*/
virtual Response *process(Request &request);
/**
* Called after a request is processed, if the controller responded
*
* @param Request the request
* @param Response the response
*/
virtual void postProcess(Request &request, Response &response);
/**
* Handle a request, this will try to match the request, if this
* controller handles it, it will preProcess, process then postProcess it
*
* @param Request the request
*
* @return Response the created response, or NULL if the controller
* does not handle this request
*/
virtual Response *handleRequest(Request &request);
/**
* Sets the controller prefix, for instance "/api"
*
* @param string the prefix of all urls for this controller
*/
void setPrefix(string prefix);
/**
* Called when a new websocket connection is ready
*
* @param WebSocket the instance of the connection
*/
virtual void webSocketReady(WebSocket *websocket);
/**
* Called when data arrive in a websocket connection
*
* @param WebSocket the instance of the connection
* @param string the data arriving
*/
virtual void webSocketData(WebSocket *websocket, string data);
/**
* Registers a route to the controller
*
* @param string the route path
* @param RequestHandlerBase the request handler for this route
*/
virtual void registerRoute(string httpMethod, string route, RequestHandlerBase *handler);
/**
* Initializes the route and settings
*/
virtual void setup();
/**
* Dump all routes
*/
void dumpRoutes();
/**
* Called when an exception occur during the rendering
*
* @param string the error message
*
* @return response a response to send, 404 will occur if NULL
*/
virtual Response *serverInternalError(string message);
/**
* Gets the session for a request/response
*
* @param Request the request
* @param Response the response
*
* @return Session the session for the request/response
*/
Session &getSession(Request &request, Response &response);
/**
* Sets the sessions
*
* @param Sessions* the pointer to the sessions jar
*/
void setSessions(Sessions *sessions);
bool handles(string method, string url);
vector<string> getUrls();
protected:
Sessions *sessions;
Server *server;
string prefix;
map<string, RequestHandlerBase*> routes;
vector<string> urls;
};
}
#endif