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