-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
85 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { NextApiRequest, NextApiResponse } from "next" | ||
|
||
import { createPersistentDatabase } from "shared/database" | ||
import { loadServerConfig } from "shared/config" | ||
import { runMiddleware } from "utils/middleware" | ||
import cors from "cors" | ||
|
||
const dbRef = createPersistentDatabase() | ||
|
||
function parseNumberQuery(value: string | string[] | undefined) { | ||
if (typeof value === "string") { | ||
return Number(value) | ||
} | ||
return undefined | ||
} | ||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
await runMiddleware(req, res, cors({ methods: ["GET", "HEAD"] })) | ||
|
||
const { authConfig } = await loadServerConfig() | ||
const db = dbRef.create(authConfig.database) | ||
|
||
const folder = req.query.folder as string | ||
if (!folder) return res.status(400).send("Missing from parameter") | ||
|
||
const fromSec = parseNumberQuery(req.query.from) | ||
let toSec = parseNumberQuery(req.query.to) | ||
const length = parseNumberQuery(req.query.length) | ||
|
||
if (fromSec == null || (toSec == null && length == null)) | ||
return res.status(400).send("No query parameters set") | ||
|
||
if (toSec != null && length != null) | ||
return res | ||
.status(400) | ||
.send("Invalid parameters, can't have `to` and `length` at the same time") | ||
|
||
if (length) toSec = fromSec + length | ||
|
||
if (toSec == null) return res.status(400).send("Missing to parameter") | ||
|
||
res.send(await db.logRange(folder, fromSec, toSec)) | ||
} |
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,29 @@ | ||
import { NextApiRequest, NextApiResponse } from "next" | ||
import { loadServerConfig } from "shared/config" | ||
import { createPersistentDatabase } from "shared/database" | ||
import { runMiddleware } from "utils/middleware" | ||
import cors from "cors" | ||
|
||
const dbRef = createPersistentDatabase() | ||
|
||
const RANGE = 10 * 2 // 20s | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
await runMiddleware(req, res, cors({ methods: ["GET", "HEAD"] })) | ||
|
||
const { authConfig } = await loadServerConfig() | ||
const db = dbRef.create(authConfig.database) | ||
|
||
const folder = req.query.folder as string | ||
const shift = Number(req.query.shift || 0) | ||
|
||
if (!folder) return res.status(400).send("Invalid parameters") | ||
const timestampSec = Math.floor((Date.now() - shift * 1000) / 1000) | ||
|
||
res.send( | ||
await db.logRange(folder, timestampSec - RANGE, timestampSec + RANGE) | ||
) | ||
} |
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