forked from sony/nmos-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings_api.cpp
72 lines (56 loc) · 2.96 KB
/
settings_api.cpp
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
#include "nmos/settings_api.h"
#include "nmos/api_utils.h"
#include "nmos/log_model.h"
#include "nmos/model.h"
#include "nmos/slog.h"
namespace nmos
{
namespace experimental
{
web::http::experimental::listener::api_router make_settings_api(nmos::base_model& model, nmos::experimental::log_model& log_model, slog::base_gate& gate_)
{
using namespace web::http::experimental::listener::api_router_using_declarations;
api_router settings_api;
settings_api.support(U("/?"), methods::GET, [](http_request, http_response res, const string_t&, const route_parameters&)
{
set_reply(res, status_codes::OK, nmos::make_sub_routes_body({ U("settings/") }, res));
return pplx::task_from_result(true);
});
settings_api.support(U("/settings/?"), methods::GET, [](http_request, http_response res, const string_t&, const route_parameters&)
{
set_reply(res, status_codes::OK, nmos::make_sub_routes_body({ U("all/") }, res));
return pplx::task_from_result(true);
});
settings_api.support(U("/settings/all/?"), methods::GET, [&model](http_request, http_response res, const string_t&, const route_parameters&)
{
auto lock = model.read_lock();
set_reply(res, status_codes::OK, model.settings);
return pplx::task_from_result(true);
});
settings_api.support(U("/settings/all/?"), methods::PATCH, [&model, &log_model, &gate_](http_request req, http_response res, const string_t&, const route_parameters& parameters)
{
nmos::api_gate gate(gate_, req, parameters);
return nmos::details::extract_json(req, gate).then([&model, &log_model, req, res, gate](value body) mutable
{
nmos::write_lock lock(model.mutex, std::defer_lock);
nmos::write_lock log_lock(log_model.mutex, std::defer_lock);
std::lock(lock, log_lock);
// Validate request?
// Merge the settings updates
web::json::merge_patch(model.settings, body, true);
// copy to the logging settings
// hmm, this is a bit icky, but simplest for now
log_model.settings = model.settings;
// the logging level is a special case because we want to turn it into an atomic value
// that can be read by logging statements without locking the mutex protecting the settings
log_model.level = nmos::fields::logging_level(log_model.settings);
// notify anyone who cares...
model.notify();
set_reply(res, status_codes::OK, model.settings);
return true;
});
});
return settings_api;
}
}
}