-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
144 lines (110 loc) · 3.38 KB
/
Copy pathapp.js
File metadata and controls
144 lines (110 loc) · 3.38 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
/*
© Copyright 2023-2023 E Reynolds, Inc. All rights reserved.
This program is confidential and proprietary to E Reynolds, and
may not be copied, reproduced, modified, disclosed to others, published or used,
in whole or in part, without the express prior written permission.
*/
import express from "express";
import { dirname } from "path";
import { fileURLToPath } from "url";
import dotenv from "dotenv";
import * as STRINGS from "./strings.js";
import swaggerUI from "swagger-ui-express";
import swaggerJSDoc from "swagger-jsdoc";
import testRouter from "./routes/test.js";
import testAuthRouter from "./routes/test_auth.js";
import userRouter from "./routes/user.js";
import quizRouter from './routes/quiz.js';
import cors from 'cors';
/* Set dirname prefix for the current root so that files can be served.
* e.g res.sendFile(_dirname + '/web/index.html'); */
const _dirname = dirname(fileURLToPath(import.meta.url));
const PROJECT = "api-server";
const MODULE = 'APP';
// Load and check env.
loadEnv();
// Set port from env or use default 4000.
const PORT = process.env.port || 4000;
// Create swagger-jsdoc options
const options = {
definition: {
openapi: '3.0.0',
info: {
title: 'api-server',
version: '1.0.0',
description: 'server-api test by E Reynolds.'
},
servers: [
{ url: `http://localhost:${PORT}` },
{ url: 'https://api-server-j2h3.onrender.com' }
]
},
// files containing annotations
apis: [
'./routes/*.js'
]
};
// Initialize swagger-jsdoc
const specs = swaggerJSDoc(options);
const app = express();
app.use(cors());
app.use(express.json());
app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(specs));
// Use routers.
app.use('/test', testRouter);
app.use('/user/', userRouter);
app.use('/test_auth', testAuthRouter);
app.use('/quiz', quizRouter);
//////////////////////////////////////////////////////////////////////////////////////////////
//
/* Handle GET requests to '/'
* - Redirect to api-docs
*/
app.get('/',
(req, res) => {
console.log('GET: "/"', req.body);
res.redirect("api-docs");
}
);
/** Load and check env.
* - If vars are not set...
* - - Assume we are in dev mode and try to load vars from .env file
* - Check for required env variables and exit if not found.
*/
function loadEnv() {
const METHOD = MODULE + ' loadEnv(): ';
console.log(METHOD);
console.log(STRINGS.SPACER);
// If vars are not set...
if (!checkVars()) {
// Assume we are in dev mode and try to load vars from .env file
console.log('= RUNNING ' + PROJECT + ' LOCALLY');
dotenv.config();
} else {
console.log('= RUNNING PRODUCTION');
}
console.log(STRINGS.SPACER);
// Check for required env constants and exit if not found.
if (!checkVars()) {
console.error(METHOD + STRINGS.ERROR + ' Unable to load all required env values.');
process.exit(5);
}
}
function checkVars() {
// return true if all required vars are loaded.
return (
process.env.MONGO_URI
&& process.env.ACCESS_TOKEN_SECRET
&& process.env.REFRESH_TOKEN_SECRET
);
}
//
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
//
// Start the app, listening on PORT "PORT".
app.listen(PORT,
() => {
console.log(MODULE + ' Server is running on port ' + PORT);
}
);