blob: bbe599e8472f251c9da2b19f0bea67822081f426 [file] [log] [blame]
Ed Tanous0fdddb12017-02-28 11:06:34 -08001#include "crow/ci_map.h"
Ed Tanousf9273472017-02-28 16:05:13 -08002#include "crow/http_parser_merged.h"
3#include "crow/query_string.h"
Ed Tanous0fdddb12017-02-28 11:06:34 -08004//#include "crow/TinySHA1.hpp"
Ed Tanousf9273472017-02-28 16:05:13 -08005#include "crow/app.h"
6#include "crow/common.h"
7#include "crow/dumb_timer_queue.h"
8#include "crow/http_connection.h"
9#include "crow/http_request.h"
10#include "crow/http_response.h"
11#include "crow/http_server.h"
12#include "crow/json.h"
13#include "crow/logging.h"
14#include "crow/middleware.h"
15#include "crow/middleware_context.h"
16#include "crow/mustache.h"
17#include "crow/parser.h"
18#include "crow/routing.h"
Ed Tanous0fdddb12017-02-28 11:06:34 -080019#include "crow/settings.h"
20#include "crow/socket_adaptors.h"
Ed Tanous0fdddb12017-02-28 11:06:34 -080021#include "crow/utility.h"
Ed Tanous0fdddb12017-02-28 11:06:34 -080022#include "crow/websocket.h"
Ed Tanous0fdddb12017-02-28 11:06:34 -080023
Ed Tanous5f34a9c2017-02-28 12:35:13 -080024#include "color_cout_g3_sink.hpp"
Ed Tanous0fdddb12017-02-28 11:06:34 -080025
Ed Tanousf9273472017-02-28 16:05:13 -080026#include "token_authorization_middleware.hpp"
27
Ed Tanous0fdddb12017-02-28 11:06:34 -080028#include <iostream>
29#include <string>
Ed Tanousf9273472017-02-28 16:05:13 -080030#include "ssl_key_handler.hpp"
Ed Tanous0fdddb12017-02-28 11:06:34 -080031
32int main(int argc, char** argv)
33{
Ed Tanousf9273472017-02-28 16:05:13 -080034 auto worker = g3::LogWorker::createLogWorker();
35 auto handle = worker->addDefaultLogger(argv[0], "/tmp/");
36 g3::initializeLogging(worker.get());
37 auto log_file_name = handle->call(&g3::FileSink::fileName);
38 auto sink_handle = worker->addSink(std::make_unique<crow::ColorCoutSink>(),
39 &crow::ColorCoutSink::ReceiveLogMessage);
Ed Tanous0fdddb12017-02-28 11:06:34 -080040
Ed Tanousf9273472017-02-28 16:05:13 -080041 LOG(DEBUG) << "Logging to " << log_file_name.get() << "\n";
Ed Tanous0fdddb12017-02-28 11:06:34 -080042
43 std::string ssl_pem_file("server.pem");
44 ensuressl::ensure_openssl_key_present_and_valid(ssl_pem_file);
45 //auto handler2 = std::make_shared<ExampleLogHandler>();
46 //crow::logger::setHandler(handler2.get());
Ed Tanousf9273472017-02-28 16:05:13 -080047 crow::App<crow::TokenAuthorizationMiddleware> app;
Ed Tanous0fdddb12017-02-28 11:06:34 -080048
49 CROW_ROUTE(app, "/")
Ed Tanousf9273472017-02-28 16:05:13 -080050 .name("hello")([] {
51 return "Hello World!";
52 });
Ed Tanous0fdddb12017-02-28 11:06:34 -080053
54 CROW_ROUTE(app, "/about")
Ed Tanousf9273472017-02-28 16:05:13 -080055 ([]() {
Ed Tanous0fdddb12017-02-28 11:06:34 -080056 return "About Crow example.";
57 });
58
59 // a request to /path should be forwarded to /path/
60 CROW_ROUTE(app, "/path/")
Ed Tanousf9273472017-02-28 16:05:13 -080061 ([]() {
Ed Tanous0fdddb12017-02-28 11:06:34 -080062 return "Trailing slash test case..";
63 });
64
Ed Tanous0fdddb12017-02-28 11:06:34 -080065 // simple json response
66 // To see it in action enter {ip}:18080/json
67 CROW_ROUTE(app, "/json")
Ed Tanousf9273472017-02-28 16:05:13 -080068 ([] {
Ed Tanous0fdddb12017-02-28 11:06:34 -080069 crow::json::wvalue x;
70 x["message"] = "Hello, World!";
71 return x;
72 });
73
74 // To see it in action enter {ip}:18080/hello/{integer_between -2^32 and 100} and you should receive
75 // {integer_between -2^31 and 100} bottles of beer!
Ed Tanousf9273472017-02-28 16:05:13 -080076 CROW_ROUTE(app, "/hello/<int>")
77 ([](int count) {
Ed Tanous0fdddb12017-02-28 11:06:34 -080078 if (count > 100)
79 return crow::response(400);
80 std::ostringstream os;
81 os << count << " bottles of beer!";
82 return crow::response(os.str());
83 });
84
85 // To see it in action submit {ip}:18080/add/1/2 and you should receive 3 (exciting, isn't it)
Ed Tanousf9273472017-02-28 16:05:13 -080086 CROW_ROUTE(app, "/add/<int>/<int>")
87 ([](const crow::request& /*req*/, crow::response& res, int a, int b) {
Ed Tanous0fdddb12017-02-28 11:06:34 -080088 std::ostringstream os;
Ed Tanousf9273472017-02-28 16:05:13 -080089 os << a + b;
Ed Tanous0fdddb12017-02-28 11:06:34 -080090 res.write(os.str());
91 res.end();
92 });
93
94 // Compile error with message "Handler type is mismatched with URL paramters"
95 //CROW_ROUTE(app,"/another/<int>")
96 //([](int a, int b){
Ed Tanousf9273472017-02-28 16:05:13 -080097 //return crow::response(500);
Ed Tanous0fdddb12017-02-28 11:06:34 -080098 //});
99
100 // more json example
101
102 // To see it in action, I recommend to use the Postman Chrome extension:
103 // * Set the address to {ip}:18080/add_json
104 // * Set the method to post
105 // * Select 'raw' and then JSON
106 // * Add {"a": 1, "b": 1}
107 // * Send and you should receive 2
108
109 // A simpler way for json example:
110 // * curl -d '{"a":1,"b":2}' {ip}:18080/add_json
111 CROW_ROUTE(app, "/add_json")
Ed Tanousf9273472017-02-28 16:05:13 -0800112 .methods("POST"_method)([](const crow::request& req) {
113 auto x = crow::json::load(req.body);
114 if (!x)
115 return crow::response(400);
116 int sum = x["a"].i() + x["b"].i();
117 std::ostringstream os;
118 os << sum;
119 return crow::response{os.str()};
120 });
Ed Tanous0fdddb12017-02-28 11:06:34 -0800121
122 // Example of a request taking URL parameters
123 // If you want to activate all the functions just query
124 // {ip}:18080/params?foo='blabla'&pew=32&count[]=a&count[]=b
125 CROW_ROUTE(app, "/params")
Ed Tanousf9273472017-02-28 16:05:13 -0800126 ([](const crow::request& req) {
Ed Tanous0fdddb12017-02-28 11:06:34 -0800127 std::ostringstream os;
128
129 // To get a simple string from the url params
130 // To see it in action /params?foo='blabla'
Ed Tanousf9273472017-02-28 16:05:13 -0800131 os << "Params: " << req.url_params << "\n\n";
Ed Tanous0fdddb12017-02-28 11:06:34 -0800132 os << "The key 'foo' was " << (req.url_params.get("foo") == nullptr ? "not " : "") << "found.\n";
133
134 // To get a double from the request
135 // To see in action submit something like '/params?pew=42'
Ed Tanousf9273472017-02-28 16:05:13 -0800136 if (req.url_params.get("pew") != nullptr) {
Ed Tanous0fdddb12017-02-28 11:06:34 -0800137 double countD = boost::lexical_cast<double>(req.url_params.get("pew"));
Ed Tanousf9273472017-02-28 16:05:13 -0800138 os << "The value of 'pew' is " << countD << '\n';
Ed Tanous0fdddb12017-02-28 11:06:34 -0800139 }
140
141 // To get a list from the request
142 // You have to submit something like '/params?count[]=a&count[]=b' to have a list with two values (a and b)
143 auto count = req.url_params.get_list("count");
144 os << "The key 'count' contains " << count.size() << " value(s).\n";
Ed Tanousf9273472017-02-28 16:05:13 -0800145 for (const auto& countVal : count) {
Ed Tanous0fdddb12017-02-28 11:06:34 -0800146 os << " - " << countVal << '\n';
147 }
148 return crow::response{os.str()};
Ed Tanousf9273472017-02-28 16:05:13 -0800149 });
Ed Tanous0fdddb12017-02-28 11:06:34 -0800150
151 CROW_ROUTE(app, "/large")
Ed Tanousf9273472017-02-28 16:05:13 -0800152 ([] {
153 return std::string(512 * 1024, ' ');
Ed Tanous0fdddb12017-02-28 11:06:34 -0800154 });
155
156 // ignore all log
157 crow::logger::setLogLevel(crow::LogLevel::DEBUG);
158
159 app.port(18080)
160 .multithreaded()
161 .run();
162}