aboutsummaryrefslogtreecommitdiff
path: root/api/umosapi.cpp
blob: 989dbe2ef97e140922015d24758cf91107a4ed3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>

#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>

#include <mongocxx/client.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>

#include <json-c/json.h>

#include <nlohmann/json.hpp>

#include "umosapi.h"
#include "../shared.h"
#include "../db/mongo_access.h"
#include "../db/uobject.h"

#include <regex>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <streambuf>

UmosapiService::mongo_access mongo;

using json = nlohmann::json;

UmosapiService::Api::Api() {}

void UmosapiService::Api::init() {

    auto uri = mongocxx::uri{config["mongoURI"]};
    mongo.configure(std::move(uri));
    UmosapiService::Api::createResource();
    std::ofstream swagger_json;
    swagger_json.open(config["swaggerui"] + "/swagger.json");
    swagger_json << UmosapiService::Api::_swagger.dump();
    swagger_json.close();
}

void UmosapiService::Api::start(int port, int thr) {
    auto settings = std::make_shared< restbed::Settings >();
    settings->set_port( port );
    settings->set_worker_limit( thr );
    settings->set_default_header("Connection", "close");
    UmosapiService::Api::_service.start(settings);
}

void service_error_handler( const int, const std::exception& e, const std::shared_ptr< restbed::Session > session )
{
    std::string message = "Backend Service is dead: ";
    message += e.what();
    if ( session ) {
        if ( session->is_open( ) ) {
            session->close( 500, message, { { "Content-Length", std::to_string(message.length()) } } );
        }
    }
    fprintf( stderr, "ERROR: %s.\n", message.c_str() );
}

void resource_error_handler( const int, const std::exception& e, const std::shared_ptr< restbed::Session > session )
{
    std::string message = "Backend Resource is dead: ";
    message += e.what();
    if ( session->is_open( ) )
        session->close( 500, message, { { "Content-Length", std::to_string(message.length()) } } );
    fprintf( stderr, "ERROR: %s.\n", message.c_str() );
}

void faulty_method_handler( const std::shared_ptr< restbed::Session > )
{
    throw restbed::SERVICE_UNAVAILABLE;
}

void is_ready(const std::shared_ptr< restbed::Session > session)
{
    session->close( restbed::OK, "1", { { "Content-Length", "1"}});
}

/*
 * Add new route in service
 * service: the service pointer
 * route: path of the route
 * http_word: word http who defin REST request
 * callback: function to call when the route is requested
 * error_callback: error to show if everything is broken
 * tags: array of tags
 */
void UmosapiService::Api::desc(std::string route, std::string http_word, const std::function< void ( const std::shared_ptr< restbed::Session > ) >& callback, const std::function< void(int, const std::exception&, std::shared_ptr< restbed::Session >) >& error_callback, tag tags[]) {
    auto resource = std::make_shared< restbed::Resource > ();
    resource->set_path(route);
    resource->set_method_handler(http_word, callback);
    resource->set_error_handler( error_callback );

    UmosapiService::Api::_service.publish(resource);
}

void UmosapiService::Api::description(std::string description) {
    UmosapiService::Api::_swagger["info"]["description"] = description;
}

void UmosapiService::Api::title(std::string title) {
    UmosapiService::Api::_swagger["info"]["title"] = title;
}

void UmosapiService::Api::version(std::string version) {
    UmosapiService::Api::_swagger["info"]["version"] = version;
}

void UmosapiService::Api::basePath(std::string basePath) {
    UmosapiService::Api::_swagger["swagger"] = "2.0";
    UmosapiService::Api::_swagger["basePath"] = basePath;
}

void UmosapiService::Api::host(std::string host) {
    UmosapiService::Api::_swagger["host"] = host;
}

void UmosapiService::Api::atag(std::string name, std::string description) {
    struct tag the_tag;
    the_tag.name = name;
    the_tag.description = description;

    UmosapiService::Api::_tags.push_back(the_tag);
    UmosapiService::Api::_swagger["tags"].push_back({ {"name",the_tag.name},{"description", the_tag.description} });

}

void UmosapiService::Api::scheme(std::string scheme) {
    UmosapiService::Api::_swagger["schemes"].push_back(scheme);
}

void UmosapiService::Api::set_path(std::string route) {
    UmosapiService::Api::_resource = std::make_shared< restbed::Resource > ();
    UmosapiService::Api::_resource->set_path(route);
    std::regex parameter(":.*?}");
    std::regex base_path("^/v2");
    auto tmp_route = std::regex_replace (route,parameter,"}");
    auto final_route = std::regex_replace (tmp_route,base_path,"");
    UmosapiService::Api::_path = Path{final_route};
    UmosapiService::Api::_swagger["paths"][final_route] = {};
}

