3 * Copyright 2016 RIFT.IO Inc
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 var router
= require('express').Router();
19 var cors
= require('cors');
20 var utils
= require('../../framework/core/api_utils/utils.js')
22 var loggingAPI
= require('./api/logging.js');
29 endpoint
: '/api/aggregate',
30 apiHandler
: loggingAPI
['aggregate'].get
34 endpoint
: '/api/config',
35 apiHandler
: loggingAPI
['config'].get
37 // Config modify methods
40 endpoint
: '/api/config',
41 apiHandler
: loggingAPI
['config'].set
45 endpoint
: '/api/aggregate',
46 apiHandler
: loggingAPI
['aggregate'].set
50 endpoint
: '/api/config/console',
51 apiHandler
: loggingAPI
['config'].setConsole
55 endpoint
: '/api/config/filter',
56 apiHandler
: loggingAPI
['config'].setFilter
60 endpoint
: '/api/config/default-severity',
61 apiHandler
: loggingAPI
['config'].setDefaultSeverity
64 endpoint
: '/api/config/default-severity',
65 apiHandler
: loggingAPI
['config'].deleteDefaultSeverity
69 endpoint
: '/api/config/default-syslog-severity',
70 apiHandler
: loggingAPI
['config'].setDefaultSyslogSeverity
74 endpoint
: '/api/config/default-syslog-severity/:nulledCategories',
75 apiHandler
: loggingAPI
['config'].deleteDefaultSyslogSeverity
79 endpoint
: '/api/config/allow-duplicate-events',
80 apiHandler
: loggingAPI
['config'].setAllowDuplicateEvents
84 endpoint
: '/api/config/deny-events',
85 apiHandler
: loggingAPI
['config'].setAllowDuplicateEvents
89 endpoint
: '/api/config/sinks',
90 apiHandler
: loggingAPI
['config'].setSinks
94 endpoint
: '/api/config/syslog-viewer',
95 apiHandler
: loggingAPI
['config'].setSyslogViewer
97 // Operational methods
100 endpoint
: '/api/operational',
101 apiHandler
: loggingAPI
['operational'].get
104 // Development/testing methods
107 endpoint
: '/api/test/roundtrip',
108 apiHandler
: loggingAPI
['test'].roundtrip
112 // Logging routes. Initial refactoring pass at reducing code duplication
113 loggingRoutes
.forEach(function(route
) {
114 registerRoute(router
, route
.method
, route
.endpoint
, route
.apiHandler
);
117 module
.exports
= router
;
121 * Default route callback function
123 function routeCallback(apiHandler
) {
124 return function(req
, res
) {
125 apiHandler(req
).then(function(data
) {
126 utils
.sendSuccessResponse(data
, res
);
128 utils
.sendErrorResponse(error
, res
);
137 function registerRoute(app
, method
, endpoint
, apiHandler
) {
138 var methodUp
= method
.toUpperCase();
139 // This is the explict version that does not use reflection to cast the
140 // HTTP method to the corresponding express app function
141 if (methodUp
=== 'GET') {
142 app
.get(endpoint
, cors(), routeCallback(apiHandler
));
143 } else if (methodUp
=== 'PUT') {
144 app
.put(endpoint
, cors(), routeCallback(apiHandler
));
145 } else if (methodUp
=== 'POST') {
146 app
.post(endpoint
, cors(), routeCallback(apiHandler
));
147 } else if (methodUp
=== 'DELETE') {
148 app
.delete(endpoint
, cors(), routeCallback(apiHandler
));
150 console
.log("ERROR: Unsupported HTTP method: %s", method
);