Open Lighting Architecture  Latest Git
OlaHTTPServer.h
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * OlaHTTPServer.h
17  * A HTTP Server with export map integration.
18  * Copyright (C) 2012 Simon Newton
19  */
20 
21 
22 #ifndef INCLUDE_OLA_HTTP_OLAHTTPSERVER_H_
23 #define INCLUDE_OLA_HTTP_OLAHTTPSERVER_H_
24 
25 #include <ola/Clock.h>
26 #include <ola/ExportMap.h>
27 #include <ola/base/Macro.h>
28 #include <ola/http/HTTPServer.h>
29 #include <string>
30 
31 namespace ola {
32 namespace http {
33 
34 /*
35  * A HTTP Server with ExportMap support. You can inherit from this class to
36  * implement specific handlers.
37  */
39  public:
41  ola::ExportMap *export_map);
42  virtual ~OlaHTTPServer() {}
43 
44  virtual bool Init();
45  bool Start() { return m_server.Start(); }
46  void Stop() { return m_server.Stop(); }
47 
48  protected:
49  Clock m_clock;
50  ola::ExportMap *m_export_map;
51  HTTPServer m_server;
52  TimeStamp m_start_time;
53 
57  void RegisterFile(const std::string &file,
58  const std::string &content_type) {
59  m_server.RegisterFile("/" + file, file, content_type);
60  }
61 
62  private:
63  static const char K_DATA_DIR_VAR[];
64  static const char K_UPTIME_VAR[];
65 
66  inline void RegisterHandler(
67  const std::string &path,
68  int (OlaHTTPServer::*method)(const HTTPRequest*, HTTPResponse*)) {
69  m_server.RegisterHandler(
70  path,
72  int,
73  const HTTPRequest*,
74  HTTPResponse*>(
75  this,
76  method));
77  }
78 
79  int DisplayDebug(const HTTPRequest *request, HTTPResponse *response);
80  int DisplayHandlers(const HTTPRequest *request, HTTPResponse *response);
81 
82  DISALLOW_COPY_AND_ASSIGN(OlaHTTPServer);
83 };
84 } // namespace http
85 } // namespace ola
86 #endif // INCLUDE_OLA_HTTP_OLAHTTPSERVER_H_
The base HTTP Server.
Definition: HTTPServer.h:156
Definition: OlaHTTPServer.h:38
A container for the exported variables.
Definition: ExportMap.h:324
Definition: HTTPServer.h:65
void RegisterFile(const std::string &file, const std::string &content_type)
Definition: OlaHTTPServer.h:57
Definition: HTTPServer.h:161
Export variables on the http server.
Definition: HTTPServer.h:109
virtual bool Start()
Start the thread and wait for the thread to be running.
Definition: Thread.cpp:90
Used to get the current time.
Definition: Clock.h:242
void Stop()
Stop the HTTP server.
Definition: HTTPServer.cpp:553
bool RegisterHandler(const std::string &path, BaseHTTPCallback *handler)
Register a handler.
Definition: HTTPServer.cpp:677
bool RegisterFile(const std::string &path, const std::string &content_type)
Register a static file. The root of the URL corresponds to the data dir.
Definition: HTTPServer.cpp:694
Helper macros.
virtual bool Init()
Definition: OlaHTTPServer.cpp:64
Callback0< ReturnType > * NewCallback(ReturnType(*callback)())
A helper function to create a new Callback with 0 create-time arguments and 0 execution time argument...
Definition: Callback.h:211
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
Represents a point in time with microsecond accuracy.
Definition: Clock.h:191
OlaHTTPServer(const HTTPServer::HTTPServerOptions &options, ola::ExportMap *export_map)
Definition: OlaHTTPServer.cpp:46