zappyzep/src/api/index.ts

46 lines
1 KiB
TypeScript
Raw Normal View History

2019-06-23 19:18:41 +03:00
import { error, notFound } from "./responses";
require("dotenv").config({ path: path.resolve(__dirname, "..", "..", "api.env") });
2019-05-26 00:13:42 +03:00
import express from "express";
import cors from "cors";
import { initAuth } from "./auth";
import { initGuildsAPI } from "./guilds";
2019-06-23 19:18:41 +03:00
import { initArchives } from "./archives";
import { connect } from "../data/db";
import path from "path";
2019-05-26 00:13:42 +03:00
console.log("Connecting to database...");
connect().then(() => {
const app = express();
app.use(
cors({
origin: process.env.DASHBOARD_URL,
}),
);
app.use(express.json());
2019-05-26 00:13:42 +03:00
initAuth(app);
initGuildsAPI(app);
2019-06-23 19:18:41 +03:00
initArchives(app);
2019-05-26 00:13:42 +03:00
2019-06-23 19:18:41 +03:00
// Default route
app.get("/", (req, res) => {
res.end({ status: "cookies" });
});
// Error response
app.use((err, req, res, next) => {
2019-06-23 19:18:41 +03:00
error(res, err.message, err.status || 500);
});
2019-06-23 19:18:41 +03:00
// 404 response
app.use((req, res, next) => {
return notFound(res);
2019-05-26 00:13:42 +03:00
});
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`API server listening on port ${port}`));
});