add selection api to back

This commit is contained in:
b1ek 2024-11-10 01:20:14 +10:00
parent f8f94217ee
commit 6dcce42cab
Signed by: blek
GPG Key ID: A622C22C9BC616B2
2 changed files with 31 additions and 5 deletions

View File

@ -1,13 +1,13 @@
import express from 'express'; import express from 'express';
import session from 'express-session'; import session from 'express-session';
import { TypeormStore } from 'connect-typeorm';
import 'reflect-metadata'; import 'reflect-metadata';
import routes from './routes.js'; import routes from './routes.js';
import dataSourceRepo from './typeorm/data-source.repo.js'; import dataSourceRepo from './typeorm/data-source.repo.js';
import { Session } from './typeorm/entity/Session.entity.js';
import './env.js'; import './env.js';
import { TypeORMError } from 'typeorm';
import { TypeormStore } from 'connect-typeorm';
const listen_host = process.env.LISTEN_HOST ?? '0.0.0.0'; const listen_host = process.env.LISTEN_HOST ?? '0.0.0.0';
const listen_port = parseInt(process.env.LISTEN_PORT ?? '80'); const listen_port = parseInt(process.env.LISTEN_PORT ?? '80');
@ -17,6 +17,12 @@ await dataSourceRepo.runMigrations();
const app = express(); const app = express();
declare module "express-session" {
interface SessionData {
selection: number[] | null
}
}
app.use(express.json()); app.use(express.json());
app.use(session({ app.use(session({
resave: false, resave: false,
@ -24,9 +30,13 @@ app.use(session({
store: new TypeormStore({ store: new TypeormStore({
cleanupLimit: 2, cleanupLimit: 2,
limitSubquery: false, limitSubquery: false,
ttl: 3600 ttl: 3600,
}), }).connect(dataSourceRepo.getRepository(Session)),
secret: 'keyboard cat' secret: 'keyboard cat',
cookie: {
httpOnly: true,
sameSite: true
}
})); }));
app.use('/api', routes()); app.use('/api', routes());

View File

@ -2,5 +2,21 @@ import { IRouter, Router } from "express";
export default function(): IRouter { export default function(): IRouter {
const app = Router(); const app = Router();
app.put('/', async (req, res) => {
const body = req.body as { selection?: number[] } | null;
if (!body) { res.send(400); return; }
if (!body.selection) { res.send(400); return; }
if (!(body.selection instanceof Array)) { res.send(400); return; }
req.session.selection = body.selection;
res.send(req.session.selection);
return
});
app.get('/', async (req, res) => {
res.send(req.session.selection ?? [])
})
return app; return app;
} }