Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 1 | #include "crow/ci_map.h" |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame^] | 2 | #include "crow/http_parser_merged.h" |
| 3 | #include "crow/query_string.h" |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 4 | //#include "crow/TinySHA1.hpp" |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame^] | 5 | #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 Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 19 | #include "crow/settings.h" |
| 20 | #include "crow/socket_adaptors.h" |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 21 | #include "crow/utility.h" |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 22 | #include "crow/websocket.h" |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 23 | |
Ed Tanous | 5f34a9c | 2017-02-28 12:35:13 -0800 | [diff] [blame] | 24 | #include "color_cout_g3_sink.hpp" |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 25 | |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame^] | 26 | #include "token_authorization_middleware.hpp" |
| 27 | |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 28 | #include <iostream> |
| 29 | #include <string> |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame^] | 30 | #include "ssl_key_handler.hpp" |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 31 | |
| 32 | int main(int argc, char** argv) |
| 33 | { |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame^] | 34 | 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 Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 40 | |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame^] | 41 | LOG(DEBUG) << "Logging to " << log_file_name.get() << "\n"; |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 42 | |
| 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 Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame^] | 47 | crow::App<crow::TokenAuthorizationMiddleware> app; |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 48 | |
| 49 | CROW_ROUTE(app, "/") |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame^] | 50 | .name("hello")([] { |
| 51 | return "Hello World!"; |
| 52 | }); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 53 | |
| 54 | CROW_ROUTE(app, "/about") |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame^] | 55 | ([]() { |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 56 | return "About Crow example."; |
| 57 | }); |
| 58 | |
| 59 | // a request to /path should be forwarded to /path/ |
| 60 | CROW_ROUTE(app, "/path/") |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame^] | 61 | ([]() { |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 62 | return "Trailing slash test case.."; |
| 63 | }); |
| 64 | |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 65 | // simple json response |
| 66 | // To see it in action enter {ip}:18080/json |
| 67 | CROW_ROUTE(app, "/json") |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame^] | 68 | ([] { |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 69 | 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 Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame^] | 76 | CROW_ROUTE(app, "/hello/<int>") |
| 77 | ([](int count) { |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 78 | 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 Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame^] | 86 | CROW_ROUTE(app, "/add/<int>/<int>") |
| 87 | ([](const crow::request& /*req*/, crow::response& res, int a, int b) { |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 88 | std::ostringstream os; |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame^] | 89 | os << a + b; |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 90 | 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 Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame^] | 97 | //return crow::response(500); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 98 | //}); |
| 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 Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame^] | 112 | .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 Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 121 | |
| 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 Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame^] | 126 | ([](const crow::request& req) { |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 127 | std::ostringstream os; |
| 128 | |
| 129 | // To get a simple string from the url params |
| 130 | // To see it in action /params?foo='blabla' |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame^] | 131 | os << "Params: " << req.url_params << "\n\n"; |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 132 | 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 Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame^] | 136 | if (req.url_params.get("pew") != nullptr) { |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 137 | double countD = boost::lexical_cast<double>(req.url_params.get("pew")); |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame^] | 138 | os << "The value of 'pew' is " << countD << '\n'; |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 139 | } |
| 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 Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame^] | 145 | for (const auto& countVal : count) { |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 146 | os << " - " << countVal << '\n'; |
| 147 | } |
| 148 | return crow::response{os.str()}; |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame^] | 149 | }); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 150 | |
| 151 | CROW_ROUTE(app, "/large") |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame^] | 152 | ([] { |
| 153 | return std::string(512 * 1024, ' '); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 154 | }); |
| 155 | |
| 156 | // ignore all log |
| 157 | crow::logger::setLogLevel(crow::LogLevel::DEBUG); |
| 158 | |
| 159 | app.port(18080) |
| 160 | .multithreaded() |
| 161 | .run(); |
| 162 | } |