-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
810: Split session and non-session routes (#811)
- Loading branch information
1 parent
2b64c6c
commit 8215fd0
Showing
16 changed files
with
215 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# Always use LF for line endings | ||
* text eol=lf | ||
# Apart from font files | ||
# Apart from font files and images | ||
*.[ot]tf binary | ||
*.png -text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
OPENAI_API_KEY=YOUR_API_KEY | ||
SESSION_SECRET=YOUR_SESSION_SECRET | ||
CORS_ALLOW_ORIGIN=http://localhost:5173 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,16 @@ | ||
import cors from 'cors'; | ||
import dotenv from 'dotenv'; | ||
import express from 'express'; | ||
import session from 'express-session'; | ||
import memoryStoreFactory from 'memorystore'; | ||
import { fileURLToPath } from 'node:url'; | ||
|
||
import { importMetaUrl } from './importMetaUtils'; | ||
import { ChatModel, defaultChatModel } from './models/chat'; | ||
import { LevelState, getInitialLevelStates } from './models/level'; | ||
import { router } from './router'; | ||
import nonSessionRoutes from './nonSessionRoutes'; | ||
import sessionRoutes from './sessionRoutes'; | ||
|
||
dotenv.config(); | ||
|
||
declare module 'express-session' { | ||
interface Session { | ||
initialised: boolean; | ||
chatModel: ChatModel; | ||
levelState: LevelState[]; | ||
} | ||
} | ||
|
||
const app = express(); | ||
const isProd = app.get('env') === 'production'; | ||
|
||
// for parsing application/json | ||
app.use(express.json()); | ||
|
||
// use session storage - currently in-memory, but in future use Redis in prod builds | ||
const maxAge = 60 * 60 * 1000 * (isProd ? 1 : 8); //1 hour in prod, 8hrs in dev | ||
const sessionOpts: session.SessionOptions = { | ||
store: new (memoryStoreFactory(session))({ | ||
checkPeriod: maxAge, | ||
}), | ||
secret: process.env.SESSION_SECRET ?? 'secret', | ||
name: 'prompt-injection.sid', | ||
resave: false, | ||
saveUninitialized: true, | ||
cookie: { | ||
secure: isProd, | ||
maxAge, | ||
}, | ||
}; | ||
|
||
app.use(session(sessionOpts)); | ||
|
||
app.use( | ||
cors({ | ||
credentials: true, | ||
origin: true, | ||
}) | ||
); | ||
|
||
app.use((req, _res, next) => { | ||
// initialise session variables first time | ||
if (!req.session.initialised) { | ||
req.session.chatModel = defaultChatModel; | ||
req.session.levelState = getInitialLevelStates(); | ||
req.session.initialised = true; | ||
} | ||
next(); | ||
}); | ||
|
||
app.use('/', router); | ||
|
||
// serve the documents folder | ||
app.use( | ||
'/documents', | ||
express.static( | ||
fileURLToPath(new URL('../resources/documents', importMetaUrl())) | ||
export default express() | ||
.use(express.json()) | ||
.use( | ||
cors({ | ||
origin: process.env.CORS_ALLOW_ORIGIN, | ||
credentials: true, | ||
}) | ||
) | ||
); | ||
|
||
export default app; | ||
.use('/', nonSessionRoutes) | ||
.use('/', sessionRoutes); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import express from 'express'; | ||
import { fileURLToPath } from 'node:url'; | ||
|
||
import { handleGetDocuments } from './controller/documentController'; | ||
import { handleHealthCheck } from './controller/healthController'; | ||
import { handleGetSystemRoles } from './controller/systemRoleController'; | ||
import { importMetaUrl } from './importMetaUtils'; | ||
|
||
const router = express.Router(); | ||
|
||
router.use( | ||
'/documents', | ||
express.static( | ||
fileURLToPath(new URL('../resources/documents', importMetaUrl())) | ||
) | ||
); | ||
router.get('/documents', handleGetDocuments); | ||
router.get('/health', handleHealthCheck); | ||
router.get('/systemRoles', handleGetSystemRoles); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.