void UmosapiService::Api::set_method_handler(std::string http_word, const std::function< void ( const std::shared_ptr< restbed::Session > ) >& callback) {
    UmosapiService::Api::_resource->set_method_handler(http_word, callback);
    std::locale loc;
    for (auto& c : http_word) { c = tolower(c); }
    UmosapiService::Api::_path.words.push_back(HttpWord{http_word});
    UmosapiService::Api::_swagger["paths"][UmosapiService::Api::_path.name][http_word]["description"] = "";
    UmosapiService::Api::_swagger["paths"][UmosapiService::Api::_path.name][http_word]["operationId"] = "";
    UmosapiService::Api::_swagger["paths"][UmosapiService::Api::_path.name][http_word]["summary"] = "";
}

void UmosapiService::Api::set_error_handler(const std::function< void(int, const std::exception&, std::shared_ptr< restbed::Session >) >& error_callback) {
    UmosapiService::Api::_resource->set_error_handler( error_callback );
}

void UmosapiService::Api::publish() {
    for (const auto& http_word: UmosapiService::Api::_path.words) {
        auto responses = UmosapiService::Api::_swagger["paths"][UmosapiService::Api::_path.name][http_word.name]["responses"];
        if (responses.find("200") == responses.end()) {
            UmosapiService::Api::_swagger["paths"][UmosapiService::Api::_path.name][http_word.name]["responses"]["200"]["description"] = "All is fine.";
        }
    }
    UmosapiService::Api::_service.publish(_resource);
}

void UmosapiService::Api::definition(std::string name, std::string type) {
    UmosapiService::Api::_definition = Definition{name, type};
    UmosapiService::Api::_definitions.defs.push_back(_definition);
    UmosapiService::Api::_swagger["definitions"][name]["type"] = type;
}

void UmosapiService::Api::propertie(std::string name, std::string format, std::string type, std::string required) {
    UmosapiService::Api::_definition.props.push_back(Propertie{name, format, type, required});
    if (type == "ref") {
        UmosapiService::Api::_swagger["definitions"][UmosapiService::Api::_definition.name]["type"] = "array";
        std::string schema_path = "#/definitions/";
        UmosapiService::Api::_swagger["definitions"][UmosapiService::Api::_definition.name]["items"]["$ref"] = schema_path.append(name);
    } else {
        UmosapiService::Api::_swagger["definitions"][UmosapiService::Api::_definition.name]["properties"][name]["format"] = format;
        UmosapiService::Api::_swagger["definitions"][UmosapiService::Api::_definition.name]["properties"][name]["type"] = type;
    }
    if (required == "true") {
        UmosapiService::Api::_swagger["definitions"][UmosapiService::Api::_definition.name]["required"].push_back(name);
    }
}

void UmosapiService::Api::consume(std::string consume) {
    UmosapiService::Api::_swagger["paths"][UmosapiService::Api::_path.name][UmosapiService::Api::_path.words.back().name]["consumes"].push_back(consume);
}

void UmosapiService::Api::produce(std::string produce) {
    UmosapiService::Api::_swagger["paths"][UmosapiService::Api::_path.name][UmosapiService::Api::_path.words.back().name]["produces"].push_back(produce);
}

void UmosapiService::Api::parameter(std::string name, std::string description, std::string schema = "") {
    json parameter;
    parameter["name"] = name;
    parameter["description"] = description;
    parameter["required"] = true;
    if (schema == "") {
        parameter["type"] = "string";
        parameter["in"] = "path";
    } else {
        parameter["in"] = "body";
        for (const auto& def: UmosapiService::Api::_definitions.defs) {
            if (def.name == schema ) {
                std::string schema_path = "#/definitions/";
                parameter["schema"]["$ref"] = schema_path.append(schema);
            }
        }
    }
    UmosapiService::Api::_swagger["paths"][UmosapiService::Api::_path.name][UmosapiService::Api::_path.words.back().name]["parameters"].push_back(parameter);
}

void UmosapiService::Api::response(std::string http_code, std::string description, std::string definition, std::string type = "object") {
    std::string schema = "#/definitions/";
    if (type == "object") {
        UmosapiService::Api::_swagger["paths"][UmosapiService::Api::_path.name][UmosapiService::Api::_path.words.back().name]["responses"][http_code]["description"] = description;
        UmosapiService::Api::_swagger["paths"][UmosapiService::Api::_path.name][UmosapiService::Api::_path.words.back().name]["responses"][http_code]["schema"]["$ref"] = schema.append(definition);
    } else {
        UmosapiService::Api::_swagger["paths"][UmosapiService::Api::_path.name][UmosapiService::Api::_path.words.back().name]["responses"][http_code]["description"] = description;
        UmosapiService::Api::_swagger["paths"][UmosapiService::Api::_path.name][UmosapiService::Api::_path.words.back().name]["responses"][http_code]["schema"]["items"]["$ref"] = schema.append(definition);
        UmosapiService::Api::_swagger["paths"][UmosapiService::Api::_path.name][UmosapiService::Api::_path.words.back().name]["responses"][http_code]["schema"]["type"] = type;
    }
}

