feat(domains) implement domain routing; Logger
This commit is contained in:
@@ -1,21 +1,24 @@
|
||||
import { readdir } from 'fs/promises';
|
||||
import { resolve as resolvePath } from 'path';
|
||||
import Router from './Routing/Router';
|
||||
|
||||
import LayRouting from './Routing/LayRouting';
|
||||
import HtmlEngine from './Views/Html';
|
||||
import DefaultLoader from './Views/DefaultLoader';
|
||||
import { readdir } from 'fs/promises';
|
||||
import BaseRouter from './Routing/BaseRouter';
|
||||
|
||||
import Logger from './Utils/Logger';
|
||||
|
||||
/**
|
||||
* Represents a LayServer instance that handles routing and server configuration.
|
||||
* @todo add multi domain support
|
||||
* @class
|
||||
*/
|
||||
export default class LayServer {
|
||||
export default class LayServer extends BaseRouter {
|
||||
controllers = new Map();
|
||||
loaders = new Map();
|
||||
proc = null;
|
||||
router;
|
||||
publicFiles;
|
||||
routing = null;
|
||||
options = new Map([
|
||||
['loaders', undefined],
|
||||
['publicDir', undefined],
|
||||
@@ -24,6 +27,7 @@ export default class LayServer {
|
||||
['discoverPublicFiles', false],
|
||||
['enforceTrailingSlash', false],
|
||||
['laycLoading', false],
|
||||
['allowRouteOverwrite', true],
|
||||
]);
|
||||
|
||||
/**
|
||||
@@ -31,6 +35,7 @@ export default class LayServer {
|
||||
* @param {Object} options - The options for configuring the LayServer.
|
||||
*/
|
||||
constructor(options = {}) {
|
||||
super();
|
||||
if (typeof options == 'object') {
|
||||
const optionKeys = Object.getOwnPropertyNames(options);
|
||||
|
||||
@@ -53,7 +58,11 @@ export default class LayServer {
|
||||
}
|
||||
}
|
||||
|
||||
this.initRouting(options.router);
|
||||
this.routing = new LayRouting(this);
|
||||
|
||||
if(options.router) {
|
||||
this.routing.addRouter(options.router);
|
||||
}
|
||||
|
||||
if (options.hasOwnProperty('controllers')) {
|
||||
this.registerControllers(options.controllers);
|
||||
@@ -110,19 +119,10 @@ export default class LayServer {
|
||||
* @param {string} publicDir - The directory where the public files are stored.
|
||||
*/
|
||||
indexPublicFiles(publicDir) {
|
||||
Logger.error('@todo implement method `indexPublicFiles`')
|
||||
// this method is meant to try to iterate over all public files which are stored as static routes
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the routing for the LayServer.
|
||||
* @param {Function} [router=null] - The router class to be used for routing.
|
||||
*/
|
||||
initRouting(router = null) {
|
||||
if (router) this.router = new router(this);
|
||||
if (typeof this.router == 'undefined') this.router = new Router(this);
|
||||
this.routeControl = new LayRouting(this.router);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the given controllers.
|
||||
*
|
||||
@@ -133,13 +133,13 @@ export default class LayServer {
|
||||
|
||||
for (let controller of controllers) {
|
||||
if (typeof controller != 'function') {
|
||||
console.log(`ignored ${controller} for not being a function`);
|
||||
Logger(`ignored ${controller} for not being a function`);
|
||||
continue;
|
||||
}
|
||||
|
||||
// if the given controller is just an anonymous function defining the routes itself
|
||||
if (!controller?.prototype?.constructor?.name) {
|
||||
controller(this.routeControl);
|
||||
controller(this.routing);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -156,9 +156,9 @@ export default class LayServer {
|
||||
* @returns {Object} - The instantiated controller.
|
||||
*/
|
||||
instantiateController(controller) {
|
||||
this.routeControl.setNamespace(controller.namespace);
|
||||
const _temp = new controller(this.routeControl);
|
||||
if (controller.namespace) this.routeControl.setNamespace(null);
|
||||
this.routing.setNamespace(controller.namespace);
|
||||
const _temp = new controller(this.routing);
|
||||
if (controller.namespace) this.routing.setNamespace(null);
|
||||
return _temp;
|
||||
}
|
||||
|
||||
@@ -199,7 +199,7 @@ export default class LayServer {
|
||||
)
|
||||
);
|
||||
} catch (err) {
|
||||
console.log(err, import.meta.dir, 'public', path);
|
||||
Logger(err, import.meta.dir, 'public', path);
|
||||
return new Response('Internal Server Error', {
|
||||
status: 500,
|
||||
});
|
||||
@@ -211,11 +211,9 @@ export default class LayServer {
|
||||
return Response.redirect(`${url.toString()}/`, 302);
|
||||
}
|
||||
|
||||
let resp = await this.router.handle(req);
|
||||
|
||||
if (!resp) return new Response('Not Found', { status: 404 });
|
||||
|
||||
return resp;
|
||||
let resp = await this.routing.handle(req);
|
||||
if (resp instanceof Response) return resp;
|
||||
return new Response('Not Found', { status: 404 });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -229,7 +227,7 @@ export default class LayServer {
|
||||
development: Bun.env.BUN_ENV != 'production',
|
||||
fetch: this.handleRequest.bind(this),
|
||||
error(err) {
|
||||
console.log('where', err.stack);
|
||||
Logger('where', err.stack);
|
||||
return new Response(`<pre>${err}\n${err.stack}</pre>`, {
|
||||
headers: {
|
||||
'Content-Type': 'text/html',
|
||||
@@ -246,8 +244,7 @@ export default class LayServer {
|
||||
cb(this.proc);
|
||||
}
|
||||
} catch (err) {
|
||||
console.log('where', err.stack);
|
||||
console.error(err);
|
||||
Logger.error('where', err.stack);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -267,7 +264,7 @@ export default class LayServer {
|
||||
|
||||
const files = await readdir(path);
|
||||
for (let file of files) {
|
||||
console.log(
|
||||
Logger(
|
||||
file,
|
||||
file.substring(0, file.indexOf('.')),
|
||||
filename.substring(0, filename.indexOf('.')) || filename
|
||||
@@ -304,4 +301,8 @@ export default class LayServer {
|
||||
|
||||
return new DefaultLoader(filepath);
|
||||
}
|
||||
|
||||
async add(path, cb) {
|
||||
this.routing.add(...arguments)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user