Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
ipgallery.common.cpp
/
Microservice
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Registry
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
f95791c1
authored
Dec 20, 2016
by
amir
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Change HttpMongooseImpl to work with the reactor
parent
da0c3d01
Show whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
286 additions
and
50 deletions
src/Microservice_App.cpp
src/Microservice_App.h
src/Microservice_Client.cpp
src/common/Microservice_Defines.h
src/common/Microservice_Iface.h
src/common/Microservice_RequestContext.h
src/handlers/Microservice_BaseHandler.h
src/handlers/Microservice_MonitorHandler.cpp
src/handlers/Microservice_RMQHandler.cpp
src/handlers/Microservice_Reactor.cpp
src/handlers/Microservice_Reactor.h
src/handlers/Microservice_RestHandler.cpp
src/impl/MSIMetricsFactoryDropwisardImpl.cpp
src/impl/MSIMetricsFactoryDropwisardImpl.h
src/impl/servers/Microservice_IRestServerMongooseImpl.cpp
src/impl/servers/Microservice_IRestServerMongooseImpl.h
src/Microservice_App.cpp
View file @
f95791c1
...
...
@@ -42,7 +42,7 @@ Microservice_App::Microservice_App(const char* appName) {
mpc_PubSubClient
=
nullptr
;
mpc_ServiceDiscovery
=
nullptr
;
enableMetrics
=
false
;
metricsFactory_
=
nullptr
;
p_
metricsFactory_
=
nullptr
;
mc_AppName
.
assign
(
appName
);
sp_instance
=
this
;
}
...
...
@@ -272,8 +272,9 @@ Microservice_App& Microservice_App::build() {
if
(
enableMetrics
)
{
metricsFactory_
=
new
MSIMetricsFactoryDropwisardImpl
(
this
->
mc_AppName
);
metricsFactory_
->
startReporting
();
p_metricsFactory_
=
new
MSIMetricsFactoryDropwisardImpl
(
this
->
mc_AppName
);
p_metricsFactory_
->
startReporting
();
reactor_
.
withMetrics
(
p_metricsFactory_
);
}
mpc_Configuration
->
Reload
();
/*
...
...
@@ -311,7 +312,7 @@ Microservice_App& Microservice_App::build() {
// specific
IRestServer
*
p_restServer
=
dynamic_cast
<
IRestServer
*>
(
p_server
);
if
(
p_restServer
)
{
p_restServer
->
build
(
this
->
mc_AppName
,
reactor_
.
HandlersMap
(),
mpc_Logger
,
mpc_PubSubClient
,
metricsFactory_
);
p_restServer
->
build
(
this
->
mc_AppName
,
reactor_
.
HandlersMap
(),
mpc_Logger
,
mpc_PubSubClient
,
p_
metricsFactory_
);
}
p_server
->
init
();
}
...
...
src/Microservice_App.h
View file @
f95791c1
...
...
@@ -56,7 +56,7 @@ private:
IPubSub
*
mpc_PubSubClient
;
IConfiguration
*
mpc_Configuration
;
cMicroservice_MonitorHandler
*
mpc_MonitorHandler
;
IMetricsFactory
*
metricsFactory_
;
IMetricsFactory
*
p_
metricsFactory_
;
// servers
// cMicroservice_RestServer* mpc_RestServer;
// cMicroservice_RMQServer* mpc_RMQServer;
...
...
@@ -154,7 +154,7 @@ public:
return
mc_ClientMap
[
ms_name
];
}
std
::
map
<
std
::
string
,
cMicroservice_Client
*>&
GetClientMap
()
{
return
mc_ClientMap
;
}
IMetricsFactory
*
GetMetricsFactory
()
const
{
return
metricsFactory_
;
}
IMetricsFactory
*
GetMetricsFactory
()
const
{
return
p_
metricsFactory_
;
}
};
...
...
src/Microservice_Client.cpp
View file @
f95791c1
...
...
@@ -119,6 +119,7 @@ MSRetStat cMicroservice_Client::Unsubscribe(std::string &topic) {
}
void
cMicroservice_Client
::
GetMetrics
(
std
::
map
<
std
::
string
,
long
>&
metrics_map
)
{
if
(
p_commandClient_
)
p_commandClient_
->
GetMetrics
(
metrics_map
);
}
...
...
src/common/Microservice_Defines.h
View file @
f95791c1
...
...
@@ -13,6 +13,7 @@
#endif
#include <array>
#include <map>
/*
* constants
*/
...
...
@@ -94,6 +95,13 @@ static char gbaa_Microservice_MethodNames[cMicroservice_Enums::eMaxMethods][nsMi
"DELETE"
};
static
std
::
map
<
std
::
string
,
cMicroservice_Enums
::
eCrudMethod
>
_microservice_RestCrudMap
=
{
{
"GET"
,
cMicroservice_Enums
::
eCrudMethod
::
eRead
},
{
"POST"
,
cMicroservice_Enums
::
eCrudMethod
::
eCreate
},
{
"PUT"
,
cMicroservice_Enums
::
eCrudMethod
::
eUpdate
},
{
"DELETE"
,
cMicroservice_Enums
::
eCrudMethod
::
eDelete
},
};
static
std
::
array
<
std
::
string
,
cMicroservice_Enums
::
eTrace
+
1
>
_microservice_LogLevels
=
{
...
...
src/common/Microservice_Iface.h
View file @
f95791c1
...
...
@@ -195,6 +195,7 @@ namespace nsMicroservice_Iface
virtual
ITimer
*
createTimer
(
std
::
string
&
name
)
=
0
;
virtual
IHistogram
*
createHistogram
(
std
::
string
&
name
)
=
0
;
virtual
void
GetMetrics
(
std
::
map
<
std
::
string
,
long
>&
metrics_map
)
=
0
;
virtual
void
meterMark
(
std
::
string
&
name
)
=
0
;
};
...
...
src/common/Microservice_RequestContext.h
View file @
f95791c1
...
...
@@ -37,16 +37,16 @@ public:
nsMicroservice_Iface
::
IContainer
*
mpti_Container
;
nsMicroservice_Iface
::
IResponse
*
mpti_Response
;
nsMicroservice_Iface
::
IRequest
*
mpti_Request
;
JsonStringWriter
*
mpc_Writer
;
//
JsonStringWriter* mpc_Writer;
cMicroservice_RequestContext
(
nsMicroservice_Iface
::
IContainer
*
pti_Container
,
JsonStringWriter
*
pc_Writer
,
//
JsonStringWriter* pc_Writer,
nsMicroservice_Iface
::
IResponse
*
pti_Response
,
nsMicroservice_Iface
::
IRequest
*
pti_Request
)
:
mpti_Response
(
pti_Response
),
mpti_Request
(
pti_Request
),
crudMethod
(
cMicroservice_Enums
::
eCrudMethod
::
eMaxMethods
)
{
mpti_Container
=
pti_Container
;
mpc_Writer
=
pc_Writer
;
//
mpc_Writer = pc_Writer;
if
(
mc_Params
.
capacity
()
<
nsMicroservice_Constants
::
MAX_PARAMS
)
mc_Params
.
reserve
(
nsMicroservice_Constants
::
MAX_PARAMS
);
}
...
...
@@ -68,7 +68,7 @@ public:
this
->
mpti_Container
=
requestContext
.
mpti_Container
;
this
->
mpti_Response
=
requestContext
.
mpti_Response
;
this
->
mpti_Request
=
requestContext
.
mpti_Request
;
this
->
mpc_Writer
=
requestContext
.
mpc_Writer
;
//
this->mpc_Writer = requestContext.mpc_Writer;
}
virtual
uint32_t
GetTypeHash
()
override
{
return
TYPE_HASH
;
}
...
...
src/handlers/Microservice_BaseHandler.h
View file @
f95791c1
...
...
@@ -17,6 +17,7 @@ class cMicroservice_BaseHandler : public nsMicroservice_Iface::IHandler
{
protected
:
JsonStringWriter
*
mpc_Writer
;
public
:
virtual
~
cMicroservice_BaseHandler
()
{
// mpc_Configuration = nullptr;
...
...
src/handlers/Microservice_MonitorHandler.cpp
View file @
f95791c1
...
...
@@ -36,7 +36,7 @@ void cMicroservice_MonitorHandler::Init() {
void
cMicroservice_MonitorHandler
::
DoCreate
(
cMicroservice_RequestContext
*
pc_reqCtx
)
{
auto
api_param
=
pc_reqCtx
->
mc_Params
[
0
];
if
(
api_param
.
compare
(
LOG_LEVEL
))
if
(
api_param
.
compare
(
LOG_LEVEL
)
==
0
)
HandleSetLogLevel
(
pc_reqCtx
);
}
...
...
@@ -55,7 +55,7 @@ void cMicroservice_MonitorHandler::DoRead(cMicroservice_RequestContext* pc_reqCt
HandleReload
(
pc_reqCtx
);
else
if
(
api_param
.
compare
(
STATS
)
==
0
)
HandleStatistics
(
pc_reqCtx
);
else
if
(
api_param
.
compare
(
LOG_LEVEL
))
else
if
(
api_param
.
compare
(
LOG_LEVEL
)
==
0
)
HandleReadLogLevel
(
pc_reqCtx
);
}
...
...
src/handlers/Microservice_RMQHandler.cpp
View file @
f95791c1
...
...
@@ -30,7 +30,7 @@ mpc_Handler(pc_Handler)
mpc_Buffer
=
new
rapidjson
::
StringBuffer
(
0
,
nsMicroservice_Constants
::
MAX_JSON_BUFFER
);
mpc_Writer
=
new
JsonStringWriter
(
*
mpc_Buffer
);
mpc_RequestContext
=
new
cMicroservice_RequestContext
(
this
,
mpc_Writer
,
//
mpc_Writer,
new
cMicroservice_IResponseRMQImpl
(),
new
cMicroservice_IRequestRMQImpl
());
this
->
apiContextPath
=
apiContextPath
;
...
...
src/handlers/Microservice_Reactor.cpp
View file @
f95791c1
...
...
@@ -9,6 +9,9 @@ MSRetStat Microservice_Reactor::Delegate(std::string& key, nsMicroservice_Iface:
auto
iter
=
handlersMap_
.
find
(
key
);
if
(
iter
!=
handlersMap_
.
end
())
{
if
(
p_metrics_factory_
){
p_metrics_factory_
->
meterMark
(
key
);
}
retStat
=
iter
->
second
->
Handle
(
p_Ctx
);
}
else
{
retStat
.
SetError
(
std
::
string
(
nsMicroservice_Constants
::
FAILED_TO_FIND_HANDLER
).
append
(
key
));
...
...
@@ -17,3 +20,7 @@ MSRetStat Microservice_Reactor::Delegate(std::string& key, nsMicroservice_Iface:
return
retStat
;
}
void
Microservice_Reactor
::
RegisterHandler
(
std
::
string
key
,
nsMicroservice_Iface
::
IHandler
*
p_Handler
)
{
handlersMap_
[
key
]
=
p_Handler
;
}
src/handlers/Microservice_Reactor.h
View file @
f95791c1
...
...
@@ -16,9 +16,9 @@
class
Microservice_Reactor
{
public
:
void
RegisterHandler
(
std
::
string
key
,
nsMicroservice_Iface
::
IHandler
*
p_Handler
)
{
handlersMap_
[
key
]
=
p_Handler
;
}
Microservice_Reactor
()
:
p_metrics_factory_
(
nullptr
){}
void
RegisterHandler
(
std
::
string
key
,
nsMicroservice_Iface
::
IHandler
*
p_Handler
);
MSRetStat
Delegate
(
std
::
string
&
key
,
nsMicroservice_Iface
::
IContext
*
p_Ctx
);
...
...
@@ -26,8 +26,14 @@ public:
return
handlersMap_
;
};
Microservice_Reactor
&
withMetrics
(
nsMicroservice_Iface
::
IMetricsFactory
*
p_metrics
){
p_metrics_factory_
=
p_metrics
;
return
*
this
;
}
private
:
std
::
map
<
std
::
string
,
nsMicroservice_Iface
::
IHandler
*>
handlersMap_
;
nsMicroservice_Iface
::
IMetricsFactory
*
p_metrics_factory_
;
};
...
...
src/handlers/Microservice_RestHandler.cpp
View file @
f95791c1
...
...
@@ -27,7 +27,7 @@ cMicroservice_RestHandler::cMicroservice_RestHandler(std::string apiContextPath,
mpc_Buffer
=
new
rapidjson
::
StringBuffer
(
0
,
nsMicroservice_Constants
::
MAX_JSON_BUFFER
);
mpc_Writer
=
new
JsonStringWriter
(
*
mpc_Buffer
);
mpc_RequestContext
=
new
cMicroservice_RequestContext
(
this
,
mpc_Writer
,
//
mpc_Writer,
new
cMicroservice_IResponseRestImpl
(),
new
cMicroservice_IRequestRestImpl
());
this
->
apiContextPath
=
apiContextPath
;
...
...
src/impl/MSIMetricsFactoryDropwisardImpl.cpp
View file @
f95791c1
...
...
@@ -116,7 +116,7 @@ void MSIMetricsFactoryDropwisardImpl::GetMetrics(std::map<std::string, long>& me
{
register
auto
meter_ptr
=
meter
.
second
;
str
.
assign
(
"meter."
+
meter
.
first
);
metrics_map
[
str
+
"count"
]
=
meter_ptr
->
getCount
();
metrics_map
[
str
+
"
.
count"
]
=
meter_ptr
->
getCount
();
metrics_map
[
str
+
".mean_rate"
]
=
meter_ptr
->
getMeanRate
();
metrics_map
[
str
+
".1m_rate"
]
=
meter_ptr
->
getOneMinuteRate
();
metrics_map
[
str
+
".5m_rate"
]
=
meter_ptr
->
getFiveMinuteRate
();
...
...
@@ -175,3 +175,7 @@ void MSIMetricsFactoryDropwisardImpl::ReportToGraphite() {
}
}
}
void
MSIMetricsFactoryDropwisardImpl
::
meterMark
(
std
::
string
&
name
)
{
registry_
->
meter
(
name
.
c_str
())
->
mark
();
}
src/impl/MSIMetricsFactoryDropwisardImpl.h
View file @
f95791c1
...
...
@@ -45,6 +45,8 @@ public:
void
startReporting
()
override
;
void
stopReporting
()
override
;
virtual
void
meterMark
(
std
::
string
&
name
)
override
;
void
GetMetrics
(
std
::
map
<
std
::
string
,
long
>&
metrics_map
)
override
;
cppmetrics
::
core
::
MetricRegistryPtr
GetRegistry
()
const
{
...
...
src/impl/servers/Microservice_IRestServerMongooseImpl.cpp
View file @
f95791c1
...
...
@@ -20,6 +20,8 @@
#include <signal.h>
#include <handlers/Microservice_RestHandler.h>
#include <handlers/Microservice_Reactor.h>
#include <impl/Microservice_IResponseRestImpl.h>
#include <impl/Microservice_IRequestRestImpl.h>
static
const
int
POLLING_INTERVAL
=
100
;
// IN MILLI
static
int
s_sig_num
=
0
;
...
...
@@ -53,7 +55,9 @@ cMicroservice_IRestServerMongooseImpl::cMicroservice_IRestServerMongooseImpl(cMi
{
serverType_
.
assign
(
getType
());
mpc_Param
=
pc_Param
;
mc_HandlersMap
.
clear
();
// mc_HandlersMap.clear();
p_requestRestImpl_
=
new
cMicroservice_IRequestRestImpl
();
p_restResponseImpl_
=
new
cMicroservice_IResponseRestImpl
();
}
cMicroservice_IRestServerMongooseImpl
::
cMicroservice_IRestServerMongooseImpl
(
const
cMicroservice_IRestServerMongooseImpl
&
orig
)
{
...
...
@@ -101,14 +105,14 @@ bool cMicroservice_IRestServerMongooseImpl::build(std::string& appName,
/*
* creating a map of rest handlers
*/
for
(
auto
prfxHandler
:
msHandlersMap
)
{
cMicroservice_RestHandler
*
pc_RestHandler
=
new
cMicroservice_RestHandler
(
prfxHandler
.
first
,
dynamic_cast
<
cMicroservice_BaseHandler
*>
(
prfxHandler
.
second
));
pc_RestHandler
->
withLogger
(
pc_Logger
);
pc_RestHandler
->
withPubSub
(
pc_PubSub
);
pc_RestHandler
->
withMetrics
(
p_metrics_factory
);
this
->
mc_HandlersMap
[
prfxHandler
.
first
]
=
pc_RestHandler
;
}
//
for (auto prfxHandler : msHandlersMap)
//
{
//
cMicroservice_RestHandler* pc_RestHandler = new cMicroservice_RestHandler(prfxHandler.first,dynamic_cast<cMicroservice_BaseHandler*>(prfxHandler.second));
//
pc_RestHandler->withLogger(pc_Logger);
//
pc_RestHandler->withPubSub(pc_PubSub);
//
pc_RestHandler->withMetrics(p_metrics_factory);
// //
this->mc_HandlersMap[prfxHandler.first] = pc_RestHandler;
//
}
result
=
true
;
}
}
...
...
@@ -151,22 +155,22 @@ void cMicroservice_IRestServerMongooseImpl::HandleRequest(mg_connection* conn, h
const
char
*
pba_NextSlash
=
strchr
(
pba_Uri
+
1
,
'/'
);
if
(
pba_NextSlash
)
{
std
::
string
apiContextPath
(
pba_Uri
,(
int
)(
pba_NextSlash
-
pba_Uri
));
std
::
string
key
(
nsMicroservice_Iface
::
IRestServer
::
TYPE
);
key
.
append
(
nsMicroservice_Constants
::
TYPE_PREFIX_SEPERATOR
).
append
(
pba_Uri
,(
int
)(
pba_NextSlash
-
pba_Uri
)
);
key
.
append
(
nsMicroservice_Constants
::
TYPE_PREFIX_SEPERATOR
).
append
(
apiContextPath
);
if
(
p_reactor_
)
HandleNewRequest
(
conn
,
msg
);
HandleNewRequest
(
conn
,
key
,
msg
,
apiContextPath
);
else
SendGeneralError
(
conn
,
500
,
"Missing Reactor body!"
);
auto
iter
=
mc_HandlersMap
.
find
(
key
);
if
(
iter
!=
mc_HandlersMap
.
end
())
{
cMicroservice_RestHandler
*
pc_Handler
=
iter
->
second
;
//mc_HandlerIterator->second;
pc_Handler
->
HandleRequest
(
conn
,
msg
);
}
else
SendNotImplemented
(
conn
);
//
auto iter = mc_HandlersMap.find(key);
//
if (iter != mc_HandlersMap.end())
//
{
//
cMicroservice_RestHandler* pc_Handler = iter->second;//mc_HandlerIterator->second;
//
pc_Handler->HandleRequest(conn,msg);
//
}
//
else
//
SendNotImplemented(conn);
}
else
SendNotImplemented
(
conn
);
...
...
@@ -195,15 +199,17 @@ bool cMicroservice_IRestServerMongooseImpl::init() {
return
true
;
}
void
cMicroservice_IRestServerMongooseImpl
::
HandleNewRequest
(
mg_connection
*
p_connection
,
http_message
*
p_message
)
{
void
cMicroservice_IRestServerMongooseImpl
::
HandleNewRequest
(
mg_connection
*
p_connection
,
std
::
string
key
,
http_message
*
p_message
,
std
::
string
&
apiContextPath
)
{
MSRetStat
retStat
;
// cMicroservice_RequestContext ctx(this,
// p_writer_,
// p_restResponseImpl_,
// p_requestRestImpl_);
// retStat = parser_.parse(p_message, ctx);
// if (retStat.IsSuccess())
// p_reactor_->Delegate(serverType_, &ctx);
cMicroservice_RequestContext
ctx
(
this
,
p_restResponseImpl_
,
p_requestRestImpl_
);
retStat
=
ParseRequest
(
p_connection
,
p_message
,
ctx
,
apiContextPath
);
if
(
retStat
.
IsSuccess
())
p_reactor_
->
Delegate
(
key
,
&
ctx
);
else
SendGeneralError
(
p_connection
,
500
,
"Failed in parsing...yored lekafa.."
);
}
...
...
@@ -212,4 +218,174 @@ cMicroservice_IRestServerMongooseImpl::SendGeneralError(mg_connection *p_connect
mg_printf
(
p_connection
,
"HTTP/1.0 %u %s
\r\n
Content-Length: 0
\r\n\r\n
"
,
respCode
,
error
);
}
void
cMicroservice_IRestServerMongooseImpl
::
SendErrorResp
(
nsMicroservice_Iface
::
IResponse
*
pti_Response
,
std
::
string
error
)
{
/*
* create error rest response
*/
std
::
ostringstream
c_OutputStream
;
c_OutputStream
<<
nsMicroservice_Constants
::
ERROR_REST_RESPONSE_TEMPLATE_PREFIX
<<
error
.
c_str
()
<<
nsMicroservice_Constants
::
ERROR_REST_RESPONSE_TEMPLATE_SUFFIX
<<
'}'
;
/*
* send it
*/
//pti_Response->Send(mba_ErrorBuff);
pti_Response
->
Send
(
c_OutputStream
.
str
().
c_str
());
}
void
cMicroservice_IRestServerMongooseImpl
::
WriteObjectToResponse
(
nsMicroservice_Iface
::
IResponse
*
pti_Response
,
rapidjson
::
Document
&
t_ObjectDoc
)
{
std
::
ostringstream
c_OutputStream
;
if
(
!
t_ObjectDoc
.
IsNull
())
{
rapidjson
::
StringBuffer
buffer
;
rapidjson
::
Writer
<
rapidjson
::
StringBuffer
>
writer
(
buffer
);
t_ObjectDoc
.
Accept
(
writer
);
c_OutputStream
<<
nsMicroservice_Constants
::
SUCCESS_REST_RESPONSE_TEMPLATE
<<
buffer
.
GetString
()
<<
'}'
;
}
else
{
c_OutputStream
<<
nsMicroservice_Constants
::
SUCCESS_NULL_REST_RESPONSE_TEMPLATE
<<
'}'
;
}
pti_Response
->
Send
(
c_OutputStream
.
str
().
c_str
());
}
void
cMicroservice_IRestServerMongooseImpl
::
WriteObjectToResponse
(
nsMicroservice_Iface
::
IResponse
*
pti_Response
,
cMicroservice_BaseRestResponse
&
t_BaseRestResponse
)
{
if
(
t_BaseRestResponse
.
IsSuccess
()){
WriteObjectToResponse
(
pti_Response
,
t_BaseRestResponse
.
GetObjectNode
());
}
else
{
SendErrorResp
(
pti_Response
,
t_BaseRestResponse
.
GetError
());
}
}
void
cMicroservice_IRestServerMongooseImpl
::
WriteStringToResponse
(
nsMicroservice_Iface
::
IResponse
*
pti_Response
,
const
char
*
pba_Doc
)
{
std
::
ostringstream
c_OutputStream
;
c_OutputStream
<<
nsMicroservice_Constants
::
SUCCESS_REST_RESPONSE_TEMPLATE
<<
pba_Doc
<<
'}'
;
pti_Response
->
Send
(
c_OutputStream
.
str
().
c_str
());
}
bool
cMicroservice_IRestServerMongooseImpl
::
ReadObjectFromRequest
(
nsMicroservice_Iface
::
IRequest
*
pti_Request
,
rapidjson
::
Document
&
t_ObjectDoc
)
{
const
char
*
pba_Content
=
pti_Request
->
GetContent
();
if
(
pba_Content
)
{
if
(
!
t_ObjectDoc
.
Parse
<
0
>
(
pba_Content
).
HasParseError
())
return
true
;
}
return
false
;
}
MSRetStat
cMicroservice_IRestServerMongooseImpl
::
ParseRequest
(
mg_connection
*
p_conn
,
http_message
*
p_message
,
cMicroservice_RequestContext
&
ctx
,
std
::
string
&
apiContextPath
)
{
/*
* get request context
*/
/*
* getting/setting request/response ifaces
*/
((
cMicroservice_IRequestRestImpl
*
)
ctx
.
mpti_Request
)
->
SetMsg
(
p_message
);
((
cMicroservice_IResponseRestImpl
*
)
ctx
.
mpti_Response
)
->
setConn
(
p_conn
);
/*
* getting params
*/
memcpy
(
mba_Buff
,
p_message
->
uri
.
p
,
p_message
->
uri
.
len
);
mba_Buff
[
p_message
->
uri
.
len
]
=
CNULL
;
char
*
pba_ParamsStr
=
&
mba_Buff
[
apiContextPath
.
length
()];
char
*
pba_token
=
strtok
(
pba_ParamsStr
,
nsMicroservice_Constants
::
SLASH_SEPERATOR
);
while
(
pba_token
)
{
ctx
.
mc_Params
.
push_back
(
pba_token
);
pba_token
=
strtok
(
NULL
,
nsMicroservice_Constants
::
SLASH_SEPERATOR
);
}
/*
* getting query parameters
*/
GetQueryParams
(
ctx
,
p_message
);
/*
* Log request
*/
LogRequest
(
p_message
);
/**
* get crud method
*/
ctx
.
crudMethod
=
GetCrudMethod
(
p_message
);
return
MSRetStat
();
}
void
cMicroservice_IRestServerMongooseImpl
::
LogRequest
(
http_message
*
p_msg
)
{
if
(
mpc_Logger
->
getLevel
()
==
cMicroservice_Enums
::
eLogLevel
::
eDebug
)
{
std
::
string
str
(
"Received request: "
);
str
.
append
(
p_msg
->
method
.
p
,
p_msg
->
method
.
len
);
str
.
append
(
", uri: "
).
append
(
p_msg
->
uri
.
p
,
p_msg
->
uri
.
len
);
if
(
p_msg
->
query_string
.
p
)
str
.
append
(
", query string: "
).
append
(
p_msg
->
query_string
.
p
,
p_msg
->
query_string
.
len
);
mpc_Logger
->
debug
(
str
);
}
}
void
cMicroservice_IRestServerMongooseImpl
::
GetQueryParams
(
cMicroservice_RequestContext
&
ctx
,
http_message
*
p_msg
)
{
/*
* getting query parameters
*/
if
(
!
p_msg
->
query_string
.
p
)
return
;
DequeStringMap
*
pc_queryParams
=
&
ctx
.
mc_QueryParameters
;
memcpy
(
mba_Buff
,
p_msg
->
query_string
.
p
,
p_msg
->
query_string
.
len
);
mba_Buff
[
p_msg
->
query_string
.
len
]
=
CNULL
;
//strlcpy(mba_Buff, p_msg->query_string.p,p_msg->query_string.len + 1);//nsMicroservice_Constants::MAX_URI_LENGTH);
char
*
pba_token
=
strtok
(
mba_Buff
,
nsMicroservice_Constants
::
AND_SEPERATOR
);
while
(
pba_token
)
{
// x=y or just x
char
*
pba_Equal
=
strchr
(
pba_token
,
'='
);
if
(
pba_Equal
)
{
*
pba_Equal
=
CNULL
;
DequeStringMap
::
iterator
t_QueryParamIter
=
pc_queryParams
->
find
(
pba_token
);
if
(
t_QueryParamIter
!=
pc_queryParams
->
end
())
{
// existing query key >> adding to deque
t_QueryParamIter
->
second
.
push_back
(
pba_Equal
+
1
);
}
else
{
// new one
std
::
deque
<
std
::
string
>
t_QueryDeque
;
t_QueryDeque
.
push_back
(
pba_Equal
+
1
);
(
*
pc_queryParams
)[
pba_token
]
=
t_QueryDeque
;
}
}
else
{
// insert empty deque - cannot insert null value
std
::
deque
<
std
::
string
>
t_QueryDeque
;
(
*
pc_queryParams
)[
pba_token
]
=
t_QueryDeque
;
}
pba_token
=
strtok
(
NULL
,
nsMicroservice_Constants
::
AND_SEPERATOR
);
}
}
eCrudMethod
cMicroservice_IRestServerMongooseImpl
::
GetCrudMethod
(
http_message
*
p_msg
)
{
auto
iter
=
_microservice_RestCrudMap
.
find
(
std
::
string
(
p_msg
->
method
.
p
,
p_msg
->
method
.
len
));
if
(
iter
!=
_microservice_RestCrudMap
.
end
())
return
iter
->
second
;
return
eCrudMethod
::
eMaxMethods
;
}
src/impl/servers/Microservice_IRestServerMongooseImpl.h
View file @
f95791c1
...
...
@@ -15,6 +15,7 @@
#define MICROSERVICE_IRESTSERVERMONGOOSEIMPL_H
#include "common/Microservice_Iface.h"
#include <thread>
#include <common/Microservice_RequestContext.h>
#include "../../params/Microservice_Params.h"
struct
mg_connection
;
...
...
@@ -22,8 +23,10 @@ struct mg_mgr;
struct
http_message
;
class
cMicroservice_RestHandler
;
class
cMicroservice_IResponseRestImpl
;
class
cMicroservice_IRequestRestImpl
;
class
cMicroservice_IRestServerMongooseImpl
:
public
nsMicroservice_Iface
::
IRestServer
{
class
cMicroservice_IRestServerMongooseImpl
:
public
nsMicroservice_Iface
::
IRestServer
,
public
nsMicroservice_Iface
::
IContainer
{
public
:
cMicroservice_IRestServerMongooseImpl
(
cMicroservice_RestServerParams
*
pc_Param
);
cMicroservice_IRestServerMongooseImpl
(
const
cMicroservice_IRestServerMongooseImpl
&
orig
);
...
...
@@ -45,19 +48,45 @@ public:
void
HandleRequest
(
mg_connection
*
conn
,
http_message
*
msg
);
void
SendNotImplemented
(
mg_connection
*
conn
);
virtual
void
SendErrorResp
(
nsMicroservice_Iface
::
IResponse
*
pti_Response
,
std
::
string
error
)
override
;
virtual
void
WriteObjectToResponse
(
nsMicroservice_Iface
::
IResponse
*
pti_Response
,
rapidjson
::
Document
&
t_ObjectDoc
)
override
;
virtual
void
WriteObjectToResponse
(
nsMicroservice_Iface
::
IResponse
*
pti_Response
,
cMicroservice_BaseRestResponse
&
t_BaseRestResponse
)
override
;
virtual
void
WriteStringToResponse
(
nsMicroservice_Iface
::
IResponse
*
pti_Response
,
const
char
*
pba_Doc
)
override
;
virtual
bool
ReadObjectFromRequest
(
nsMicroservice_Iface
::
IRequest
*
pti_Request
,
rapidjson
::
Document
&
t_ObjectDoc
)
override
;
private
:
cMicroservice_RestServerParams
*
mpc_Param
;
std
::
map
<
std
::
string
,
cMicroservice_RestHandler
*>
mc_HandlersMap
;
//
std::map<std::string,cMicroservice_RestHandler*> mc_HandlersMap;
std
::
string
mc_AppName
;
mg_mgr
*
mpt_ServerManager
;
char
mba_UriBuff
[
nsMicroservice_Constants
::
MAX_URI_LENGTH
];
std
::
thread
*
mpc_RunThread
;
nsMicroservice_Iface
::
ILogger
*
mpc_Logger
;
std
::
string
serverType_
;
char
mba_Buff
[
nsMicroservice_Constants
::
MAX_URI_LENGTH
];
// should be thread safe
cMicroservice_IResponseRestImpl
*
p_restResponseImpl_
;
cMicroservice_IRequestRestImpl
*
p_requestRestImpl_
;
void
HandleNewRequest
(
mg_connection
*
p_connection
,
http_message
*
p_message
);
void
HandleNewRequest
(
mg_connection
*
p_connection
,
std
::
string
key
,
http_message
*
p_message
,
std
::
string
&
apiContextPath
);
void
SendGeneralError
(
mg_connection
*
p_connection
,
int
respCode
,
const
char
*
error
);
MSRetStat
ParseRequest
(
mg_connection
*
p_conn
,
http_message
*
p_message
,
cMicroservice_RequestContext
&
ctx
,
std
::
string
&
apiContextPath
);
void
LogRequest
(
http_message
*
p_msg
);
void
GetQueryParams
(
cMicroservice_RequestContext
&
ctx
,
http_message
*
p_msg
);
eCrudMethod
GetCrudMethod
(
http_message
*
p_msg
);
};
#endif
/* MICROSERVICE_IRESTSERVERMONGOOSEIMPL_H */
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment