diff --git a/.gitignore b/.gitignore index 8000dd9db..cb09301a9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ .vagrant + +node_modules/ diff --git a/README.md b/README.md index ea2ccacea..d691e35b9 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ cd /path-to-jscity-directory/js ``` * Start the application server using the command. ```sh -node server.js +npm start ``` * Using your browser, access the url below to open the jscity system: ``` @@ -41,6 +41,53 @@ http://localhost:8888/ ``` * Select the system from the combobox and wait for the end of city design. +### Docker setup + +If you would like it as a docker + +``` +docker-compose up jscity +``` + +then give it 15 seconds or so and `http://localhost:8888` + +There are several docker-compose services + +* `database` - just a stock mysql database so you don't have to install anything +* `database-migration` - run database migration script to create and seed the jscity database +* `jscity` - the web server itself +* `generator` - used to run the city generator. Typically you would run this with the `run` and not the `up` command. + +#### Generate City + +Note that this isn't as easy as running `run-generate.sh` - this script is meant to be run *inside the docker container* not outside of it. + +The code to be scanned needs to be somewhere accessible to the generator docker container. You may mount additional volumes or simply place it in the cannonical location `js/backend/system/`. + +If you were to simply `docker-compose run` the application it will start with a new instance of the `database` service so that your changes will be created and immediately discarded. To bypass that limitation first start the database and run any migrations (if this is not your first time you can just up `database`) + +``` +docker-compose up -d database-migration +``` +and then run without creating additional dependencies. + +Note that int he following examples we're just creating further versions of the jscity sample city + +``` +docker-compose run --rm --no-deps generator ./js/backend/system/metafora/ -c "Metafora Sample" +``` + +You may also append `--verbose` for more verbose output on the generation process. + +##### Debugging + +To debug `generator.js` you can pass in a `--debug` flag and don't forget to map port 9229 + +``` +docker-compose run --rm -p 9229:9229 --no-deps generator ./js/backend/system/metafora/ -c "Metafora" --verbose --debug +``` + +This will start the script with the debugger and break on the very first line. You can navigate to `chrome://inspect/#devices` in Chrome to attach the debugger there. ### Vagrant setup diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 000000000..f6742beeb --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,40 @@ +version: '3.6' +services: + jscity: + image: node:13.0.1-stretch-slim + ports: + - '8888:8888' + - '9234:9234' # node debug port + volumes: + - '.:/app' + depends_on: + - database + - database-migration + working_dir: /app + command: 'npm start' + database: + image: mysql:8.0.18 + ports: + - '3306:3306' + environment: + MYSQL_ALLOW_EMPTY_PASSWORD: 'yes' + database-migration: + image: mysql:8.0.18 + depends_on: + - database + volumes: + - './sql:/sql' + environment: + CREATE_TEST_CITY: 1 + command: 'bash -c "sleep 15 && mysql --host=database < ./sql/schema.sql"' #sleep to give the db time to come up + generator: + image: node:13.0.1-stretch-slim + ports: + - '9229:9229' # node debug port + volumes: + - '.:/app' + depends_on: + - database + - database-migration + working_dir: /app + entrypoint: './run-generator.sh' diff --git a/js/config.json b/js/config.json index 617559de8..767fcc080 100644 --- a/js/config.json +++ b/js/config.json @@ -12,9 +12,9 @@ "local": { "connectionLimit": 200, "waitForConnections": true, - "host": "localhost", + "host": "database", "user": "jscity", - "password": "", + "password": "password", "database": "jscity" }, "amazon": { diff --git a/js/server.js b/js/server.js index 4da005130..719eea4bf 100644 --- a/js/server.js +++ b/js/server.js @@ -248,6 +248,7 @@ sSQL = mysql.format(sSQL, format); connection.query(sSQL, function(err, rows, fields) { if(err) { + console.log(err) responseObj.message = msg('Internal Error. {FCBD}', err.message); writeEnd(JSON.stringify(responseObj), "application/json", 500); } else { diff --git a/package.json b/package.json index 1a492d716..9c49cf962 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,6 @@ "node": ">= 0.12" }, "scripts": { - "start": "node ./js/server.js" + "start": "node --inspect=0.0.0.0:9234 ./js/server.js" } } diff --git a/provision_script.sh b/provision_script.sh index 948bf0be9..18823ad07 100644 --- a/provision_script.sh +++ b/provision_script.sh @@ -1,4 +1,5 @@ -# provision_script.sh +##!/bin/bash + apt-get update debconf-set-selections <<< 'mysql-server mysql-server/root_password password your_password' debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password your_password' diff --git a/run-generator.sh b/run-generator.sh new file mode 100755 index 000000000..19f2e4eed --- /dev/null +++ b/run-generator.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# +# Runs generator.js to parse a city. Meant to be run from inside the docker-compose but if you have the application configured outside of it can work as well. +# When the --debug flag is passed will start the node debugger and idle on the first line + +if [[ $* == *--debug* ]]; then + echo "Starting node debugger on port 9229" + echo "Navigate to chrome://inspect/#devices to connect, if you do not see it there and are running this via docker make sure you remembered to map -p 9229:9229" + node --inspect-brk=0.0.0.0:9229 ./js/backend/generator.js "$@" +else + node ./js/backend/generator.js "$@" +fi diff --git a/sql/schema.sql b/sql/schema.sql index dbd47bf32..348c38bac 100644 --- a/sql/schema.sql +++ b/sql/schema.sql @@ -1,83 +1,80 @@ -/* -SQLyog -MySQL - 5.6.26 : Database - jscity -********************************************************************* -*/ - -/*!40101 SET NAMES utf8 */; - -/*!40101 SET SQL_MODE=''*/; - -/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; -/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; -/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; -/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; -CREATE DATABASE /*!32312 IF NOT EXISTS*/`jscity` /*!40100 DEFAULT CHARACTER SET latin1 */; - -USE `jscity`; - -/*Table structure for table `tb_building` */ - -DROP TABLE IF EXISTS `tb_building`; - -CREATE TABLE `tb_building` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `name` varchar(255) NOT NULL, - `height` int(11) unsigned NOT NULL DEFAULT '0', - `width` int(11) unsigned NOT NULL DEFAULT '0', - `color` varchar(10) NOT NULL, - `tooltip` varchar(255) NOT NULL, - `district_id` int(11) DEFAULT NULL, - `building_id` int(11) DEFAULT NULL, - PRIMARY KEY (`id`), - KEY `idx_fk_tb_construction_tb_district1` (`district_id`), - KEY `idx_fk_tb_building_tb_building1_idx` (`building_id`), - CONSTRAINT `fk_tb_building_tb_building1` FOREIGN KEY (`building_id`) REFERENCES `tb_building` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, - CONSTRAINT `fk_tb_construction_tb_district1` FOREIGN KEY (`district_id`) REFERENCES `tb_district` (`id`) ON DELETE CASCADE ON UPDATE CASCADE -) ENGINE=InnoDB DEFAULT CHARSET=utf8; - -/*Data for the table `tb_building` */ - -insert into `tb_building`(`id`,`name`,`height`,`width`,`color`,`tooltip`,`district_id`,`building_id`) values (1,'A',27,25,'0x337AB7','A, LOC: 27 [28-54]',1,NULL),(2,'Anonymous function',29,5,'0x4CAE4C','Anonymous function, LOC: 29 [56-84]',1,NULL),(3,'C',12,2,'0x337AB7','C, LOC: 12 [57-68]',NULL,2),(4,'B',8,2,'0x337AB7','B, LOC: 8 [70-77]',NULL,2),(5,'C',12,2,'0x337AB7','C, LOC: 12 [28-39]',4,NULL),(6,'B',11,3,'0x337AB7','B, LOC: 11 [41-51]',4,NULL),(7,'Anonymous function',12,2,'0x4CAE4C','Anonymous function, LOC: 12 [59-70]',4,NULL),(8,'Anonymous function',11,3,'0x4CAE4C','Anonymous function, LOC: 11 [72-82]',4,NULL),(9,'C',12,2,'0x337AB7','C, LOC: 12 [28-39]',5,NULL),(10,'B',11,3,'0x337AB7','B, LOC: 11 [41-51]',5,NULL),(11,'C',75,2,'0x337AB7','C, LOC: 75 [28-102]',6,NULL),(12,'B',11,3,'0x337AB7','B, LOC: 11 [104-114]',6,NULL),(13,'A',3,1,'0x337AB7','A, LOC: 3 [43-45]',NULL,6),(14,'Anonymous function',3,1,'0x4CAE4C','Anonymous function, LOC: 3 [74-76]',NULL,8),(15,'A',3,1,'0x337AB7','A, LOC: 3 [43-45]',NULL,10),(16,'A',3,1,'0x337AB7','A, LOC: 3 [106-108]',NULL,12); - -/*Table structure for table `tb_city` */ - -DROP TABLE IF EXISTS `tb_city`; - -CREATE TABLE `tb_city` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `name` varchar(255) NOT NULL, - `tooltip` varchar(255) NOT NULL, - PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; - -/*Data for the table `tb_city` */ - -insert into `tb_city`(`id`,`name`,`tooltip`) values (1,'Example City','Example City @ ./system/metafora/'); - -/*Table structure for table `tb_district` */ - -DROP TABLE IF EXISTS `tb_district`; - -CREATE TABLE `tb_district` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `name` varchar(255) NOT NULL, - `color` varchar(10) NOT NULL, - `tooltip` varchar(255) NOT NULL, - `city_id` int(11) DEFAULT NULL, - `district_id` int(11) DEFAULT NULL, - PRIMARY KEY (`id`), - KEY `idx_fk_tb_district_tb_city` (`city_id`), - KEY `fk_tb_district_tb_district1_idx` (`district_id`), - CONSTRAINT `fk_tb_district_tb_city` FOREIGN KEY (`city_id`) REFERENCES `tb_city` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, - CONSTRAINT `fk_tb_district_tb_district1` FOREIGN KEY (`district_id`) REFERENCES `tb_district` (`id`) ON DELETE CASCADE ON UPDATE CASCADE -) ENGINE=InnoDB DEFAULT CHARSET=utf8; - -/*Data for the table `tb_district` */ - -insert into `tb_district`(`id`,`name`,`color`,`tooltip`,`city_id`,`district_id`) values (1,'index.js','0xD9534F','index.js, LOC: 57 [28-84]',1,NULL),(2,'distrito-b','0xF0AD4E','Folder: distrito-b',1,NULL),(3,'distrito-a','0xF0AD4E','Folder: distrito-a',1,NULL),(4,'index-2.js','0xD9534F','index-2.js, LOC: 55 [28-82]',NULL,2),(5,'index.js','0xD9534F','index.js, LOC: 30 [28-57]',NULL,2),(6,'index.js','0xD9534F','index.js, LOC: 93 [28-120]',NULL,3); - -/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; -/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; -/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; -/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; +/*!40101 SET NAMES utf8 */; + +/*!40101 SET SQL_MODE=''*/; + +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; +CREATE DATABASE IF NOT EXISTS `jscity`; + +CREATE USER IF NOT EXISTS jscity IDENTIFIED WITH mysql_native_password BY 'password'; +GRANT ALL PRIVILEGES ON jscity.* TO jscity; + +USE `jscity`; + +/*Table structure for table `tb_building` */ + +DROP TABLE IF EXISTS `tb_building`; + +CREATE TABLE `tb_building` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(255) NOT NULL, + `height` int(11) unsigned NOT NULL DEFAULT '0', + `width` int(11) unsigned NOT NULL DEFAULT '0', + `color` varchar(10) NOT NULL, + `tooltip` varchar(255) NOT NULL, + `district_id` int(11) DEFAULT NULL, + `building_id` int(11) DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `idx_fk_tb_construction_tb_district1` (`district_id`), + KEY `idx_fk_tb_building_tb_building1_idx` (`building_id`), + CONSTRAINT `fk_tb_building_tb_building1` FOREIGN KEY (`building_id`) REFERENCES `tb_building` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT `fk_tb_construction_tb_district1` FOREIGN KEY (`district_id`) REFERENCES `tb_district` (`id`) ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +/*Data for the table `tb_building` */ + +insert into `tb_building`(`id`,`name`,`height`,`width`,`color`,`tooltip`,`district_id`,`building_id`) values (1,'A',27,25,'0x337AB7','A, LOC: 27 [28-54]',1,NULL),(2,'Anonymous function',29,5,'0x4CAE4C','Anonymous function, LOC: 29 [56-84]',1,NULL),(3,'C',12,2,'0x337AB7','C, LOC: 12 [57-68]',NULL,2),(4,'B',8,2,'0x337AB7','B, LOC: 8 [70-77]',NULL,2),(5,'C',12,2,'0x337AB7','C, LOC: 12 [28-39]',4,NULL),(6,'B',11,3,'0x337AB7','B, LOC: 11 [41-51]',4,NULL),(7,'Anonymous function',12,2,'0x4CAE4C','Anonymous function, LOC: 12 [59-70]',4,NULL),(8,'Anonymous function',11,3,'0x4CAE4C','Anonymous function, LOC: 11 [72-82]',4,NULL),(9,'C',12,2,'0x337AB7','C, LOC: 12 [28-39]',5,NULL),(10,'B',11,3,'0x337AB7','B, LOC: 11 [41-51]',5,NULL),(11,'C',75,2,'0x337AB7','C, LOC: 75 [28-102]',6,NULL),(12,'B',11,3,'0x337AB7','B, LOC: 11 [104-114]',6,NULL),(13,'A',3,1,'0x337AB7','A, LOC: 3 [43-45]',NULL,6),(14,'Anonymous function',3,1,'0x4CAE4C','Anonymous function, LOC: 3 [74-76]',NULL,8),(15,'A',3,1,'0x337AB7','A, LOC: 3 [43-45]',NULL,10),(16,'A',3,1,'0x337AB7','A, LOC: 3 [106-108]',NULL,12); + +/*Table structure for table `tb_city` */ + +DROP TABLE IF EXISTS `tb_city`; + +CREATE TABLE `tb_city` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(255) NOT NULL, + `tooltip` varchar(255) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +/*Data for the table `tb_city` */ + +insert into `tb_city`(`id`,`name`,`tooltip`) values (1,'Example City','Example City @ ./system/metafora/'); + +/*Table structure for table `tb_district` */ + +DROP TABLE IF EXISTS `tb_district`; + +CREATE TABLE `tb_district` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(255) NOT NULL, + `color` varchar(10) NOT NULL, + `tooltip` varchar(255) NOT NULL, + `city_id` int(11) DEFAULT NULL, + `district_id` int(11) DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `idx_fk_tb_district_tb_city` (`city_id`), + KEY `fk_tb_district_tb_district1_idx` (`district_id`), + CONSTRAINT `fk_tb_district_tb_city` FOREIGN KEY (`city_id`) REFERENCES `tb_city` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT `fk_tb_district_tb_district1` FOREIGN KEY (`district_id`) REFERENCES `tb_district` (`id`) ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +/*Data for the table `tb_district` */ + +insert into `tb_district`(`id`,`name`,`color`,`tooltip`,`city_id`,`district_id`) values (1,'index.js','0xD9534F','index.js, LOC: 57 [28-84]',1,NULL),(2,'distrito-b','0xF0AD4E','Folder: distrito-b',1,NULL),(3,'distrito-a','0xF0AD4E','Folder: distrito-a',1,NULL),(4,'index-2.js','0xD9534F','index-2.js, LOC: 55 [28-82]',NULL,2),(5,'index.js','0xD9534F','index.js, LOC: 30 [28-57]',NULL,2),(6,'index.js','0xD9534F','index.js, LOC: 93 [28-120]',NULL,3); + +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;