void UmosapiService::Api::swagger(std::string ui_path, std::string swagger_dir, std::string api_path) {
    UmosapiService::Api::_resource = std::make_shared< restbed::Resource > ();
    UmosapiService::Api::_resource->set_path(ui_path);
    UmosapiService::Api::_resource->set_method_handler("GET", swaggerEndpoint);
    UmosapiService::Api::_service.publish(_resource);

    UmosapiService::Api::_resource = std::make_shared< restbed::Resource > ();
    UmosapiService::Api::_resource->set_path(ui_path + "/{filename: .*}");
    UmosapiService::Api::_resource->set_method_handler("GET", swaggerEndpointResources);
    UmosapiService::Api::_service.publish(_resource);

    UmosapiService::Api::_resource = std::make_shared< restbed::Resource > ();
    UmosapiService::Api::_resource->set_path(api_path);
    UmosapiService::Api::_resource->set_method_handler("GET", swaggerEndpointApi);
    UmosapiService::Api::_service.publish(_resource);
}


void UmosapiService::Api::createResource() {
    UmosapiService::Api::basePath("/v2");
    UmosapiService::Api::description("Umosapi rest api");
    UmosapiService::Api::version("0.1");
    UmosapiService::Api::title("UMOSAPI");
    UmosapiService::Api::host("127.0.0.1:"+config["port"]);

    UmosapiService::Api::swagger("/doc", config["swaggerui"], "/api");

    UmosapiService::Api::atag("uobject", "Everything about your UObjec");

    UmosapiService::Api::scheme("http");
    UmosapiService::Api::scheme("https");

    UmosapiService::Api::definition("UObject", "object");
    UmosapiService::Api::propertie("id", "int64", "integer", "true");
    UmosapiService::Api::propertie("value", "string", "object", "true");

    UmosapiService::Api::definition("UObjectSended", "object");

    UmosapiService::Api::set_path("/v2/ready");
    UmosapiService::Api::set_method_handler("GET", is_ready);
    UmosapiService::Api::produce("application/json");
    UmosapiService::Api::publish();

    UmosapiService::Api::set_path("/v2/{mcollection: .*}");
    UmosapiService::Api::set_method_handler("GET", retrieveAll);
    UmosapiService::Api::produce("application/json");
    UmosapiService::Api::parameter("mcollection", "Name of the collection where the uobject are located");
    UmosapiService::Api::response("200", "All is fine", "UObject", "array");
    UmosapiService::Api::set_error_handler(&resource_error_handler);
    UmosapiService::Api::set_method_handler("POST", addUObject);
    UmosapiService::Api::consume("application/json");
    UmosapiService::Api::parameter("mcollection", "Name of the collection where the uobject are located");
    UmosapiService::Api::parameter("body", "UObject to add", "UObjectSended");
    UmosapiService::Api::response("200", "All is fine", "UObject");
    UmosapiService::Api::set_error_handler(&resource_error_handler);
    UmosapiService::Api::publish();

    UmosapiService::Api::set_path("/v2/{mcollection: .*}/{oid: .*}");
    UmosapiService::Api::set_method_handler("DELETE", deleteUObject);
    UmosapiService::Api::produce("application/json");
    UmosapiService::Api::parameter("mcollection", "Name of the collection where the uobject are located" );
    UmosapiService::Api::parameter("oid", "MongoDB oid of the uobject");
    UmosapiService::Api::response("200", "All is fine", "UObject");
    UmosapiService::Api::set_error_handler(&resource_error_handler);
    UmosapiService::Api::publish();

    UmosapiService::Api::set_path("/v2/{mcollection: .*}/{key: .*}/{value: .*}");
    UmosapiService::Api::set_method_handler("GET", searchUObjectByKeyValue);
    UmosapiService::Api::produce("application/json");
    UmosapiService::Api::parameter("mcollection", "Name of the collection where the uobject are located");
    UmosapiService::Api::parameter("key", "Key of uobject to search, ex: kill or total.kill");
    UmosapiService::Api::parameter("value", "Value of uobject to search, ex: 42");
    UmosapiService::Api::response("200", "All is fine", "UObject");
    UmosapiService::Api::set_error_handler(&resource_error_handler);
    UmosapiService::Api::publish();

    UmosapiService::Api::_service.set_error_handler( service_error_handler );
}

