-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backend): create
services
& regexes
endpoints
- Loading branch information
Showing
5 changed files
with
167 additions
and
1 deletion.
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
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,68 @@ | ||
import { Router } from "express" | ||
import { z } from "zod" | ||
import { Database } from "../database/db" | ||
import { CerberoServiceCreate } from "../types/service" | ||
|
||
const regexesRoute = Router() | ||
|
||
regexesRoute.get("/:nfq", async (req, res) => { | ||
const redis = Database.getInstance() | ||
const { nfq } = req.params | ||
|
||
if(!parseInt(nfq)) { | ||
return res.status(400).json({ | ||
error: "The provided nfq id is not a number" | ||
}) | ||
} | ||
|
||
const activeRegexes = await redis.sMembers(`regexes:${nfq}:active`) | ||
const inactiveRegexes = await redis.sMembers(`regexes:${nfq}:inactive`) | ||
|
||
return res.status(201).json({ | ||
regexes: { | ||
active: activeRegexes, | ||
inactive: inactiveRegexes | ||
} | ||
}) | ||
}) | ||
|
||
regexesRoute.post("/:nfq", async (req, res) => { | ||
const redis = Database.getInstance() | ||
const { nfq } = req.params | ||
|
||
const bodySchema = z.object({ | ||
regexes: z.array(z.string()) | ||
}) | ||
|
||
let typeValidatedBody: Required<Pick<CerberoServiceCreate, "regexes">> | ||
|
||
try { | ||
typeValidatedBody = bodySchema.parse(req.body) | ||
} | ||
catch(e) { | ||
return res.status(400).json({ | ||
error: e | ||
}) | ||
} | ||
|
||
if(!parseInt(nfq)) { | ||
return res.status(400).json({ | ||
error: "The provided nfq id is not a number" | ||
}) | ||
} | ||
|
||
// Regexes are considered active by default | ||
await redis.sAdd(`regexes:${nfq}:active`, typeValidatedBody.regexes) | ||
|
||
const activeRegexes = await redis.sMembers(`regexes:${nfq}:active`) | ||
const inactiveRegexes = await redis.sMembers(`regexes:${nfq}:inactive`) | ||
|
||
return res.status(201).json({ | ||
regexes: { | ||
active: activeRegexes, | ||
inactive: inactiveRegexes | ||
} | ||
}) | ||
}) | ||
|
||
export default regexesRoute |
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,4 @@ | ||
export type CerberoRegexes = { | ||
active: string[] | ||
inactive: string[] | ||
} |
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,13 @@ | ||
import type { CerberoRegexes } from "./regex" | ||
|
||
export type CerberoService = { | ||
name: string | ||
nfq: number | ||
port: number | ||
protocol: "tcp" | "udp" | ||
regexes?: CerberoRegexes[] | ||
} | ||
|
||
export type CerberoServiceCreate = Omit<CerberoService, "regexes"> & { | ||
regexes?: string[] | ||
} |