UNPKG

axiodb

Version:

A blazing-fast, lightweight, and scalable nodejs package based DBMS for modern application. Supports schemas, encryption, and advanced query capabilities.

81 lines 4.04 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = createAxioDBControlServer; /* eslint-disable @typescript-eslint/no-unused-vars */ const fastify_1 = __importDefault(require("fastify")); const cors_1 = __importDefault(require("@fastify/cors")); const static_1 = __importDefault(require("@fastify/static")); const path_1 = __importDefault(require("path")); const fs_1 = __importDefault(require("fs")); const keys_1 = require("./keys"); const PortFreeChecker_1 = __importDefault(require("./PortFreeChecker")); const Router_1 = __importDefault(require("../router/Router")); function createAxioDBControlServer(AxioDBInstance) { return __awaiter(this, void 0, void 0, function* () { yield (0, PortFreeChecker_1.default)(keys_1.ServerKeys.PORT); const AxioDBControlServer = (0, fastify_1.default)({ logger: false, // Disable default logging trustProxy: true, // Trust the reverse proxy headers bodyLimit: 52428800, // Set body limit to 50MB }); // Attach Middlewares yield AxioDBControlServer.register(cors_1.default, { origin: keys_1.CORS_CONFIG.ORIGIN, // Allow all origins methods: keys_1.CORS_CONFIG.METHODS, // Allow specific methods allowedHeaders: keys_1.CORS_CONFIG.ALLOWED_HEADERS, // Allow specific headers credentials: keys_1.CORS_CONFIG.ALLOW_CREDENTIALS, // Allow credentials exposedHeaders: keys_1.CORS_CONFIG.EXPOSED_HEADERS, // Expose specific headers maxAge: keys_1.CORS_CONFIG.MAX_AGE, // Cache preflight response for 24 hours }); // Configure JSON parsing AxioDBControlServer.addContentTypeParser("application/json", { parseAs: "string", bodyLimit: 52428800 }, (req, body, done) => { try { const json = JSON.parse(body); done(null, json); } catch (err) { done(err, undefined); } }); // Serve static files first (JS, CSS, images) yield AxioDBControlServer.register(static_1.default, { root: keys_1.staticPath, prefix: "/", decorateReply: false, }); // Serve React app for all other routes as SPA fallback AxioDBControlServer.get("/", (request, reply) => __awaiter(this, void 0, void 0, function* () { const indexPath = path_1.default.join(keys_1.staticPath, "index.html"); const stream = fs_1.default.createReadStream(indexPath); return reply.type("text/html").send(stream); })); // Register the main router with /api prefix AxioDBControlServer.register(Router_1.default, { prefix: "/api", }); try { yield AxioDBControlServer.listen({ port: Number(keys_1.ServerKeys.PORT), host: "0.0.0.0", }); console.log(`AxioDB Control Server is running on http://localhost:${keys_1.ServerKeys.PORT}`); } catch (err) { AxioDBControlServer.log.error(err); process.exit(1); } }); } //# sourceMappingURL=server.js.map