feature: Adds backend creation.
This commit is contained in:
parent
61ee91a955
commit
2237568b92
4 changed files with 93 additions and 5 deletions
23
routes/NextNet API/Create Backend.bru
Normal file
23
routes/NextNet API/Create Backend.bru
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
meta {
|
||||||
|
name: Create User
|
||||||
|
type: http
|
||||||
|
seq: 1
|
||||||
|
}
|
||||||
|
|
||||||
|
post {
|
||||||
|
url: http://127.0.0.1:3000/api/v1/backends/create
|
||||||
|
body: json
|
||||||
|
auth: none
|
||||||
|
}
|
||||||
|
|
||||||
|
body:json {
|
||||||
|
{
|
||||||
|
"token": "134597ea81976ac0799824ea3f345cfd2e31b9febaeef10ba93817fa562862d69e88d8e3acd58ebdd1345a1fd11f38b7153c5d8516edce67aaebce55fc9cde",
|
||||||
|
"name": "PortCopier Route",
|
||||||
|
"description": "This is a test route for portcopier.",
|
||||||
|
"backend": "PortCopier",
|
||||||
|
"connectionDetails": {
|
||||||
|
"funny": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
12
src/index.ts
12
src/index.ts
|
@ -4,8 +4,10 @@ import { PrismaClient } from '@prisma/client';
|
||||||
import Fastify from "fastify";
|
import Fastify from "fastify";
|
||||||
|
|
||||||
import { ServerOptions, SessionToken } from "./libs/types.js";
|
import { ServerOptions, SessionToken } from "./libs/types.js";
|
||||||
import { route as create } from "./routes/user/create.js";
|
import { route as backendCreate } from "./routes/backends/create.js";
|
||||||
import { route as login } from "./routes/user/login.js";
|
|
||||||
|
import { route as userCreate } from "./routes/user/create.js";
|
||||||
|
import { route as userLogin } from "./routes/user/login.js";
|
||||||
|
|
||||||
const prisma = new PrismaClient();
|
const prisma = new PrismaClient();
|
||||||
|
|
||||||
|
@ -31,8 +33,10 @@ const fastify = Fastify({
|
||||||
logger: true
|
logger: true
|
||||||
});
|
});
|
||||||
|
|
||||||
create(fastify, prisma, sessionTokens, serverOptions);
|
backendCreate(fastify, prisma, sessionTokens, serverOptions);
|
||||||
login(fastify, prisma, sessionTokens, serverOptions);
|
|
||||||
|
userCreate(fastify, prisma, sessionTokens, serverOptions);
|
||||||
|
userLogin(fastify, prisma, sessionTokens, serverOptions);
|
||||||
|
|
||||||
// Run the server!
|
// Run the server!
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
# Route Plan
|
# Route Plan
|
||||||
- /api/v1/users/create
|
- /api/v1/users/create
|
||||||
- /api/v1/users/disable
|
- /api/v1/users/login
|
||||||
- /api/v1/users/remove
|
- /api/v1/users/remove
|
||||||
- /api/v1/backends/create
|
- /api/v1/backends/create
|
||||||
- /api/v1/backends/remove
|
- /api/v1/backends/remove
|
||||||
|
|
61
src/routes/backends/create.ts
Normal file
61
src/routes/backends/create.ts
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
import type { PrismaClient } from "@prisma/client";
|
||||||
|
import type { FastifyInstance } from "fastify";
|
||||||
|
|
||||||
|
import { ServerOptions, SessionToken } from "../../libs/types.js";
|
||||||
|
import { hasPermissionByToken } from "../../libs/permissions.js";
|
||||||
|
|
||||||
|
export function route(fastify: FastifyInstance, prisma: PrismaClient, tokens: Record<number, SessionToken[]>, options: ServerOptions) {
|
||||||
|
function hasPermission(token: string, permissionList: string[]): Promise<boolean> {
|
||||||
|
return hasPermissionByToken(permissionList, token, tokens, prisma);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new backend to use
|
||||||
|
*/
|
||||||
|
fastify.post("/api/v1/backends/create", {
|
||||||
|
schema: {
|
||||||
|
body: {
|
||||||
|
type: "object",
|
||||||
|
required: ["token", "name", "backend", "connectionDetails"],
|
||||||
|
|
||||||
|
properties: {
|
||||||
|
token: { type: "string" },
|
||||||
|
name: { type: "string" },
|
||||||
|
description: { type: "string" },
|
||||||
|
backend: { type: "string" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, async(req, res) => {
|
||||||
|
// @ts-ignore
|
||||||
|
const body: {
|
||||||
|
token: string,
|
||||||
|
name: string,
|
||||||
|
description?: string,
|
||||||
|
connectionDetails: any,
|
||||||
|
backend: string
|
||||||
|
} = req.body;
|
||||||
|
|
||||||
|
if (!await hasPermission(body.token, [
|
||||||
|
"backends.add"
|
||||||
|
])) {
|
||||||
|
return res.status(403).send({
|
||||||
|
error: "Unauthorized"
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
await prisma.desinationProvider.create({
|
||||||
|
data: {
|
||||||
|
name: body.name,
|
||||||
|
description: body.description,
|
||||||
|
|
||||||
|
backend: body.backend,
|
||||||
|
connectionDetails: JSON.stringify(body.connectionDetails)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: true
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue