@webda/shell
Version:
Deploy a Webda app or configure it
120 lines • 3.86 kB
JavaScript
import { Application, CoreModel, SectionEnum, Service } from "@webda/core";
import * as path from "path";
import { Deployment } from "../models/deployment.js";
import { WebdaServer } from "./http.js";
/**
* @WebdaModda webdashell/configuration
*/
export default class ConfigurationService extends Service {
async checkRequest(context) {
if (context.getHttpContext().getHeader("origin") === "localhost:18181") {
return true;
}
return false;
}
resolve() {
super.resolve();
this._webda.registerCORSFilter(this);
this._webda.registerRequestFilter(this);
this.addRoute("/configuration", ["GET", "PUT"], this.crudConfiguration);
this.addRoute("/application", ["GET"], this.getApplication);
this.addRoute("/npm", ["POST"], this.npmSearch);
this.addRoute("/npm/search", ["POST"], this.npmSearch);
this.addRoute("/webda", ["GET"], this.getWebdaVersions);
this.addRoute("/openapi", ["GET"], this.crudConfiguration);
return this;
}
async init() {
await super.init();
this.webdaApplication = this._webda.getWebdaApplication();
return this;
}
async crudConfiguration(ctx) {
if (ctx.getHttpContext().getMethod() === "GET") {
ctx.write(this.webdaApplication.getConfiguration());
}
else if (ctx.getHttpContext().getMethod() === "PUT") {
ctx.write("Will write webda.config.json");
}
}
/**
* Return webda Core and Shell version
*
* @param ctx
*/
async getWebdaVersions(ctx) {
ctx.write({
Core: this._webda.getApplication(),
Shell: this.webdaApplication.getPackageDescription().version
});
}
async npmSearch(ctx) {
ctx.write({});
}
async getApplication(ctx) {
ctx.write(this.webdaApplication);
}
}
export { ConfigurationService };
export class ConfigApplication extends Application {
getModel(model) {
if (model.toLowerCase() === "webdaconfiguration/deployment") {
return Deployment;
}
else if (model.toLowerCase() === "webda/coremodel") {
return CoreModel;
}
return super.getModel(model);
}
getModda(name) {
if (name.toLowerCase() === "webdaconfiguration/api") {
return ConfigurationService;
}
return super.getModda(name);
}
constructor(application) {
super(application.getAppPath());
Object.values(SectionEnum).forEach(section => {
this[section] = application[section];
});
}
getConfiguration(_deploymentName = undefined) {
return {
version: 3,
parameters: {},
services: {
api: {
type: "WebdaConfiguration/API"
},
logger: {
type: "Webda/FileLogger",
logLevel: "INFO",
file: "/tmp/.webda.config.log"
},
deployments: {
type: "Webda/FileStore",
folder: path.join(this.getAppPath(), "deployments"),
model: "WebdaConfiguration/Deployment",
lastUpdate: false,
beautify: " ",
expose: {
url: "/deployments"
}
}
}
};
}
}
export class WebdaConfiguration extends WebdaServer {
constructor(application) {
super(new ConfigApplication(application));
this.webdaApplication = application;
}
getWebdaApplication() {
return this.webdaApplication;
}
async serve() {
return super.serve(18181);
}
}
//# sourceMappingURL=config.js.map