2 # ==================================================================================
3 # Copyright (c) 2020 HCL Technologies Limited.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 # ==================================================================================
18 #include"pistacheserver.h"
22 pistacheserver::pistacheserver(Pistache::Address addr,std::vector<std::string> method,std::vector<bool> static_routing,std::vector<bool> dynamic_routing)
23 : httpEndpoint(std::make_shared<Pistache::Http::Endpoint>(addr))
25 this->t=new std::thread*[1];
27 this->static_routing=static_routing;
28 this->dynamic_routing=dynamic_routing;
29 assert(static_routing.size()==method.size() && dynamic_routing.size()==method.size());
30 stat_sz=cal_stat_size();
31 dyn_sz=cal_dyn_size();
33 int pistacheserver::cal_stat_size()//caclualting no of true in static_routing
36 for(int i=0;i<static_routing.size();i++)
38 if (static_routing[i])
44 int pistacheserver::cal_dyn_size() //calaculating no true in dynamic_routing
47 for(int i=0;i<dynamic_routing.size();i++)
49 if (dynamic_routing[i])
55 void pistacheserver::setup_base_url(std::string base)
59 void pistacheserver::setup_static_route(std::unordered_map<int,std::string> route)//routing path
61 this->route_static=route;
63 void pistacheserver::setup_dynamic_route(std::unordered_map<int,std::string> route)//routing path
65 this->route_dynamic=route;
67 void pistacheserver::add_static_cb(std::unordered_map<int,usr_callback> cb)
71 void pistacheserver::add_dynamic_cb(std::unordered_map<int,usr_callback> cb)
76 void pistacheserver::init(size_t thr = 2)
78 auto opts = Pistache::Http::Endpoint::options().threads(thr).flags(Pistache::Tcp::Options::ReuseAddr)/*.flags(Pistache::Tcp::Options::ReusePort)*/; // how many threads for the server
79 httpEndpoint->init(opts);
80 assert(route_static.size()==stat_sz && route_dynamic.size()==dyn_sz); //no of true in satatic_routing and dynamic_routig should match with size of route_static and route_dynamic respectively.
82 assert(cb_static.size()==stat_sz && cb_dynamic.size()==dyn_sz); //no of true in satatic_routing and dynamic_routig should match with size of cb_static and cb_dynamic respectively.
83 for (int i=0;i<method.size();i++)
89 else if (method[i]=="post" )
91 setupRoutes_post(i+1);
94 else if (method[i]=="del")
101 std::cout<<"Wrong Method called \n";
109 void pistacheserver::start()
111 httpEndpoint->setHandler(router.handler());
112 t[0]=new std::thread(&pistacheserver::thread_start,this);
115 void pistacheserver::thread_start()
117 this->httpEndpoint->serve();
121 void pistacheserver::shutdown()
123 httpEndpoint->shutdown();
126 void pistacheserver::setupRoutes_get(int index)
128 using namespace Pistache::Rest;
129 auto search_s=route_static.find(index);
130 auto search_d=route_dynamic.find(index);
131 auto cbs_search=cb_static.find(index);
132 auto cbd_search=cb_dynamic.find(index);
133 assert (search_s!=route_static.end() || search_d!=route_dynamic.end());
134 if (static_routing[index-1])
136 std::cout<<"static routing get"<<std::endl;
137 assert (search_s!=route_static.end() && cbs_search!=cb_static.end());
138 Routes::Get(router, base + search_s->second, Routes::bind(cbs_search->second));
140 if (dynamic_routing[index-1])
142 std::cout<<"dynamic routing get"<<std::endl;
143 assert (search_d!=route_dynamic.end() && cbd_search!=cb_dynamic.end());
144 Routes::Get(router, base + search_d->second+ dynamic_id, Routes::bind(cbd_search->second));
147 // Default handler, called when a route is not found
148 router.addCustomHandler(Routes::bind(&pistacheserver::default_handler, this));
150 void pistacheserver::setupRoutes_post(int index)
152 using namespace Pistache::Rest;
153 auto search_s=route_static.find(index);
154 auto search_d=route_dynamic.find(index);
155 auto cbs_search=cb_static.find(index);
156 auto cbd_search=cb_dynamic.find(index);
157 assert (search_s!=route_static.end() || search_d!=route_dynamic.end());
158 if (static_routing[index-1])
160 std::cout<<"static routing post"<<std::endl;
161 assert (search_s!=route_static.end() && cbs_search!=cb_static.end());
162 Routes::Post(router, base + search_s->second, Routes::bind(cbs_search->second));
164 if (dynamic_routing[index-1])
166 std::cout<<"dynamic routing post"<<std::endl;
167 assert (search_d!=route_dynamic.end() && cbd_search!=cb_dynamic.end());
168 Routes::Post(router, base + search_d->second+ dynamic_id, Routes::bind(cbd_search->second));
170 // Default handler, called when a route is not found
171 router.addCustomHandler(Routes::bind(&pistacheserver::default_handler, this));
173 void pistacheserver::setupRoutes_del(int index)
175 using namespace Pistache::Rest;
176 auto search_s=route_static.find(index);
177 auto search_d=route_dynamic.find(index);
178 auto cbs_search=cb_static.find(index);
179 auto cbd_search=cb_dynamic.find(index);
180 assert (search_s!=route_static.end() || search_d!=route_dynamic.end());
181 if (static_routing[index-1])
183 std::cout<<"static routing delete"<<std::endl;
184 assert (search_s!=route_static.end() && cbs_search!=cb_static.end());
185 Routes::Delete(router, base + search_s->second, Routes::bind(cbs_search->second));
187 if (dynamic_routing[index-1])
189 std::cout<<"dynamic routing delete"<<std::endl;
190 assert (search_d!=route_dynamic.end() && cbd_search!=cb_dynamic.end());
191 Routes::Delete(router, base + search_d->second+ dynamic_id, Routes::bind(cbd_search->second));
193 // Default handler, called when a route is not found
194 router.addCustomHandler(Routes::bind(&pistacheserver::default_handler, this));