Skip to content

Commit

Permalink
Move version to search params
Browse files Browse the repository at this point in the history
  • Loading branch information
huulbaek committed Jan 23, 2024
1 parent 3c86bfb commit 5ada7c1
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const headers = {
'Access-Control-Allow-Headers': 'Content-Type',
}

const versions: string[] = ['v1']
const currentVersion: string = versions[versions.length - 1]
const allowedVersions: string[] = ['1']
let version: string = allowedVersions[allowedVersions.length - 1]

Bun.serve({
port: process.env.PORT,
Expand All @@ -34,8 +34,11 @@ Bun.serve({
return res
}
const url = new URL(req.url)
if (url.searchParams.get('version') !== null) { // We have a version parameter
version = url.searchParams.get('version') as string
}
if (url.pathname === "/") return new Response("Agiliate is running", { headers })
if (url.pathname === "/calculate" || url.pathname === `/calculate/${currentVersion}`) {
if (url.pathname === "/calculate") {
const jsonReq: IRequest = await req.json() as IRequest
if (process.env.CACHE === '1') {
const cachedResult = await redisClient.get(JSON.stringify(jsonReq))
Expand All @@ -46,11 +49,14 @@ Bun.serve({
const variables: IVariable = jsonReq.variables
const customSpaceConstants: TCustomSpaceConstants|undefined = jsonReq?.customSpaceConstants
const customConstants: IConstant|undefined = jsonReq?.customConstants
const calculator = new Calculator(variables, customSpaceConstants, customConstants, currentVersion)
const calculator = new Calculator(variables, customSpaceConstants, customConstants, version)
const result = calculator.result()
if (process.env.CACHE === '1')
redisClient.set(JSON.stringify(jsonReq), JSON.stringify(result))
return Response.json(result, { headers })
return Response.json({
... result,
version,
}, { headers })
}
return new Response("404!", { headers })
},
Expand Down

0 comments on commit 5ada7c1

Please sign in to comment.