summary refs log tree commit diff
path: root/src/api/routes.js
blob: ade440e53a9114c02d98d82c17644a7041c33417 (plain) (blame)
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
import * as routes from './routes/index.js';

function logHttpEntry(method, route, exampleBody) {
    console.log('%', method, '{{baseUrl}}' + route, 'HTTP/1.1');
    if (exampleBody) {
        console.log('%', 'Content-Type: application/json');
        console.log('% ');
        console.log('%', exampleBody);
        console.log('% ');
    }
    console.log('% ');
    console.log('%', '###');
}

export function registerRoutes(app) {
    // http file header:
    console.log('%', '');
    let routeCount = 0;

    Object.keys(routes).forEach(routeName => {
        /**
         * @type {RouteDescription}
         */
        const route = routes[routeName];
        console.log(
            `Registering ${routeName} (${route.path})`,
            routes[routeName]
        );

        Object.keys(route.methods).forEach(routeMethodName => {
            /**
             * @type {RouteMethod}
             */
            const routeMethod = route.methods[routeMethodName];
            console.log(routeMethodName, routeMethod);
            app[routeMethodName](route.path, [
                ...routeMethod.middlewares,
                routeMethod.method
            ]);
            routeCount++;
        });
    });

    console.log(`Registered ${routeCount} routes.`);
}