NetFlex  0.0.0
C++11 HTTP Server Library.
server.hpp
1 // The MIT License (MIT)
2 //
3 // Copyright (c) 2015-2017 Simon Ninon <simon.ninon@gmail.com>
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #pragma once
24 
25 #include <list>
26 #include <string>
27 #include <vector>
28 
29 #include <tacopie/tacopie>
30 
31 #include <netflex/http/client.hpp>
32 #include <netflex/routing/middleware_chain.hpp>
33 #include <netflex/routing/route.hpp>
34 
35 namespace netflex {
36 
37 namespace http {
38 
43 class server {
44 public:
46  server(void);
48  ~server(void) = default;
49 
51  server(const server&) = delete;
53  server& operator=(const server&) = delete;
54 
55 public:
62  server& add_route(const routing::route& route);
63 
70  server& add_routes(const std::vector<routing::route>& routes);
71 
78  server& set_route(const std::vector<routing::route>& routes);
79 
80 public:
89  server& add_middleware(const routing::middleware_t& middleware);
90 
100  server& add_middlewares(const std::list<routing::middleware_t>& middlewares);
101 
109  server& set_middlewares(const std::list<routing::middleware_t>& middlewares);
110 
111 public:
118  void start(const std::string& host = "0.0.0.0", unsigned int port = 3000);
119 
123  void stop(void);
124 
128  bool is_running(void) const;
129 
130 private:
137  bool on_connection_received(const std::shared_ptr<tacopie::tcp_client>& client);
138 
142  typedef std::list<client>::iterator client_iterator_t;
143 
152  void on_http_request_received(bool success, request& request, client_iterator_t client);
153 
160  void on_client_disconnected(client_iterator_t client);
161 
169  void dispatch(routing::middleware_chain& chain, http::request& request, http::response& response);
170 
171 private:
175  tacopie::tcp_server m_tcp_server;
176 
180  std::vector<routing::route> m_routes;
181 
185  std::list<routing::middleware_t> m_middlewares;
186 
190  std::list<client> m_clients;
191 };
192 
193 } // namespace http
194 
195 } // namespace netflex
~server(void)=default
default dtor
Definition: server.hpp:43
Definition: route.hpp:42
server & add_routes(const std::vector< routing::route > &routes)
bool is_running(void) const
Definition: client.hpp:41
Definition: middleware_chain.hpp:48
server(void)
default ctor
server & add_middlewares(const std::list< routing::middleware_t > &middlewares)
Definition: client.hpp:33
server & add_middleware(const routing::middleware_t &middleware)
server & operator=(const server &)=delete
assignment operator
void start(const std::string &host="0.0.0.0", unsigned int port=3000)
Definition: response.hpp:37
server & add_route(const routing::route &route)
server & set_route(const std::vector< routing::route > &routes)
Definition: request.hpp:39
server & set_middlewares(const std::list< routing::middleware_t > &middlewares)