mirror of
https://github.com/NebulaServices/Nebula.git
synced 2025-05-13 03:50:02 -04:00
Chore: format
This commit is contained in:
parent
7d325d424a
commit
c18f175b08
8 changed files with 236 additions and 195 deletions
237
server/server.ts
237
server/server.ts
|
@ -1,47 +1,53 @@
|
|||
import { createWriteStream } from "node:fs";
|
||||
import { constants, access, mkdir } from "node:fs/promises";
|
||||
import { pipeline } from "node:stream/promises";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import fastifyCompress from "@fastify/compress";
|
||||
import fastifyMiddie from "@fastify/middie";
|
||||
import fastifyStatic from "@fastify/static";
|
||||
import fastifyMultipart from "@fastify/multipart";
|
||||
import Fastify, { FastifyReply, FastifyRequest } from 'fastify';
|
||||
import chalk from 'chalk';
|
||||
import { serverFactory } from "./serverFactory.js";
|
||||
import { handler as ssrHandler } from "../dist/server/entry.mjs";
|
||||
import fastifyStatic from "@fastify/static";
|
||||
import chalk from "chalk";
|
||||
import Fastify, { FastifyReply, FastifyRequest } from "fastify";
|
||||
import gradient from "gradient-string";
|
||||
import { parsedDoc } from "./config.js";
|
||||
import { DataTypes, InferAttributes, InferCreationAttributes, Model, Sequelize } from "sequelize";
|
||||
import { pipeline } from "node:stream/promises";
|
||||
import { createWriteStream } from "node:fs";
|
||||
import { handler as ssrHandler } from "../dist/server/entry.mjs";
|
||||
import { parsedDoc } from "./config.js";
|
||||
import { setupDB } from "./dbSetup.js";
|
||||
import { access, constants, mkdir } from "node:fs/promises";
|
||||
import { serverFactory } from "./serverFactory.js";
|
||||
|
||||
|
||||
const app = Fastify({ logger: parsedDoc.server.server.logging, ignoreDuplicateSlashes: true, ignoreTrailingSlash: true, serverFactory: serverFactory });
|
||||
const app = Fastify({
|
||||
logger: parsedDoc.server.server.logging,
|
||||
ignoreDuplicateSlashes: true,
|
||||
ignoreTrailingSlash: true,
|
||||
serverFactory: serverFactory
|
||||
});
|
||||
const db = new Sequelize(parsedDoc.db.name, parsedDoc.db.username, parsedDoc.db.password, {
|
||||
host: parsedDoc.db.postgres ? `${parsedDoc.postgres.domain}` : 'localhost',
|
||||
host: parsedDoc.db.postgres ? `${parsedDoc.postgres.domain}` : "localhost",
|
||||
port: parsedDoc.db.postgres ? parsedDoc.postgres.port : undefined,
|
||||
dialect: parsedDoc.db.postgres ? 'postgres': 'sqlite',
|
||||
dialect: parsedDoc.db.postgres ? "postgres" : "sqlite",
|
||||
logging: parsedDoc.server.server.logging,
|
||||
storage: 'database.sqlite' //this is sqlite only
|
||||
storage: "database.sqlite" //this is sqlite only
|
||||
});
|
||||
|
||||
type CatalogType = "theme" | "plugin"
|
||||
type CatalogType = "theme" | "plugin";
|
||||
|
||||
interface Catalog {
|
||||
package_name: string
|
||||
title: string
|
||||
description: string
|
||||
author: string
|
||||
image: string
|
||||
tags: object
|
||||
version: string
|
||||
background_image: string
|
||||
background_video: string
|
||||
payload: string
|
||||
type: CatalogType
|
||||
package_name: string;
|
||||
title: string;
|
||||
description: string;
|
||||
author: string;
|
||||
image: string;
|
||||
tags: object;
|
||||
version: string;
|
||||
background_image: string;
|
||||
background_video: string;
|
||||
payload: string;
|
||||
type: CatalogType;
|
||||
}
|
||||
|
||||
interface CatalogModel extends Catalog, Model<InferAttributes<CatalogModel>, InferCreationAttributes<CatalogModel>> {};
|
||||
interface CatalogModel
|
||||
extends Catalog,
|
||||
Model<InferAttributes<CatalogModel>, InferCreationAttributes<CatalogModel>> {}
|
||||
|
||||
const catalogAssets = db.define<CatalogModel>("catalog_assets", {
|
||||
package_name: { type: DataTypes.STRING, unique: true },
|
||||
|
@ -58,32 +64,32 @@ const catalogAssets = db.define<CatalogModel>("catalog_assets", {
|
|||
});
|
||||
|
||||
await app.register(fastifyCompress, {
|
||||
encodings: ['br', 'gzip', 'deflate']
|
||||
encodings: ["br", "gzip", "deflate"]
|
||||
});
|
||||
|
||||
await app.register(fastifyMultipart);
|
||||
|
||||
await app.register(fastifyStatic, {
|
||||
root: fileURLToPath(new URL('../dist/client', import.meta.url)),
|
||||
decorateReply: false,
|
||||
root: fileURLToPath(new URL("../dist/client", import.meta.url)),
|
||||
decorateReply: false
|
||||
});
|
||||
|
||||
await app.register(fastifyStatic, {
|
||||
root: fileURLToPath(new URL('../database_assets', import.meta.url)),
|
||||
prefix: '/packages/',
|
||||
root: fileURLToPath(new URL("../database_assets", import.meta.url)),
|
||||
prefix: "/packages/",
|
||||
decorateReply: false
|
||||
});
|
||||
|
||||
await app.register(fastifyMiddie);
|
||||
|
||||
app.get("/api", (request, reply) => {
|
||||
reply.send({ Server: 'Active' });
|
||||
reply.send({ Server: "Active" });
|
||||
});
|
||||
|
||||
// This API returns a list of the assets in the database (SW plugins and themes).
|
||||
// It also returns the number of pages in the database.
|
||||
// It can take a `?page=x` argument to display a different page, with a limit of 20 assets per page.
|
||||
type CatalogAssetsReq = FastifyRequest<{Querystring: { page: string } }>
|
||||
type CatalogAssetsReq = FastifyRequest<{ Querystring: { page: string } }>;
|
||||
app.get("/api/catalog-assets/", async (request: CatalogAssetsReq, reply) => {
|
||||
try {
|
||||
const { page } = request.query;
|
||||
|
@ -110,70 +116,73 @@ app.get("/api/catalog-assets/", async (request: CatalogAssetsReq, reply) => {
|
|||
return acc;
|
||||
}, {});
|
||||
reply.send({ assets, pages: Math.ceil(totalItems / 20) });
|
||||
}
|
||||
catch (error) {
|
||||
reply.status(500).send({ error: 'An error occured' });
|
||||
} catch (error) {
|
||||
reply.status(500).send({ error: "An error occured" });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
type PackageReq = FastifyRequest<{Params: { package: string } }>
|
||||
type PackageReq = FastifyRequest<{ Params: { package: string } }>;
|
||||
app.get("/api/packages/:package", async (request: PackageReq, reply) => {
|
||||
try {
|
||||
const packageRow = await catalogAssets.findOne({ where: { package_name: request.params.package }});
|
||||
if (!packageRow) return reply.status(404).send({ error: 'Package not found!' });
|
||||
const packageRow = await catalogAssets.findOne({
|
||||
where: { package_name: request.params.package }
|
||||
});
|
||||
if (!packageRow) return reply.status(404).send({ error: "Package not found!" });
|
||||
const details = {
|
||||
title: packageRow.get("title"),
|
||||
description: packageRow.get('description'),
|
||||
image: packageRow.get('image'),
|
||||
author: packageRow.get('author'),
|
||||
tags: packageRow.get('tags'),
|
||||
version: packageRow.get('version'),
|
||||
background_image: packageRow.get('background_image'),
|
||||
background_video: packageRow.get('background_video'),
|
||||
payload: packageRow.get('payload'),
|
||||
type: packageRow.get('type')
|
||||
description: packageRow.get("description"),
|
||||
image: packageRow.get("image"),
|
||||
author: packageRow.get("author"),
|
||||
tags: packageRow.get("tags"),
|
||||
version: packageRow.get("version"),
|
||||
background_image: packageRow.get("background_image"),
|
||||
background_video: packageRow.get("background_video"),
|
||||
payload: packageRow.get("payload"),
|
||||
type: packageRow.get("type")
|
||||
};
|
||||
reply.send(details);
|
||||
}
|
||||
catch (error) {
|
||||
reply.status(500).send({ error: 'An unexpected error occured' });
|
||||
} catch (error) {
|
||||
reply.status(500).send({ error: "An unexpected error occured" });
|
||||
}
|
||||
});
|
||||
|
||||
type UploadReq = FastifyRequest<{Headers: { psk: string, packagename: string }}>;
|
||||
type CreateReq = FastifyRequest<{Headers: { psk: string },
|
||||
Body: {
|
||||
uuid: string,
|
||||
title: string,
|
||||
image: string,
|
||||
author: string,
|
||||
version: string,
|
||||
description: string,
|
||||
tags: object | any,
|
||||
payload: string,
|
||||
background_video: string,
|
||||
background_image: string,
|
||||
type: CatalogType
|
||||
}}>;
|
||||
type UploadReq = FastifyRequest<{ Headers: { psk: string; packagename: string } }>;
|
||||
type CreateReq = FastifyRequest<{
|
||||
Headers: { psk: string };
|
||||
Body: {
|
||||
uuid: string;
|
||||
title: string;
|
||||
image: string;
|
||||
author: string;
|
||||
version: string;
|
||||
description: string;
|
||||
tags: object | any;
|
||||
payload: string;
|
||||
background_video: string;
|
||||
background_image: string;
|
||||
type: CatalogType;
|
||||
};
|
||||
}>;
|
||||
interface VerifyStatus {
|
||||
status: number;
|
||||
error?: Error;
|
||||
}
|
||||
async function verifyReq(request: UploadReq | CreateReq, upload: Boolean, data: any): Promise<VerifyStatus> {
|
||||
async function verifyReq(
|
||||
request: UploadReq | CreateReq,
|
||||
upload: Boolean,
|
||||
data: any
|
||||
): Promise<VerifyStatus> {
|
||||
if (parsedDoc.marketplace.enabled === false) {
|
||||
return {status: 500, error: new Error('Marketplace Is disabled!')};
|
||||
return { status: 500, error: new Error("Marketplace Is disabled!") };
|
||||
} else if (request.headers.psk !== parsedDoc.marketplace.psk) {
|
||||
return { status: 403, error: new Error("PSK isn't correct!") };
|
||||
} else if (upload && !request.headers.packagename) {
|
||||
return { status: 500, error: new Error("No packagename defined!") };
|
||||
} else if (upload && !data) {
|
||||
return { status: 400, error: new Error("No file uploaded!") };
|
||||
} else {
|
||||
return { status: 200 };
|
||||
}
|
||||
else if (request.headers.psk !== parsedDoc.marketplace.psk) {
|
||||
return {status: 403, error: new Error("PSK isn't correct!")};
|
||||
}
|
||||
else if(upload && !request.headers.packagename) {
|
||||
return {status: 500, error: new Error('No packagename defined!')};
|
||||
}
|
||||
else if (upload && !data) {
|
||||
return {status: 400, error: new Error('No file uploaded!')};
|
||||
}
|
||||
else { return {status: 200 }; }
|
||||
}
|
||||
|
||||
app.post("/api/upload-asset", async (request: UploadReq, reply) => {
|
||||
|
@ -181,15 +190,25 @@ app.post("/api/upload-asset", async (request: UploadReq, reply) => {
|
|||
const verify: VerifyStatus = await verifyReq(request, true, data);
|
||||
if (verify.error !== undefined) {
|
||||
reply.status(verify.status).send({ status: verify.error.message });
|
||||
}
|
||||
else {
|
||||
try {
|
||||
await pipeline(data.file, createWriteStream(fileURLToPath(new URL(`../database_assets/${request.headers.packagename}/${data.filename}`, import.meta.url))));
|
||||
} else {
|
||||
try {
|
||||
await pipeline(
|
||||
data.file,
|
||||
createWriteStream(
|
||||
fileURLToPath(
|
||||
new URL(
|
||||
`../database_assets/${request.headers.packagename}/${data.filename}`,
|
||||
import.meta.url
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
} catch (error) {
|
||||
return reply
|
||||
.status(500)
|
||||
.send({ status: `File couldn't be uploaded! (Package most likely doesn't exist)` });
|
||||
}
|
||||
catch (error) {
|
||||
return reply.status(500).send({ status: `File couldn't be uploaded! (Package most likely doesn't exist)` });
|
||||
}
|
||||
return reply.status(verify.status).send({ status: 'File uploaded successfully!' });
|
||||
return reply.status(verify.status).send({ status: "File uploaded successfully!" });
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -197,9 +216,8 @@ app.post("/api/create-package", async (request: CreateReq, reply) => {
|
|||
const verify: VerifyStatus = await verifyReq(request, false, undefined);
|
||||
if (verify.error !== undefined) {
|
||||
reply.status(verify.status).send({ status: verify.error.message });
|
||||
}
|
||||
else {
|
||||
const body: Catalog = {
|
||||
} else {
|
||||
const body: Catalog = {
|
||||
package_name: request.body.uuid,
|
||||
title: request.body.title,
|
||||
image: request.body.image,
|
||||
|
@ -211,8 +229,8 @@ app.post("/api/create-package", async (request: CreateReq, reply) => {
|
|||
background_video: request.body.background_video,
|
||||
background_image: request.body.background_image,
|
||||
type: request.body.type as CatalogType
|
||||
}
|
||||
await catalogAssets.create({
|
||||
};
|
||||
await catalogAssets.create({
|
||||
package_name: body.package_name,
|
||||
title: body.title,
|
||||
image: body.image,
|
||||
|
@ -225,40 +243,47 @@ app.post("/api/create-package", async (request: CreateReq, reply) => {
|
|||
background_image: body.background_image,
|
||||
type: body.type
|
||||
});
|
||||
const assets = fileURLToPath(new URL('../database_assets', import.meta.url));
|
||||
const assets = fileURLToPath(new URL("../database_assets", import.meta.url));
|
||||
try {
|
||||
await access(`${assets}/${body.package_name}/`, constants.F_OK);
|
||||
return reply.status(500).send({ status: 'Package already exists!' });
|
||||
}
|
||||
catch (err) {
|
||||
return reply.status(500).send({ status: "Package already exists!" });
|
||||
} catch (err) {
|
||||
await mkdir(`${assets}/${body.package_name}/`);
|
||||
return reply.status(verify.status).send({ status: 'Package created successfully!' });
|
||||
return reply.status(verify.status).send({ status: "Package created successfully!" });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
app.use(ssrHandler);
|
||||
|
||||
const port: number = parseInt(process.env.PORT as string) || parsedDoc.server.server.port || parseInt('8080');
|
||||
const port: number =
|
||||
parseInt(process.env.PORT as string) || parsedDoc.server.server.port || parseInt("8080");
|
||||
const titleText = `
|
||||
_ _ _ _ ____ _
|
||||
| \\ | | ___| |__ _ _| | __ _ / ___| ___ _ ____ _(_) ___ ___ ___
|
||||
| \\| |/ _ \\ '_ \\| | | | |/ _' | \\___ \\ / _ \\ '__\\ \\ / / |/ __/ _ \\/ __|
|
||||
| |\\ | __/ |_) | |_| | | (_| | ___) | __/ | \\ V /| | (_| __/\\__ \\
|
||||
|_| \\_|\\___|_.__/ \\__,_|_|\\__,_| |____/ \\___|_| \\_/ |_|\\___\\___||___/
|
||||
`
|
||||
`;
|
||||
const titleColors = {
|
||||
purple: "#7967dd",
|
||||
pink: "#eb6f92"
|
||||
};
|
||||
|
||||
|
||||
console.log(gradient(Object.values(titleColors)).multiline(titleText as string));
|
||||
app.listen({ port: port, host: '0.0.0.0' }).then(async () => {
|
||||
console.log(chalk.hex('#7967dd')(`Server listening on ${chalk.hex('#eb6f92').bold('http://localhost:' + port + '/')}`));
|
||||
console.log(chalk.hex('#7967dd')(`Server also listening on ${chalk.hex('#eb6f92').bold('http://0.0.0.0:' + port + '/')}`));
|
||||
await catalogAssets.sync()
|
||||
app.listen({ port: port, host: "0.0.0.0" }).then(async () => {
|
||||
console.log(
|
||||
chalk.hex("#7967dd")(
|
||||
`Server listening on ${chalk.hex("#eb6f92").bold("http://localhost:" + port + "/")}`
|
||||
)
|
||||
);
|
||||
console.log(
|
||||
chalk.hex("#7967dd")(
|
||||
`Server also listening on ${chalk.hex("#eb6f92").bold("http://0.0.0.0:" + port + "/")}`
|
||||
)
|
||||
);
|
||||
await catalogAssets.sync();
|
||||
await setupDB(catalogAssets);
|
||||
});
|
||||
|
||||
export { CatalogModel, Catalog }
|
||||
export { CatalogModel, Catalog };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue