Open nicraMarcin opened 5 years ago
I import these header files:
#include <pistache/http.h>
#include <pistache/http_header.h>
#include <pistache/router.h>
#include <pistache/endpoint.h>
and use a shared pointer for my endpoint
Pistache::Rest::Router router;
boost::shared_ptr<Pistache::Http::Endpoint> httpEndpoint;
My initialization then looks like:
Pistache::Address addr(Pistache::Ipv4::any(), Pistache::Port(8080));
httpEndpoint.reset(new Pistache::Http::Endpoint (addr));
auto opts = Pistache::Http::Endpoint::options()
.threads(1)
.flags(Pistache::Tcp::Options::ReuseAddr);
httpEndpoint->init(opts);
setup_routes();
httpEndpoint->setHandler(router.handler());
httpEndpoint->serve(); // blocking function
Elsewhere I setup my routes in my own pistache class:
Pistache::Rest::Routes::Get(router, "/check_status/*", Pistache::Rest::Routes::bind(&pistache::check_status, this));
void pistache::check_status (const Pistache::Rest::Request& request, Pistache::Http::ResponseWriter response)
{
response.headers().add<Pistache::Http::Header::AccessControlAllowOrigin>("*");
// query MySQL and create json_str with boost::property_tree::ptree pt;
response.send(Pistache::Http::Code::Ok, json_str);
return;
}
Thanks for hint. About my error I've made static method and now works.
Rest::Routes::Get(router, "/", Rest::Routes::bind(RootEndpoint::doMain));
@xinthose Could You give me more detailed example in wich files You declare router and how you access to this pointer in other project files? btw Rest::Router has method addRoute() so isn't better to create new routes with this api instead of using Rest::Routes::XXX?
Hello, I'm new in C++ so I'm asking for your understanding.
1. I try to create routerService to store and manage routes and run threaded server. routerService.h
routerService.cpp
mainEndpoint.h
mainEndpont.cpp
and in main file **sandbox.cpp***
And I cannot compile this:
Where I'm making mistake?
"^/admin" => &adminEndpoint
(and in adminEnpoint store rest of this path "/admin/show" etc.)"^user" => &userEndpont
(and in userEnpoint rest of this path)Thanks for advance for this hints.