All files / src server.ts

0% Statements 0/23
100% Branches 0/0
0% Functions 0/3
0% Lines 0/23

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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72                                                                                                                                               
import express from "express"
import pino from "pino"
 
import { env } from "./config"
import { about } from "./endpoints/about"
import { autoImportPage } from "./endpoints/autoImportPage"
import { categoriesForTransaction } from "./endpoints/categoriesForTransaction"
import { createNewCategory } from "./endpoints/createNewCategory"
import { settingBudgetForTransaction } from "./endpoints/settingBudgetForTransaction"
import { settingCategoryForTransaction } from "./endpoints/settingCategoryForTransaction"
import { triggerAutoImport } from "./endpoints/triggerAutoImport"
import { webhook } from "./endpoints/webhook"
import { initializeJobs } from "./queues"
import { AssertTransactionExistsMiddleware } from "./utils/assertTransactionExistsMiddleware"
import { ParseBodyMiddleware } from "./utils/middleware"
import { TokenMiddleware } from "./utils/tokenMiddleware"
import { TransactionResultMiddleware } from "./utils/transactionResultMiddleware"
import { verifyWebhookMiddleware } from "./utils/webhookSecret"
 
const logger = pino()
const app = express()
 
app.use("/public", express.static("public"))
app.set("views", "templates")
app.set("view engine", "pug")
 
app.use(ParseBodyMiddleware)
 
app.get(
  "/transaction/:transactionId/budget/:budget_id",
  TokenMiddleware,
  AssertTransactionExistsMiddleware,
  settingBudgetForTransaction,
  TransactionResultMiddleware,
)
app.get("/transaction/:transactionId/categories", TokenMiddleware, AssertTransactionExistsMiddleware, categoriesForTransaction)
app.get(
  "/transaction/:transactionId/category/:category_id",
  TokenMiddleware,
  AssertTransactionExistsMiddleware,
  settingCategoryForTransaction,
  TransactionResultMiddleware,
)
app.get(
  "/transaction/:transactionId/newCategory",
  TokenMiddleware,
  createNewCategory,
  settingCategoryForTransaction,
  TransactionResultMiddleware,
)
app.post("/webhook", verifyWebhookMiddleware, webhook)
app.get("/autoimport", TokenMiddleware, autoImportPage)
app.post("/autoimport", TokenMiddleware, triggerAutoImport)
app.get("/about", about)
 
async function startServer() {
  try {
    await new Promise<void>((resolve) => {
      app.listen(env.port, () => {
        logger.info("Server is running on http://localhost:%s", env.port)
        resolve()
      })
    })
    await initializeJobs()
  } catch (err) {
    logger.error({ err }, "Failed to start server:")
    process.exit(1)
  }
}
 
startServer()