-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (57 loc) · 1.7 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
require("dotenv").config();
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const winston = require("winston");
const spaceRoutes = require("./routes/spaceRoutes");
const swaggerUi = require("swagger-ui-express");
const swaggerJsdoc = require("swagger-jsdoc");
const promClient = require('prom-client');
const app = express();
const port = 3000;
promClient.collectDefaultMetrics();
// Set up Winston logger
const logger = winston.createLogger({
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: "error.log", level: "error" }),
new winston.transports.File({ filename: "combined.log" }),
],
});
// Connect to MongoDB
mongoose
.connect(process.env.MONGODB_CONNECTION)
.then(() => {
logger.info("Connected to MongoDB");
app.listen(port, () => {
logger.info(`Server is running on port ${port}`);
});
})
.catch((error) => {
logger.error("Failed to connect to MongoDB:", error);
});
app.use(bodyParser.json());
// Swagger configuration
const swaggerOptions = {
definition: {
openapi: "3.0.0",
info: {
title: "Spaces API",
version: "1.0.0",
description: "APIs for managing spaces",
},
},
apis: ["./routes/*.js"], // Path to the API routes
};
const swaggerSpec = swaggerJsdoc(swaggerOptions);
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec));
app.get("/", (req, res) => {
res.redirect("/api-docs");
});
// Expose metrics endpoint
app.get('/metrics', async (req, res) => {
res.set('Content-Type', promClient.register.contentType);
res.end(await promClient.register.metrics());
});
// Routes
app.use("/spaces", spaceRoutes);