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

View File

@ -2,5 +2,21 @@ import { IRouter, Router } from "express";
export default function(): IRouter {
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;
}