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/allow-duplicate-events',
70 apiHandler
: loggingAPI
['config'].setAllowDuplicateEvents
74 endpoint
: '/api/config/deny-events',
75 apiHandler
: loggingAPI
['config'].setAllowDuplicateEvents
79 endpoint
: '/api/config/sinks',
80 apiHandler
: loggingAPI
['config'].setSinks
84 endpoint
: '/api/config/syslog-viewer',
85 apiHandler
: loggingAPI
['config'].setSyslogViewer
87 // Operational methods
90 endpoint
: '/api/operational',
91 apiHandler
: loggingAPI
['operational'].get
94 // Development/testing methods
97 endpoint
: '/api/test/roundtrip',
98 apiHandler
: loggingAPI
['test'].roundtrip
102 // Logging routes. Initial refactoring pass at reducing code duplication
103 loggingRoutes
.forEach(function(route
) {
104 registerRoute(router
, route
.method
, route
.endpoint
, route
.apiHandler
);
107 module
.exports
= router
;
111 * Default route callback function
113 function routeCallback(apiHandler
) {
114 return function(req
, res
) {
115 apiHandler(req
).then(function(data
) {
116 utils
.sendSuccessResponse(data
, res
);
118 utils
.sendErrorResponse(error
, res
);
127 function registerRoute(app
, method
, endpoint
, apiHandler
) {
128 var methodUp
= method
.toUpperCase();
129 // This is the explict version that does not use reflection to cast the
130 // HTTP method to the corresponding express app function
131 if (methodUp
=== 'GET') {
132 app
.get(endpoint
, cors(), routeCallback(apiHandler
));
133 } else if (methodUp
=== 'PUT') {
134 app
.put(endpoint
, cors(), routeCallback(apiHandler
));
135 } else if (methodUp
=== 'POST') {
136 app
.post(endpoint
, cors(), routeCallback(apiHandler
));
137 } else if (methodUp
=== 'DELETE') {
138 app
.delete(endpoint
, cors(), routeCallback(apiHandler
));
140 console
.log("ERROR: Unsupported HTTP method: %s", method
);