Skip to content

Commit

Permalink
Merge pull request #34 from ogavb/dynamicOptionsAlignment4
Browse files Browse the repository at this point in the history
Dynamic options alignment4
  • Loading branch information
stefanogermano committed May 26, 2020
2 parents 40becbf + 59ae268 commit dc98dc6
Show file tree
Hide file tree
Showing 4 changed files with 342 additions and 1 deletion.
65 changes: 65 additions & 0 deletions app.js
Expand Up @@ -6,6 +6,7 @@ var forceSSL = require('express-force-ssl');
var webSocket = require('websocket').w3cwebsocket;
var fs = require('fs');
var pug = require('pug');
var jpointer = require('json-pointer');
const compression = require('compression');

// System config loading
Expand All @@ -19,6 +20,10 @@ var maxAge = properties.max_age;
// Services configuration file
var servicesConfig = require('./config/services.json');

// This function validates the JSON schemas
var Ajv = require('ajv');
validateJsonSchemas();

var pckg = require('./package.json');

var app = express();
Expand Down Expand Up @@ -146,3 +151,63 @@ function getExcecutorURL(data) {
}
}
}

function validateJsonSchemas() {
// Validate JSON file with the relative scheme
var servicesValidation = validateSchema('./config/services.json', './config/services-schema.json');
var appConfigValidation = validateSchema('./config/app-config.json', './config/app-config-schema.json');

if(servicesValidation.criticalError || appConfigValidation.criticalError) {
console.log('Fatal error: configuration files are not setted up properly!');
process.exit(1);
}
}

