Repository navigation
Expand file tree
/
Copy pathroute.js
More file actions
146 lines (138 loc) · 5.91 KB
/
Copy pathroute.js
File metadata and controls
146 lines (138 loc) · 5.91 KB
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
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict'
/* eslint no-console: "off" */
/* eslint no-undef: "off" */
/* eslint no-unused-vars: "off" */
/* eslint callback-return: "off" */
const k = require('./constants')
const utils = require('./utils')
// sample data for pages and responses
const commonPageData = {
projectName: k.packageName,
projectVersion: k.packageVersion,
environment: utils.currentEnv(),
assets: k.mappings.staticAssetsMapping,
welcome: 'Hello from EJS Templates',
sampleRoutes: null,
pluginRoutes: null
}
// load some publish/subscribe utility functions
const { publish, subscribe } = require('./pubsub')
// manually define the list of some exposed routes, with some description
const sampleRoutes = [
{ link: 'time', url: '/time', description: 'Sample API call that returns the current server time, as timestamp and in ISO format (async)' },
{ link: 'error', url: '/error', description: 'Sample route that always returns an error (async)' },
{ link: 'template', url: '/template', description: 'Sample EJS template page' },
{ link: 'app', url: '/info/app', description: 'Show some info about application package version etc (async)' },
{ link: 'scm', url: '/info/scm', description: 'Show some info about source control system (if any): branch, commit hash, version, etc (async)' }
// ].sort((a, b) => a.link.localeCompare(b.link))
].sort(utils.compareProperties('link')) // opt. add sort order, 'asc' (by default) or 'desc'
// manually define the list of some routes exposed by some loaded plugins
const pluginRoutes = [
{ link: 'static', url: '/static/', description: "Serve static resources, sample (by default 'index.html' will be served, by 'fastify-static' plugin" },
{ link: 'favicon', url: '/favicon.ico', description: "Serve the favicon, by 'fastify-favicon' plugin" },
{ link: 'custom-webhook', url: '/custom-webhook', description: "Expose a sample webhook (via HTTP POST), by 'fastify-webhook' plugin" },
{ link: 'healthcheck', url: '/health', description: "Expose an healthcheck, by 'fastify-healthcheck' plugin" }
].sort(utils.compareProperties('link')) // opt. add sort order, 'asc' (by default) or 'desc'
// define routes but no async at outer level
// note that some routes here are normal (non-async) but others are async ...
function routes (fastify, options = {}) {
// define the root route
fastify.get('/', (request, reply) => {
// utils.logRoute(request)
reply.view('index', {
...commonPageData,
title: 'Home',
welcome: 'Work-In-Progress/Prototype webapp',
sampleRoutes,
pluginRoutes
})
// publish a message in the queue, as a sample
publish(fastify.nats, k.queueName, k.queueDisabled,
`Hello World, from the root page of a Fastify web application at '${k.hostname}'!`
)
})
// example route to return current timestamp, in async way
fastify.get('/time', async (request, reply) => {
const now = new Date()
const timestamp = now.getTime()
// publish a message in the queue, as a sample
publish(fastify.nats, k.queueName, k.queueDisabled,
`Ask for server time: timestamp is ${timestamp} at '${k.hostname}'`
)
return {
timestamp,
time: now.toISOString()
}
})
// example route to return a page from a template, but no async here
fastify.get('/template', (request, reply) => {
// publish a message in the queue, as a sample
publish(fastify.nats, k.queueName, k.queueDisabled,
`Ask for a template page at '${k.hostname}'`
)
reply.view('index', {
...commonPageData,
title: 'Template'
})
})
// example route, to always generate an error
fastify.get('/error', async (req, reply) => {
reply.code(500)
const err = new Error()
err.message = 'Error Message'
err.statusCode = reply.code
err.description = 'Verbose Error description...'
// publish a message in the queue, as a sample
publish(fastify.nats, k.queueName, k.queueDisabled,
`Ask for error page at '${k.hostname}'`
)
return err
})
// example route to return some info on current package/version, in async way
fastify.get('/info/app', async (request, reply) => {
return {
app: {
env: utils.fromEnv('ENVIRONMENT'),
frameworkVersion: k.fastifyVersion,
name: k.packageName,
version: k.packageVersion
},
runtime: {
mode: utils.currentEnv()
}
}
})
// example route to return some info on current scm (if available), in async way
fastify.get('/info/scm', async (request, reply) => {
// check git related data by executing it
const scm = {}
try { // simpler, catch errors from all promises via try/catch in callers ...
// scm.description = await utils.gitVersion().catch(e => fastify.log.error('Error: ', e.message)), // sample catch for error in a single promise
scm.description = await utils.gitVersion()
scm.branch = await utils.gitBranch()
scm.hashShort = await utils.gitHashShort()
scm.hashFull = await utils.gitHashFull()
} catch (e) {
fastify.log.error(e)
// scm = undefined // empty scm object, to remove from returned values
// async functions must always return a result, pay attention ...
}
return scm
})
}
module.exports = routes