164 lines
4.5 KiB
TypeScript
164 lines
4.5 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 GoogleStrategy from "passport-google-oauth20";
|
|
import cookieParser from "cookie-parser";
|
|
import session from "express-session";
|
|
import useragent from "express-useragent";
|
|
|
|
import calendarRoutes from "./src/routes/calendarRoutes";
|
|
import paymentRoutes from "./src/routes/paymentRoutes";
|
|
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, { initLogHandler } from "./src/logger/logger";
|
|
import passport from "passport";
|
|
import rabbitmq from "./src/rabbitmq/rabbitmq";
|
|
import { GOOGLE_CALLBACK_URL } from "./src/utils/constants";
|
|
import { StripeWebhook } from "./src/controllers/paymentController";
|
|
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"],
|
|
};
|
|
|
|
app.use(
|
|
session({
|
|
secret: "keyboard cat",
|
|
resave: false, // don't save session if unmodified
|
|
saveUninitialized: false, // don't create session until something stored
|
|
})
|
|
);
|
|
|
|
// app.use(passport.authenticate('session'));
|
|
|
|
passport.use(
|
|
new GoogleStrategy.Strategy(
|
|
{
|
|
clientID: process.env.GOOGLE_CLIENT_ID as string,
|
|
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
|
|
callbackURL: GOOGLE_CALLBACK_URL,
|
|
/*scope: [
|
|
"https://www.googleapis.com/auth/calendar.app.created",
|
|
"https://www.googleapis.com/auth/calendar.events.freebusy",
|
|
"https://www.googleapis.com/auth/userinfo.profile",
|
|
],*/
|
|
//skipUserProfile: false, // this is important, if not set to true, an error will be thrown
|
|
},
|
|
(accessToken, refreshToken, profile, cb) => {
|
|
console.log("accessToken", accessToken);
|
|
console.log("refreshToken", refreshToken);
|
|
console.log("profile", profile);
|
|
|
|
return cb(null, { profile, accessToken, refreshToken });
|
|
}
|
|
)
|
|
);
|
|
|
|
interface User {
|
|
id: string;
|
|
username: string;
|
|
name: string;
|
|
}
|
|
|
|
passport.serializeUser(function (user, cb) {
|
|
process.nextTick(function () {
|
|
//console.log("user", user);
|
|
|
|
cb(null, {});
|
|
});
|
|
});
|
|
|
|
passport.deserializeUser(function (user: User, cb) {
|
|
process.nextTick(function () {
|
|
return cb(null, user);
|
|
});
|
|
});
|
|
|
|
// TODO: setup cors
|
|
app.use(cors());
|
|
app.use(cookieParser());
|
|
app.use(useragent.express());
|
|
|
|
// we need to parse the body of the request to get the stripe signature
|
|
// it is important that the webhook is registered before the body parser
|
|
// see https://github.com/stripe/stripe-node/issues/341
|
|
app.use(
|
|
"/api/v1/webhooks/stripe",
|
|
bodyParser.raw({ type: "application/json" }),
|
|
StripeWebhook
|
|
);
|
|
app.use(bodyParser.json());
|
|
app.use("/api/v1/calendar", calendarRoutes);
|
|
app.use("/api/v1/payment", paymentRoutes);
|
|
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) => {
|
|
logger.warn(`req not found, path: ${req.path}`);
|
|
res.status(404).send("not found");
|
|
});
|
|
|
|
app.use((err: any, req: any, res: any, next: any) => {
|
|
logger.error(`req err: ${err.stack}`);
|
|
res.status(500).send({ err: "invalid request" });
|
|
});
|
|
|
|
syncModels();
|
|
|
|
// start server
|
|
|
|
initLogHandler();
|
|
|
|
rabbitmq
|
|
.connect(
|
|
process.env.RABBITMQ_USERNAME as string,
|
|
process.env.RABBITMQ_PASSWORD as string,
|
|
process.env.RABBITMQ_HOST as string,
|
|
process.env.RABBITMQ_PORT as string
|
|
)
|
|
.then(() => {
|
|
app.listen(port, host, () =>
|
|
logger.info(`⚡️[server]: Server is running at http://${host}:${port}`)
|
|
);
|
|
})
|
|
.catch((err) => {
|
|
logger.error(err);
|
|
});
|