Add basic api endpoint

This commit is contained in:
Owen Schwartz 2024-09-28 12:35:07 -04:00
parent 8308df1f49
commit 584a4f28dc
No known key found for this signature in database
GPG key ID: 8271FDFFD9E0CCBD
2 changed files with 29 additions and 0 deletions

View file

@ -1,4 +1,5 @@
import { Router } from "express";
import { getConfig } from "./getConfig";
const badger = Router();
@ -6,4 +7,6 @@ badger.get("/", (_, res) => {
res.status(200).json({ message: "Healthy" });
});
badger.get("/getConfig", getConfig);
export default badger;

View file

@ -0,0 +1,26 @@
import { Request, Response, NextFunction } from 'express';
import { Database } from 'better-sqlite3';
interface CustomRequest extends Request {
db?: Database;
}
export const getConfig = (req: Request, res: Response, next: NextFunction): void => {
try {
const customReq = req as CustomRequest;
const db = customReq.db;
if (!db) {
throw new Error('Database is not attached to the request');
}
const query = 'SELECT * FROM sites';
const statement = db.prepare(query);
const results = statement.all();
res.json(results);
} catch (error) {
console.error('Error querying database:', error);
next(error);
}
};