mirror of
https://github.com/fosrl/pangolin.git
synced 2025-07-29 15:14:56 +02:00
Add basic api endpoint
This commit is contained in:
parent
8308df1f49
commit
584a4f28dc
2 changed files with 29 additions and 0 deletions
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue