blob: c75ad6126507593a7af056e0fae75b657d97e696 [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);
Ed Tanousd44b4442017-02-28 11:12:44 -080069 auto sink_handle = worker->addSink(std::make_unique<crow::ColorCoutSink>(),
Ed Tanous0fdddb12017-02-28 11:06:34 -080070 &crow::ColorCoutSink::ReceiveLogMessage);
71
72 LOG(DEBUG) << "Logging to " << log_file_name.get() << "\n";
73
74 std::string ssl_pem_file("server.pem");
75 ensuressl::ensure_openssl_key_present_and_valid(ssl_pem_file);
76 //auto handler2 = std::make_shared<ExampleLogHandler>();
77 //crow::logger::setHandler(handler2.get());
78 crow::App<ExampleMiddleware> app;
79
80 app.get_middleware<ExampleMiddleware>().setMessage("hello");
81
82 CROW_ROUTE(app, "/")
83 .name("hello")
84 ([]{
85 return "Hello World!";
86 });
87
88 CROW_ROUTE(app, "/about")
89 ([](){
90 return "About Crow example.";
91 });
92
93 // a request to /path should be forwarded to /path/
94 CROW_ROUTE(app, "/path/")
95 ([](){
96 return "Trailing slash test case..";
97 });
98
99
100 // simple json response
101 // To see it in action enter {ip}:18080/json
102 CROW_ROUTE(app, "/json")
103 ([]{
104 crow::json::wvalue x;
105 x["message"] = "Hello, World!";
106 return x;
107 });
108
109 // To see it in action enter {ip}:18080/hello/{integer_between -2^32 and 100} and you should receive
110 // {integer_between -2^31 and 100} bottles of beer!
111 CROW_ROUTE(app,"/hello/<int>")
112 ([](int count){
113 if (count > 100)
114 return crow::response(400);
115 std::ostringstream os;
116 os << count << " bottles of beer!";
117 return crow::response(os.str());
118 });
119
120 // To see it in action submit {ip}:18080/add/1/2 and you should receive 3 (exciting, isn't it)
121 CROW_ROUTE(app,"/add/<int>/<int>")
122 ([](const crow::request& /*req*/, crow::response& res, int a, int b){
123 std::ostringstream os;
124 os << a+b;
125 res.write(os.str());
126 res.end();
127 });
128
129 // Compile error with message "Handler type is mismatched with URL paramters"
130 //CROW_ROUTE(app,"/another/<int>")
131 //([](int a, int b){
132 //return crow::response(500);
133 //});
134
135 // more json example
136
137 // To see it in action, I recommend to use the Postman Chrome extension:
138 // * Set the address to {ip}:18080/add_json
139 // * Set the method to post
140 // * Select 'raw' and then JSON
141 // * Add {"a": 1, "b": 1}
142 // * Send and you should receive 2
143
144 // A simpler way for json example:
145 // * curl -d '{"a":1,"b":2}' {ip}:18080/add_json
146 CROW_ROUTE(app, "/add_json")
147 .methods("POST"_method)
148 ([](const crow::request& req){
149 auto x = crow::json::load(req.body);
150 if (!x)
151 return crow::response(400);
152 int sum = x["a"].i()+x["b"].i();
153 std::ostringstream os;
154 os << sum;
155 return crow::response{os.str()};
156 });
157
158 // Example of a request taking URL parameters
159 // If you want to activate all the functions just query
160 // {ip}:18080/params?foo='blabla'&pew=32&count[]=a&count[]=b
161 CROW_ROUTE(app, "/params")
162 ([](const crow::request& req){
163 std::ostringstream os;
164
165 // To get a simple string from the url params
166 // To see it in action /params?foo='blabla'
167 os << "Params: " << req.url_params << "\n\n";
168 os << "The key 'foo' was " << (req.url_params.get("foo") == nullptr ? "not " : "") << "found.\n";
169
170 // To get a double from the request
171 // To see in action submit something like '/params?pew=42'
172 if(req.url_params.get("pew") != nullptr) {
173 double countD = boost::lexical_cast<double>(req.url_params.get("pew"));
174 os << "The value of 'pew' is " << countD << '\n';
175 }
176
177 // To get a list from the request
178 // You have to submit something like '/params?count[]=a&count[]=b' to have a list with two values (a and b)
179 auto count = req.url_params.get_list("count");
180 os << "The key 'count' contains " << count.size() << " value(s).\n";
181 for(const auto& countVal : count) {
182 os << " - " << countVal << '\n';
183 }
184 return crow::response{os.str()};
185 });
186
187 CROW_ROUTE(app, "/large")
188 ([]{
189 return std::string(512*1024, ' ');
190 });
191
192 // ignore all log
193 crow::logger::setLogLevel(crow::LogLevel::DEBUG);
194
195 app.port(18080)
196 .multithreaded()
197 .run();
198}