void UmosapiService::Api::retrieveAll( const std::shared_ptr< restbed::Session> session ){
    UmosapiService::uobject uobject;
    auto jsonObjects = json_object_new_array();
    const auto& request = session->get_request( );
    auto json_string = uobject.retrieveAll(request->get_path_parameter( "mcollection" ), jsonObjects);

    session->close( restbed::OK, json_string, {
        { "Content-Length", std::to_string(json_string.length()) },
        { "Content-Type", "application/json" } 
    });
    json_object_put(jsonObjects);

}

void UmosapiService::Api::addUObject( const std::shared_ptr< restbed::Session > session ){
    const auto request = session->get_request();

    size_t content_length = request->get_header( "Content-Length", 0 );

    session->fetch( content_length, [ request ]( const std::shared_ptr< restbed::Session > session, const restbed::Bytes & body )
    {
        UmosapiService::uobject uobject;
        auto jsonObject = json_object_new_object();
        char bodyData[body.size()+1];
        memset(bodyData, 0, sizeof(bodyData));
        snprintf(bodyData, sizeof(bodyData), "%.*s", ( int ) body.size( ), body.data());
        auto json_string = uobject.add(request->get_path_parameter("mcollection"), jsonObject, bodyData);
        session->close( restbed::OK, json_string, {
            { "Content-Length", std::to_string(json_string.length()) },
            { "Content-Type", "application/json" } 
        });
        json_object_put(jsonObject);
    } );
}

void UmosapiService::Api::deleteUObject( const std::shared_ptr< restbed::Session > session ){

    UmosapiService::uobject uobject;
    auto jsonObject = json_object_new_object();
    const auto request = session->get_request();

    auto json_string = uobject.remove(request->get_path_parameter("mcollection"), request->get_path_parameter("oid"), jsonObject);

    session->close( restbed::OK, json_string, {
        { "Content-Length", std::to_string(json_string.length()) },
        { "Content-Type", "application/json" }
    });
    json_object_put(jsonObject);
}

void UmosapiService::Api::searchUObjectByKeyValue( const std::shared_ptr< restbed::Session > session ){
    UmosapiService::uobject uobject;
    auto jsonObject = json_object_new_array();
    const auto request = session->get_request();

    auto json_string = uobject.searchKeyValue(request->get_path_parameter("mcollection"), request->get_path_parameter("key"), request->get_path_parameter("value"), jsonObject);

    session->close( restbed::OK, json_string, {
        { "Content-Length", std::to_string(json_string.length()) },
        { "Content-Type", "application/json" }
    });
    json_object_put(jsonObject);
}

void UmosapiService::Api::swaggerEndpoint( const std::shared_ptr< restbed::Session > session ){
    const auto request = session->get_request();

    std::ifstream stream(config["swaggerui"] + "/index.html", std::ifstream::in );

    if ( stream.is_open() ) {
        const std::string body = std::string( std::istreambuf_iterator< char >(stream), std::istreambuf_iterator< char>());

        const std::multimap< std::string, std::string> headers {
            { "Content-Type", "text/html" },
            { "Content-Length", std::to_string( body.length() ) }
        };
        session->close(restbed::OK, body, headers);
    } else {
        session->close( restbed::NOT_FOUND );
    }
}

void UmosapiService::Api::swaggerEndpointResources( const std::shared_ptr< restbed::Session > session )
{
    const auto request = session->get_request( );
    const std::string filename = request->get_path_parameter( "filename" );

    std::ifstream stream( config["swaggerui"] + "/" + filename, std::ifstream::in );

    if ( stream.is_open( ) )
    {
        const std::string body = std::string( std::istreambuf_iterator< char >( stream ), std::istreambuf_iterator< char >( ) );

        const std::multimap< std::string, std::string > headers
        {
            { "Content-Length", std::to_string( body.length( ) ) }
        };

        session->close( restbed::OK, body, headers );
    }
    else
    {
        session->close( restbed::NOT_FOUND );
    }
}

void UmosapiService::Api::swaggerEndpointApi( const std::shared_ptr< restbed::Session > session )
{
    const auto request = session->get_request( );
    const std::string filename = request->get_path_parameter( "filename" );
    const std::string path = request->get_path();

    std::ifstream stream( config["swaggerui"] + "/swagger.json", std::ifstream::in );

    if ( stream.is_open( ) )
    {
        const std::string body = std::string( std::istreambuf_iterator< char >( stream ), std::istreambuf_iterator< char >( ) );

        const std::multimap< std::string, std::string > headers
        {
            { "Content-Type", "application/json" },
            { "Content-Length", std::to_string( body.length( ) ) }
        };

        session->close( restbed::OK, body, headers );
    }
    else
    {
        session->close( restbed::NOT_FOUND );
    }
}