shaman-website-compiler
Version:
Compile raw HTML, CSS and Javascript into the smallest possible, SEO friendly website.
85 lines • 4.14 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebsiteServer = void 0;
var _http = require("http");
var _fs = require("fs");
var inversify_1 = require("inversify");
var app_composition_1 = require("./composition/app.composition");
var logger_1 = require("./logger");
var file_functions_1 = require("./functions/file.functions");
var WebsiteServer = /** @class */ (function () {
function WebsiteServer() {
var _this = this;
this._listening = false;
this.start = function (routes) {
_this.updateRoutes(routes);
_this._listening = true;
var port = _this.config.port;
_http.createServer(_this.handleRequest).listen(port);
_this.logger.log("Development server listening on port: ".concat(port), logger_1.LogLevels.info);
};
this.updateRoutes = function (routes) {
_this.routes = routes;
};
this.handleRequest = function (req, res) {
var path = (req.url == "/" ? "/index.html" : req.url).slice(1);
if (path.includes('?'))
path = path.substring(0, path.indexOf('?'));
if (_this.isAssetRoute(path))
return _this.handleAssetRequest(path, res);
var route = _this.routes.find(function (r) { return r.path == path; });
if (!route) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
_this.logger.log("Route not found: ".concat(path), logger_1.LogLevels.warn);
}
else {
res.writeHead(200, { 'Content-Type': route.mimeType });
_this.logger.log("Route found: ".concat(path), logger_1.LogLevels.info);
res.write(route.content);
}
res.end();
};
this.handleAssetRequest = function (path, res) {
var route = _this.context.models.assets.find(path);
if (!route) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
_this.logger.log("Route not found: ".concat(path), logger_1.LogLevels.warn);
res.end();
return;
}
res.writeHead(200, { 'Content-Type': (0, file_functions_1.GetFileMimeType)(route.extension) });
var stream = _fs.createReadStream(route.path);
stream.pipe(res);
};
this.isAssetRoute = function (path) {
if (path == 'sitemap.xml')
return false;
var extension = (0, file_functions_1.GetFileExtension)(path);
return ['', 'js', 'css', 'html'].indexOf(extension) == -1;
};
this.logger = app_composition_1.IoC.get(app_composition_1.TYPES.Logger);
this.config = app_composition_1.IoC.get(app_composition_1.TYPES.WebsiteConfig);
this.context = app_composition_1.IoC.get(app_composition_1.TYPES.CompilerDataContext);
}
Object.defineProperty(WebsiteServer.prototype, "listening", {
get: function () { return this._listening; },
enumerable: false,
configurable: true
});
WebsiteServer = __decorate([
(0, inversify_1.injectable)(),
__metadata("design:paramtypes", [])
], WebsiteServer);
return WebsiteServer;
}());
exports.WebsiteServer = WebsiteServer;
//# sourceMappingURL=website-server.js.map