Browse Source

Add docker compose script to rapidly build a test DB locally for tests

johan 4 years ago
parent
commit
9b03aa9da4

+ 14 - 0
.env.development

@@ -0,0 +1,14 @@
+export VUE_APP_SERVER_CONN=http
+export VUE_APP_SERVER_HOST=localhost
+export VUE_APP_SERVER_PORT=2610
+export VUE_APP_PORT=2005
+export DB=127.0.0.1
+export DB_NAME=twelve_heroes
+export DB_PORT=3308
+export DB_SOCKET=/run/mysqld/mysqld.sock
+export DB_USER=th
+export DB_PASS=th123
+
+export BROWSER=firefox
+export BABEL_ENV=debug
+export NODE_ENV=development

+ 14 - 0
.env.production

@@ -0,0 +1,14 @@
+export VUE_APP_SERVER_CONN=https
+export VUE_APP_SERVER_HOST=
+export VUE_APP_SERVER_PORT=
+export VUE_APP_PORT=
+export DB=
+export DB_NAME=
+export DB_PORT=
+export DB_SOCKET=
+export DB_USER=
+export DB_PASS=
+
+unset BROWSER
+unset BABEL_ENV
+unset NODE_ENV

+ 2 - 0
.gitignore

@@ -18,3 +18,5 @@ yarn-error.log*
 *.njsproj
 *.sln
 *.sw?
+
+tests/db/.db-data

+ 20 - 8
README.md

@@ -24,25 +24,38 @@ What's left :
  - tests (shoud have started with that :) )
 
 ## Project setup
+
+### Prerequisites
+ - nodeJS >= 14
+ - npm >= v6.14.8
+ - Docker >= v19.03.13
+ - Docker Compose >= v1.27.4
 ```
 npm install
 cd server && npm install
 ```
 
 ## Compiles and hot-reloads for development in Firefox browser
+
+For development there is a default environment in `.env.development`
+
+### Run dev server with hot reloading upon changes
+To run the backend you also need a mariaDB, following will start :
+ - a local test DB with user1 to user5 created 
+ - the backend server
 ```
-npm run dev
+npm run dbtest
+npm run server-dev
 ```
 
-## Start dev server
+### Run VueJS app with hot reloading upon changes
 ```
-npm run server-dev
+npm run dev
 ```
 
 ## How to deploy using docker compose
 
-See in the docker-compose.yaml file, you will need to export some environment variables. 
-You can do a `.env` file with :
+You will need to fill up the `.env.production` file
 ```
 # HOST and PORT of the backend server for client socket connection
 export VUE_APP_SERVER_HOST=
@@ -66,9 +79,8 @@ export DB_PASS=
 
 Then you just need to do :
 ```
-source .env
-docker-compose build
-deocker-compose start
+npm run build-docker
+docker-compose start
 ## Or docker-compose up    to get logs
 ```
 

+ 9 - 7
package.json

@@ -4,14 +4,16 @@
   "private": true,
   "scripts": {
     "build": "vue-cli-service build",
+    "build-docker": "source .env.production && docker-compose build",
+    "build-docker-dev": "source .env.development && docker-compose build",
     "lint": "vue-cli-service lint",
     "appserver": "node app-server/app-server.js",
-    "build-dev": "vue-cli-service build --mode development",
-    "dev": "vue-cli-service serve --open",
-    "server": "cd server && npm start",
-    "server-dev": "cd server && npm run server-dev",
-    "tu": "vue-cli-service test:unit --watch",
-    "show": "ssh jojo@149.91.81.94 -p 1988 -nNT -o ServerAliveInterval=30 -R 8085:localhost:8080",
+    "dbtest": "source .env.development && cd tests/db && docker-compose start",
+    "build-dev": "source .env.development && vue-cli-service build --mode development",
+    "dev": "source .env.development && vue-cli-service serve --open",
+    "server": "source .env.production && cd server && npm start",
+    "server-dev": "source .env.development && cd server && npm run server-dev",
+    "tu": "source .env.development && vue-cli-service test:unit --watch",
     "doc": "jsdoc -c jsdoc/jsdoc.conf"
   },
   "dependencies": {
@@ -51,4 +53,4 @@
     "vue-cli-plugin-style-resources-loader": "^0.1.4",
     "vue-template-compiler": "^2.6.11"
   }
-}
+}

File diff suppressed because it is too large
+ 704 - 7
server/package-lock.json


+ 3 - 9
server/package.json

@@ -7,8 +7,8 @@
   ],
   "type": "module",
   "scripts": {
-    "start": "babel-node src/server.js",
-    "server-dev": "babel-node -r ../node_modules/dotenv/config src/server.js dotenv_config_path=../.env.development",
+    "start": "node src/server.js",
+    "server-dev": "nodemon src/server.js",
     "tools": "node src/tools/server-tools.js"
   },
   "author": "JOJO",
@@ -18,13 +18,7 @@
     "socket.io": "^2.3.0"
   },
   "devDependencies": {
-    "@babel/cli": "^7.8.4",
-    "@babel/core": "^7.9.6",
-    "@babel/node": "7.6.2",
-    "@babel/plugin-proposal-object-rest-spread": "^7.9.0",
-    "@babel/plugin-transform-runtime": "^7.9.6",
-    "@babel/preset-env": "7.6.2",
-    "@babel/register": "^7.9.0",
+    "nodemon": "^2.0.6",
     "socket.io-client": "^2.3.0"
   }
 }

+ 2 - 2
server/src/tools/server-tools.js

@@ -1,5 +1,5 @@
 'use strict';
-
+import io from 'socket.io-client';
 // process.exit(1);
 var usage = function() {
   var text = `
