-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
147 lines (132 loc) · 3.48 KB
/
index.js
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
147
require("dotenv").config()
const express = require("express")
const forceSSL = require("express-force-ssl")
const rateLimit = require("express-rate-limit")
const swaggerUi = require("swagger-ui-express")
const swaggerJsdoc = require("swagger-jsdoc")
const cors = require("cors")
const morganMiddleware = require("./middleware/morgan.middleware")
const logger = require("./utils/logger")
const { connect } = require("./src/db")
const routes = require("./src/routes/routes")
const server = express()
const port = process.env.PORT || 3000
const host_port = process.env.HOST_PORT || process.env.PORT || 3000
const environment = process.env.NODE_ENV || "production"
const isDevelopment = environment === "development"
/**
* Create the initial database connection here
*/
connect(() =>
logger.info(
`📡 Database connection established http${
!isDevelopment ? "s" : ""
}://localhost:${host_port}/api/v1/services`
)
)
/**
* Settings & middleware
*/
server.set("trust proxy", 1)
// outside of dev environment if we send FORCE_SSL then SSL is forced
if (!isDevelopment) {
if (process.env.FORCE_SSL && process.env.FORCE_SSL.toLowerCase() === "true") {
server.use(forceSSL)
}
}
server.use(
rateLimit({
windowMs: 1 * 60 * 1000, // 1 minute
max: 1000, // limit each IP to 1000 requests per windowMs
})
)
server.use(cors())
server.use(morganMiddleware)
/**
* Swagger
*/
const swaggerJsdocOptions = {
swaggerDefinition: {
openapi: "3.0.0",
info: {
title: "Outpost API Service",
version: "1.0.0",
description:
"The Outpost service API is a RESTful API that provides access to the services in the Outpost Platform",
version: "1.0.0",
contact: {
name: "Outpost API service",
url: "https://github.com/wearefuturegov/outpost-api-service",
},
},
externalDocs: {
description: "Find out more about the Outpost Platform",
url: "https://outpost-platform.wearefuturegov.com/",
},
tags: [
{
name: "Services",
description: "Services and events",
externalDocs: {
description: "Find out more",
url: "https://developers.openreferraluk.org/Guidance/",
},
},
{
name: "Utilities",
description: "Utilities and helpers",
},
],
},
apis: ["./src/routes/*.js", "./src/routes/*.yml"],
failOnErrors: true,
}
const openapiSpecification = swaggerJsdoc(swaggerJsdocOptions)
var options = {
swaggerOptions: {
// validatorUrl: null,
},
}
server.use(
"/api-docs",
swaggerUi.serve,
swaggerUi.setup(openapiSpecification, options)
)
server.get("/schema.json", (req, res) => {
// And here we go, we serve it.
res.setHeader("Content-Type", "application/json")
res.send(openapiSpecification)
})
/**
* Routes
*/
routes.setup(server)
// 404
server.use("*", (req, res, next) => {
res.status(404).json({
error: "No route matches your request",
})
})
// Error handling
server.use((err, req, res, next) => {
logger.error(err.stack)
if (err.message === "No matching document") {
res.status(404).json({
error: "No matching document",
})
} else {
res.status(500).json({
error:
process.env.NODE_ENV === "production"
? "There was an internal server error. Please try again later"
: err.message,
})
}
})
/**
* Start the server
*/
server.listen(port, () => {
logger.info(`Server is running on port ${host_port}`)
logger.info(`Logging level is set to ${logger.level}`)
})