2022-10-29 22:21:24 +02:00
|
|
|
// Dependencies
|
|
|
|
import express from 'express'
|
|
|
|
import chalk from 'chalk'
|
|
|
|
import path from 'path'
|
|
|
|
import fs from 'fs'
|
|
|
|
import { PrismaClient } from '@prisma/client'
|
|
|
|
import passport from 'passport'
|
|
|
|
// Routes
|
|
|
|
import { routes } from './routes/index.mjs'
|
|
|
|
// Config
|
|
|
|
import { verifyConfig } from './config.mjs'
|
|
|
|
// Middleware
|
|
|
|
import { loadExpressMiddleware, loadPassportMiddleware } from './middleware.mjs'
|
|
|
|
// Encryption
|
2022-10-31 08:36:36 +01:00
|
|
|
import { encryption } from './utils/crypto.mjs'
|
2022-11-01 18:00:25 +01:00
|
|
|
// Email
|
|
|
|
import { mailer } from './utils/email.mjs'
|
2022-10-29 22:21:24 +02:00
|
|
|
|
|
|
|
// Bootstrap
|
|
|
|
const config = verifyConfig()
|
|
|
|
const prisma = new PrismaClient()
|
|
|
|
const app = express()
|
|
|
|
app.use(express.json())
|
|
|
|
app.use(express.static('public'))
|
|
|
|
|
2022-11-05 18:55:59 +01:00
|
|
|
const tools = {
|
2022-10-29 22:21:24 +02:00
|
|
|
app,
|
|
|
|
passport,
|
2022-10-30 19:16:19 +01:00
|
|
|
prisma,
|
2022-10-29 22:21:24 +02:00
|
|
|
...encryption(config.encryption.key),
|
2022-11-01 18:00:25 +01:00
|
|
|
...mailer(config),
|
2022-10-29 22:25:00 +02:00
|
|
|
config,
|
2022-10-29 22:21:24 +02:00
|
|
|
}
|
2022-11-05 18:55:59 +01:00
|
|
|
|
|
|
|
// Load middleware
|
|
|
|
loadExpressMiddleware(app)
|
|
|
|
loadPassportMiddleware(passport, tools)
|
|
|
|
|
2022-10-29 22:21:24 +02:00
|
|
|
// Load routes
|
2022-11-05 18:55:59 +01:00
|
|
|
for (const type in routes) routes[type](tools)
|
2022-10-29 22:21:24 +02:00
|
|
|
|
|
|
|
// Catch-all route (Load index.html once instead of at every request)
|
|
|
|
const index = fs.readFileSync(path.resolve('.', 'src', 'landing', 'index.html'))
|
|
|
|
app.get('/', async (req, res) => res.set('Content-Type', 'text/html').status(200).send(index))
|
|
|
|
|
|
|
|
// Start listening for requests
|
|
|
|
app.listen(config.port, (err) => {
|
|
|
|
if (err) console.error(chalk.red('Error occured'), err)
|
|
|
|
if (process.env.NODE_ENV === 'development') console.log(chalk.yellow('> in development'))
|
|
|
|
console.log(chalk.green(`🚀 REST API ready, listening on ${config.api}`))
|
|
|
|
})
|