customer-dashboard-api/server.ts

74 lines
2.0 KiB
TypeScript

import express, { Express } from "express";
import dotenv from "dotenv";
import bodyParser from "body-parser";
import swaggerUI from "swagger-ui-express";
import cors from "cors";
import storeRoutes from "./src/routes/storeRoutes";
import storeServicesRoutes from "./src/routes/storeServicesRoutes";
import userRoutes from "./src/routes/userRoutes";
import usersRoutes from "./src/routes/usersRoutes";
dotenv.config();
import swaggerJsDoc from "swagger-jsdoc";
import syncModels from "./src/models/index";
import logger from "./src/logger/logger";
const app: Express = express();
const host = process.env.HOST || "localhost";
const port = Number(process.env.PORT) || 3000;
const options = {
definition: {
openapi: "3.0.0",
info: {
title: "PartyApp API",
version: "1.0.0",
//description: "PartyApp API",
//termsOfService: "http://example.com/terms/",
/*contact: {
name: "API Support",
url: "http://www.exmaple.com/support",
email: "support@example.com",
},*/
},
servers: [
{
url: "http://localhost:3000/api/v1",
//description: "PartyApp API Documentation",
},
],
},
apis: ["./src/routes/*.ts"],
};
// TODO: add cors
app.use(cors());
app.use(bodyParser.json());
app.use("/api/v1/store", storeRoutes);
app.use("/api/v1/store/services", storeServicesRoutes);
app.use("/api/v1/user", userRoutes);
app.use("/api/v1/users", usersRoutes);
const specs = swaggerJsDoc(options);
app.use("/api-docs", swaggerUI.serve, swaggerUI.setup(specs));
app.use((req, res, next) => {
console.log("req not found, path:", req.path);
res.status(404).send("not found");
});
app.use((err: any, req: any, res: any, next: any) => {
console.log("req err", err.stack);
res.status(500).send({ err: "invalid request" });
});
syncModels();
app.listen(port, host, () => {
//console.log(`⚡️[server]: Server is running at http://${host}:${port}`);
logger.info(`⚡️[server]: Server is running at http://${host}:${port}`);
});