All files / src server.ts

0% Statements 0/28
100% Branches 0/0
0% Functions 0/2
0% Lines 0/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                             
import dotenv from "dotenv";
import express from "express";
import cors from "cors";
import { setupDatabase } from "./config/database";
import { swaggerConfig } from "./config/swagger";
import swaggerJSdoc from "swagger-jsdoc";
import swaggerUi from "swagger-ui-express";
import devicesRouter from "./api/routes/devices"
import thingyRouter from "./api/routes/thingy";
import tagRouter from "./api/routes/tag";
import { MqttConnection } from "./config/mqtt";
 
dotenv.config();
 
const app = express();
const swaggerDocs = swaggerJSdoc(swaggerConfig);
const mqttService = new MqttConnection();
 
setupDatabase()
  .then(() => {
    console.log("Connected to database");
  })
  .catch((error) => {
    console.error("Database connection failed:", error);
    process.exit(1);
  });
 
 
app.use(
  cors({
    origin: "*",    
    methods: ["GET", "POST", "PUT", "DELETE"],
    allowedHeaders: ["Content-Type", "Authorization"],
  })
);
 
app.use(express.json());
 
app.use("/api/docs", swaggerUi.serve, swaggerUi.setup(swaggerDocs));
 
app.use("/api/devices", devicesRouter);
 
app.use("/api/thingys", thingyRouter);
 
app.use("/api/tags", tagRouter);
 
 
 
// Define the topics to subscribe to
const topics = ['smartclassroom/air-sensing/#', 'smartclassroom/tag/#'];
// Connect to the MQTT broker and subscribe to the topics
mqttService.connectToBroker(topics);
 
export { app };