@@ -74,7 +74,7 @@ switch (args[0]) {
 
 if (command !== '') {
   console.log('Connecting to server...');
-  var socket = require('socket.io-client')('http://localhost:1664');
+  var socket = io('http://localhost:1664');
   socket.emit(command, option, function(response) {
     console.log('Done, response from server : ', response);
   });

+ 8 - 2
src/common/socket-service.js

@@ -1,7 +1,9 @@
 'use strict';
 import io from 'socket.io-client';
 /* eslint-disable no-console */
-const SERVER_URL = 'https://' + process.env.VUE_APP_SERVER_HOST;
+
+const SERVER_URL =
+  process.env.VUE_APP_SERVER_CONN + '://' + process.env.VUE_APP_SERVER_HOST;
 const SERVER_PORT = process.env.VUE_APP_SERVER_PORT;
 
 export default function SocketService(socketEventBus, vuexStore) {
@@ -9,7 +11,11 @@ export default function SocketService(socketEventBus, vuexStore) {
   let ioClient = null;
   let connect = function(name) {
     let promise = new Promise((resolve, reject) => {
-      ioClient = io(SERVER_URL, {
+      let serverUrl = SERVER_URL;
+      if (SERVER_PORT != '') {
+        serverUrl += ':' + SERVER_PORT;
+      }
+      ioClient = io(serverUrl, {
         reconnectionAttempts: 2
       });
 

+ 22 - 0
tests/db/docker-compose.yaml

@@ -0,0 +1,22 @@
+version: '3.1'
+
+services:
+  db:
+    image: mariadb:10.5.8
+    restart: always
+    environment:
+      - MYSQL_ROOT_PASSWORD=root00
+      - MYSQL_DATABASE=${DB_NAME}
+      - MYSQL_USER=${DB_USER}
+      - MYSQL_PASSWORD=${DB_PASS}
+    volumes:
+      - ./.db-data:/var/lib/mysql
+      - ./initdb:/docker-entrypoint-initdb.d
+    ports:
+      - "${DB_PORT}:3306"
+
+  adminer:
+    image: adminer
+    restart: always
+    ports:
+      - "8080:8080"

+ 81 - 0
tests/db/initdb/twelve_heroes.sql

@@ -0,0 +1,81 @@
+-- MySQL dump 10.17  Distrib 10.3.25-MariaDB, for debian-linux-gnu (x86_64)
+--
+-- Host: localhost    Database: th_backup
+-- ------------------------------------------------------
+-- Server version	10.3.25-MariaDB-0+deb10u1
+
+/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
+/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
+/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
+/*!40101 SET NAMES utf8mb4 */;
+/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
+/*!40103 SET TIME_ZONE='+00:00' */;
+/*!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 */;
+
+--
+-- Table structure for table `games`
+--
+
+DROP TABLE IF EXISTS `games`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `games` (
+  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
+  `player1` varchar(64) NOT NULL,
+  `player2` varchar(64) NOT NULL,
+  `deck` enum('faction','draft','tournament') NOT NULL DEFAULT 'faction',
+  `adv_rules` set('popularity','discard') DEFAULT '',
+  `status` enum('CREATED','PLAYING','FINISHED','PAUSED') NOT NULL DEFAULT 'CREATED',
+  `game_data` longtext NOT NULL,
+  `last_played` datetime NOT NULL,
+  PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=47 DEFAULT CHARSET=utf8mb4 COMMENT='All the online games (created, ongoing & finished)';
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `games`
+--
+
+LOCK TABLES `games` WRITE;
+/*!40000 ALTER TABLE `games` DISABLE KEYS */;
+/*!40000 ALTER TABLE `games` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `players`
+--
+
+DROP TABLE IF EXISTS `players`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `players` (
+  `username` varchar(30) NOT NULL,
+  `last_connection` datetime NOT NULL,
+  `friends` text DEFAULT NULL,
+  UNIQUE KEY `username` (`username`) USING BTREE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='table with all players with online account';
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `players`
+--
+
+LOCK TABLES `players` WRITE;
+/*!40000 ALTER TABLE `players` DISABLE KEYS */;
+INSERT INTO `players` VALUES ('user1','2020-11-22 00:00:00',NULL),('user2','2020-11-22 00:00:00',NULL),('user3','2020-11-22 00:00:00',NULL),('user4','2020-11-22 00:00:00',NULL),('user5','2020-11-22 00:00:00',NULL);
+/*!40000 ALTER TABLE `players` ENABLE KEYS */;
+UNLOCK TABLES;
+/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
+
+/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
+/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
+/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
+/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
+/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
+/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
+/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
+
+-- Dump completed on 2020-11-24 11:54:36

Some files were not shown because too many files changed in this diff