function validateSchema(jsonPath, schemaPath) {
// Loading files
var json = require(jsonPath);
var schema = require(schemaPath);

// Config
var ajv = new Ajv({
allErrors: true,
jsonPointers: true
});

// Compiling the schema
var compiledSchema = ajv.compile(schema);
var validated = false;
var printError = true;
var response = { };

while(!validated) {
// Validating
var validatedJson = compiledSchema(json);
// If some there is some error, the nearest parent object in the file, containing this error, is deleted
if (!validatedJson) {
// Prints the errors only the first time
if(printError) {
console.log(compiledSchema.errors);
printError = false;
}

for(var index in compiledSchema.errors) {
var path = compiledSchema.errors[index].dataPath;
if(path === '') {
// 'This' case happen when there is a problem in to the root of the json file (eg. when the file is empty)
console.log('Fatal error: ' + jsonPath + ' is not setted up properly!');
response.criticalError = true;
validated = true;
} else {
jpointer.remove(json, path);
}
}
}
else {
console.log('Validated: ' + jsonPath);
validated = true;
}
}

return response;
}
71 changes: 71 additions & 0 deletions config/app-config-schema.json
@@ -0,0 +1,71 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "app-config-schema.json",
"self": {
"vendor": "unical",
"name": "app-config",
"format": "jsonschema",
"version": "1-0-0"
},
"type": "object",
"properties": {
"max_age": {
"title": "HSTS max age",
"description": "This property specifies HTTP Strict Transport Security max age.",
"type": "number",
"minimum": 0
},
"port": {
"title": "Ports configuration object",
"description": "This object contains information about ports to be used for the various supported protocols.",
"type": "object",
"properties": {
"http": {
"title": "HTTP port",
"description": "This property specifies the port to use for HTTP connections.",
"type": "number",
"minimum": 0,
"maximum": 65535
},
"https": {
"title": "HTTPS port",
"description": "This property specifies the port to use for HTTPS connections.",
"type": "number",
"minimum": 0,
"maximum": 65535
}
},
"required": [
"http"
],
"additionalProperties": false
},
"path": {
"title": "Paths configuration object",
"description": "This object contains information about paths to be used for the various configurations.",
"type": "object",
"properties": {
"key": {
"title": "HTTPS key",
"description": "This property specifies the path for the HTTPS key.",
"type": "string"
},
"cert": {
"title": "HTTPS certificate",
"description": "This property specifies the path for the HTTPS certificate.",
"type": "string"
}
},
"required": [
"key",
"cert"
],
"additionalProperties": false
}
},
"required": [
"port",
"max_age"
],
"additionalProperties": false
}
203 changes: 203 additions & 0 deletions config/services-schema.json
@@ -0,0 +1,203 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "services-schema.json",
"self": {
"vendor": "unical",
"name": "services",
"format": "jsonschema",
"version": "1-0-0"
},
"properties": {
"languages": {
"title": "Supported languages.",
"description": "This property indicates all the supported languages by the services avalable on the LoIDE Web Application.",
"type": "array",
"minItems": 1,
"items": {
"$ref": "#/definitions/language"
}
}
},
"required": [
"languages"
],
"additionalProperties": false,
"definitions": {
"language": {
"type": "object",
"properties": {
"name": {
"title": "Language name.",
"description": "This property indicates the language name.",
"type": "string"
},
"value": {
"title": "Language value.",
"description": "This property indicates the language value used in communication with other services.",
"type": "string"
},
"syntax-highlighter": {
"title": "Syntax-Highlighter path.",
"description": "This property indicates the syntax highlighter path corresponding to this language.",
"type": "object",
"properties": {
"name": {
"title": "Syntax-Highlighter name.",
"description": "This property indicates the syntax-highlighter name.",
"type": "string"
},
"description": {
"title": "Syntax-Highlighter description.",
"description": "This property indicates the syntax-highlighter description.",
"type": "string"
},
"path": {
"title": "Syntax-Highlighter path.",
"description": "This property indicates the syntax-highlighter file path.",
"type": "string"
}
},
"required": [
"name",
"path"
],
"additionalProperties": false
},
"solvers": {
"title": "Supporting solvers.",
"description": "This property indicates all supporting solvers for this language.",
"type": "array",
"minItems": 1,
"items": {
"$ref": "#/definitions/solver"
}
}
},
"required": [
"name",
"value",
"solvers"
],
"additionalProperties": false
},
"solver": {
"type": "object",
"properties": {
"name": {
"title": "Solver name.",
"description": "This property indicates the solver name.",
"type": "string"
},
"value": {
"title": "Solver value.",
"description": "This property indicates the solver value used in communication with other services.",
"type": "string"
},
"executors": {
"title": "Supporting executors.",
"description": "This property indicates all supporting executors for this solver.",
"type": "array",
"minItems": 1,
"items": {
"$ref": "#/definitions/executor"
}
},
"options": {
"title": "Solver option",
"description": "This property indicates the options supported by the solver.",
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"name": {
"title": "Option name.",
"description": "This property indicates the option name.",
"type": "string"
},
"value": {
"title": "Option value.",
"description": "This property indicates the command line argument passed to the solver.",
"type": "string"
},
"word_argument": {
"title": "Word argument.",
"description": "This property indicates if this type of argument requires the second argument or it's just a word argument (this is just a flag).",
"type": "boolean"
},
"description": {
"title": "Option description.",
"description": "This property contains the description of the option.",
"type": "string"
}
},
"required": [
"name",
"value",
"word_argument",
"description"
],
"additionalProperties": false
}
}
},
"required": [
"name",
"value",
"executors",
"options"
],
"additionalProperties": false
},
"executor": {
"type": "object",
"properties": {
"protocol": {
"title": "Protocol name.",
"description": "This property indicates the protocol name to use for a connection with this Executor.",
"enum": [ "ws", "http" ]
},
"url": {
"title": "Executor URL.",
"description": "This property indicates the executor URL to connect with it.",
"type": "string",
"oneOf": [
{ "format": "hostname" },
{ "format": "ipv4" },
{ "format": "ipv6" }
]
},
"name": {
"title": "Executor name.",
"description": "This property indicates the executor name.",
"type": "string"
},
"value": {
"title": "Executor value.",
"description": "This property indicates the option value.",
"type": "string"
},
"path": {
"title": "Executor path.",
"description": "This property indicates the executor path relative to its URL.",
"type": "string",
"_comment": "Add the pattern to match with the 'path'."
},
"port": {
"title": "Connection port number.",
"description": "This property indicates the port number open on this executor for the incoming connections.",
"type": "number",
"minimum": 0,
"maximum": 65535
}
},
"required": [
"protocol",
"url",
"path",
"port"
],
"additionalProperties": false
}
}
}
4 changes: 3 additions & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "LoIDE",
"version": "2.6.1",
"version": "2.7.0",
"description": "Web-based IDE for Logic Programming",
"main": "app.js",
"scripts": {
Expand Down Expand Up @@ -38,9 +38,11 @@
"url": "https://github.com/DeMaCS-UNICAL/LoIDE/issues"
},
"dependencies": {
"ajv": "^6.12.2",
"express": "^4.17.1",
"express-force-ssl": "^0.3.2",
"helmet": "^3.21.1",
"json-pointer": "^0.6.0",
"pug": "^2.0.4",
"socket.io": "^2.3.0",
"websocket": "^1.0.30"
Expand Down

0 comments on commit dc98dc6

Please sign in to comment.