blob: ed893e6aedcffeb9893cb450aeec897d5e267a8b [file] [log] [blame]
Ed Tanous0fdddb12017-02-28 11:06:34 -08001#include "crow/query_string.h"
2#include "crow/http_parser_merged.h"
3#include "crow/ci_map.h"
4//#include "crow/TinySHA1.hpp"
5#include "crow/settings.h"
6#include "crow/socket_adaptors.h"
7#include "crow/json.h"
8#include "crow/mustache.h"
9#include "crow/logging.h"
10#include "crow/dumb_timer_queue.h"
11#include "crow/utility.h"
12#include "crow/common.h"
13#include "crow/http_request.h"
14#include "crow/websocket.h"
15#include "crow/parser.h"
16#include "crow/http_response.h"
17#include "crow/middleware.h"
18#include "crow/routing.h"
19#include "crow/middleware_context.h"
20#include "crow/http_connection.h"
21#include "crow/http_server.h"
22#include "crow/app.h"
23
24#include "ColorCoutG3Sink.hpp"
25
26#include "ssl_key_handler.hpp"
27#include <iostream>
28#include <string>
29
30
31
32struct ExampleMiddleware
33{
34 std::string message;
35
36 ExampleMiddleware()
37 {
38 message = "foo";
39 }
40
41 void setMessage(std::string newMsg)
42 {
43 message = newMsg;
44 }
45
46 struct context
47 {
48 };
49
50 void before_handle(crow::request& /*req*/, crow::response& /*res*/, context& /*ctx*/)
51 {
52 CROW_LOG_DEBUG << " - MESSAGE: " << message;
53 }
54
55 void after_handle(crow::request& /*req*/, crow::response& /*res*/, context& /*ctx*/)
56 {
57 // no-op
58 }
59};
60
61
62
63int main(int argc, char** argv)
64{
65 auto worker = g3::LogWorker::createLogWorker();
66 auto handle= worker->addDefaultLogger(argv[0], "/tmp/");
67 g3::initializeLogging(worker.get());
68 auto log_file_name = handle->call(&g3::FileSink::fileName);
69 auto stdout_handler = std::make_unique<crow::ColorCoutSink>();
70 auto sink_handle = worker->addSink(stdout_handler,
71 &crow::ColorCoutSink::ReceiveLogMessage);
72
73 LOG(DEBUG) << "Logging to " << log_file_name.get() << "\n";
74
75 std::string ssl_pem_file("server.pem");
76 ensuressl::ensure_openssl_key_present_and_valid(ssl_pem_file);
77 //auto handler2 = std::make_shared<ExampleLogHandler>();
78 //crow::logger::setHandler(handler2.get());
79 crow::App<ExampleMiddleware> app;
80
81 app.get_middleware<ExampleMiddleware>().setMessage("hello");
82
83 CROW_ROUTE(app, "/")
84 .name("hello")
85 ([]{
86 return "Hello World!";
87 });
88
89 CROW_ROUTE(app, "/about")
90 ([](){
91 return "About Crow example.";
92 });
93
94 // a request to /path should be forwarded to /path/
95 CROW_ROUTE(app, "/path/")
96 ([](){
97 return "Trailing slash test case..";
98 });
99
100
101 // simple json response
102 // To see it in action enter {ip}:18080/json
103 CROW_ROUTE(app, "/json")
104 ([]{
105 crow::json::wvalue x;
106 x["message"] = "Hello, World!";
107 return x;
108 });
109
110 // To see it in action enter {ip}:18080/hello/{integer_between -2^32 and 100} and you should receive
111 // {integer_between -2^31 and 100} bottles of beer!
112 CROW_ROUTE(app,"/hello/<int>")
113 ([](int count){
114 if (count > 100)
115 return crow::response(400);
116 std::ostringstream os;
117 os << count << " bottles of beer!";
118 return crow::response(os.str());
119 });
120
121 // To see it in action submit {ip}:18080/add/1/2 and you should receive 3 (exciting, isn't it)
122 CROW_ROUTE(app,"/add/<int>/<int>")
123 ([](const crow::request& /*req*/, crow::response& res, int a, int b){
124 std::ostringstream os;
125 os << a+b;
126 res.write(os.str());
127 res.end();
128 });
129
130 // Compile error with message "Handler type is mismatched with URL paramters"
131 //CROW_ROUTE(app,"/another/<int>")
132 //([](int a, int b){
133 //return crow::response(500);
134 //});
135
136 // more json example
137
138 // To see it in action, I recommend to use the Postman Chrome extension:
139 // * Set the address to {ip}:18080/add_json
140 // * Set the method to post
141 // * Select 'raw' and then JSON
142 // * Add {"a": 1, "b": 1}
143 // * Send and you should receive 2
144
145 // A simpler way for json example:
146 // * curl -d '{"a":1,"b":2}' {ip}:18080/add_json
147 CROW_ROUTE(app, "/add_json")
148 .methods("POST"_method)
149 ([](const crow::request& req){
150 auto x = crow::json::load(req.body);
151 if (!x)
152 return crow::response(400);
153 int sum = x["a"].i()+x["b"].i();
154 std::ostringstream os;
155 os << sum;
156 return crow::response{os.str()};
157 });
158
159 // Example of a request taking URL parameters
160 // If you want to activate all the functions just query
161 // {ip}:18080/params?foo='blabla'&pew=32&count[]=a&count[]=b
162 CROW_ROUTE(app, "/params")
163 ([](const crow::request& req){
164 std::ostringstream os;
165
166 // To get a simple string from the url params
167 // To see it in action /params?foo='blabla'
168 os << "Params: " << req.url_params << "\n\n";
169 os << "The key 'foo' was " << (req.url_params.get("foo") == nullptr ? "not " : "") << "found.\n";
170
171 // To get a double from the request
172 // To see in action submit something like '/params?pew=42'
173 if(req.url_params.get("pew") != nullptr) {
174 double countD = boost::lexical_cast<double>(req.url_params.get("pew"));
175 os << "The value of 'pew' is " << countD << '\n';
176 }
177
178 // To get a list from the request
179 // You have to submit something like '/params?count[]=a&count[]=b' to have a list with two values (a and b)
180 auto count = req.url_params.get_list("count");
181 os << "The key 'count' contains " << count.size() << " value(s).\n";
182 for(const auto& countVal : count) {
183 os << " - " << countVal << '\n';
184 }
185 return crow::response{os.str()};
186 });
187
188 CROW_ROUTE(app, "/large")
189 ([]{
190 return std::string(512*1024, ' ');
191 });
192
193 // ignore all log
194 crow::logger::setLogLevel(crow::LogLevel::DEBUG);
195
196 app.port(18080)
197 .multithreaded()
198 .run();
199}