create-directus-docker 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,86 @@
1
+ ## Docker with Directus / MySQL / Adminer / GraphQL
2
+
3
+ This package contains everything you need to get up and running with Directus CMS with a MySQL backend, plus an interactive GraphQL playground to help you develop your API queries.
4
+
5
+ **Before you do anything else**, you need [Docker](https://www.docker.com) and [Node](https://nodejs.org) installed on your machine:
6
+
7
+ - [Install Docker Desktop](https://www.docker.com/products/docker-desktop/)
8
+ - [Install Node](https://nodejs.org/en/download/)
9
+
10
+ Once you've installed Node and Docker, and have Docker Desktop running on your machine, you can install and configure this package by following these steps:
11
+
12
+ 1. Open a terminal session:
13
+ **Mac:** Go to /Applications/Utilities then double-click Terminal.
14
+ **Windows:** Click 'Start' taskbar button, Select 'All apps', scroll down and select 'Terminal'
15
+ 2. CD into the parent directory where you'd like to install this, ie `cd '~/Documents/My-Folder'`.
16
+ 3. From there, run:
17
+ `git clone https://github.com/rollmug/directus-mysql-template.git`
18
+ OR, if you don't have git installed, [manually download it here](https://github.com/rollmug/directus-mysql-template/archive/refs/heads/main.zip) and unzip it.
19
+ 4. Navigate into the folder you just downloaded, ie `cd 'directus-mysql-template'`
20
+
21
+ **Configure your Directus/MySQL setup:**
22
+
23
+ 1. Within the directus-mysql-template directory, run `npm install`
24
+ 2. Lastly, run `npm start`. The wizard will walk you through the config, and automatically launch all containers for you.
25
+
26
+ *Note that you'll only have to do these two steps the first time you run this.*
27
+
28
+ ### Starting/stopping with Docker Compose
29
+
30
+ To **stop** your running containers, simply run `docker compose down` in your terminal from within the project directory. All containers will be stopped.
31
+
32
+ To **restart** your stopped containers, you can let the helper app do it for you:
33
+
34
+ `npm start`
35
+
36
+ Or you can follow this sequence:
37
+
38
+ `docker compose up mysql -d`
39
+
40
+ Then wait 10-20 seconds (for MySQL to boot), then type:
41
+
42
+ `docker compose up -d`
43
+
44
+ **Boom!** You're done. Now you can access the URLS from here:
45
+
46
+ Directus CMS: http://localhost:8055
47
+ GraphQL Playground: http://localhost:4000/graphql
48
+ Adminer (for MySQL): http://localhost:8080
49
+
50
+ **Check on running containers:**
51
+
52
+ Simply run `docker compose ps` to see the status of running containers. Or, run `docker compose ps -a` to see all containers, running or not.
53
+
54
+ ## CORS problems on localhost
55
+
56
+ When using the GraphiQL playground on localhost, you'll run into some browser problems related to CORS. Here's how to get around it:
57
+
58
+ **Safari:**
59
+
60
+ 1. Enable the developer menu by opening up Settings > Advanced, then check "Show Develop bar in menu"
61
+ 2. Click the Develop menu and check "Disable Cross-Origin Restrictions"
62
+
63
+ **Chrome**
64
+
65
+ Temporarily Disable CORS in Chrome (MacOS):
66
+
67
+ 1. Quit Chrome
68
+ 2. Open terminal and run:
69
+
70
+ `open -n -a "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" --args --user-data-dir="/tmp/chrome_dev_test" --disable-web-security`
71
+
72
+ **Windows:**
73
+
74
+ 1. Right click on desktop, add new shortcut
75
+ 2. Add the target as "[PATH_TO_CHROME]\chrome.exe" --disable-web-security --disable-gpu --user-data-dir=~/chromeTemp
76
+ 3. Click OK
77
+
78
+ **Firefox:**
79
+
80
+ For Firefox you can simply install [CORS Everywhere](https://addons.mozilla.org/en-US/firefox/addon/cors-everywhere/) addon.
81
+
82
+ ### Examples of getting an auth token for API:
83
+
84
+ curl -X POST localhost:8055/auth/login -H 'Content-Type: application/json' -d '{"email":"you@email.com","password":"your-password"}'
85
+
86
+ curl -X POST localhost:8055/auth/refresh -H 'Content-Type: application/json' -d '{"refresh_token": "W5L70MBXKElx5ZVZwxmQVG8qdVjukiRVIwD5FYG7tCPyyuCM_I3IyCsYnFhMUrRi", "mode": "json"}'
@@ -0,0 +1,93 @@
1
+ version: '3'
2
+
3
+ services:
4
+
5
+ mysql:
6
+ container_name: mysql
7
+ image: mysql:8
8
+ restart: always
9
+ healthcheck:
10
+ test: ['mysqladmin', 'ping', '--silent']
11
+ interval: 5s
12
+ timeout: 20s
13
+ retries: 3
14
+ start_period: 5s
15
+ networks:
16
+ - directus
17
+ ports:
18
+ - 3306:3306
19
+ volumes:
20
+ - ./mysql:/var/lib/mysql
21
+ command: --default-authentication-plugin=mysql_native_password
22
+ environment:
23
+ MYSQL_USER: ${MYSQL_USER}
24
+ MYSQL_PASSWORD: ${MYSQL_PASS}
25
+ MYSQL_DATABASE: ${MYSQL_DB}
26
+ MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASS}
27
+
28
+ adminer:
29
+ container_name: adminer
30
+ image: adminer:latest
31
+ restart: always
32
+ depends_on:
33
+ - mysql
34
+ networks:
35
+ - directus
36
+ environment:
37
+ ADMINER_DEFAULT_SERVER: mysql
38
+ ports:
39
+ - 8080:8080
40
+
41
+ cache:
42
+ container_name: cache
43
+ image: redis:6
44
+ networks:
45
+ - directus
46
+
47
+ directus:
48
+ container_name: directus
49
+ image: directus/directus:latest
50
+ ports:
51
+ - 8055:8055
52
+ volumes:
53
+ - ./directus/uploads:/directus/uploads
54
+ networks:
55
+ - directus
56
+ depends_on:
57
+ - cache
58
+ - mysql
59
+ environment:
60
+ KEY: '255d861b-5ea1-5996-9aa3-922530ec40b1'
61
+ SECRET: '6116487b-cda1-52c2-b5b5-c8022c45e263'
62
+ DB_CLIENT: 'mysql'
63
+ DB_HOST: 'mysql'
64
+ DB_USER: ${MYSQL_USER}
65
+ DB_PASSWORD: ${MYSQL_PASS}
66
+ DB_DATABASE: ${MYSQL_DB}
67
+ DB_PORT: '3306'
68
+
69
+ CACHE_ENABLED: 'true'
70
+ CACHE_STORE: 'redis'
71
+ CACHE_REDIS: 'redis://cache:6379'
72
+ ADMIN_EMAIL: ${ADMIN_EMAIL}
73
+ ADMIN_PASSWORD: ${ADMIN_PASSWORD}
74
+ CORS_ENABLED: 'true'
75
+ CORS_ORIGIN: '*'
76
+
77
+ PUBLIC_URL: ${PUBLIC_URL}
78
+
79
+ graphiql:
80
+ container_name: graphiql
81
+ image: plutoz/graphiql
82
+ depends_on:
83
+ - directus
84
+ ports:
85
+ - 4000:4000
86
+ environment:
87
+ API_ENDPOINT: ${API_ENDPOINT}
88
+ networks:
89
+ - directus
90
+
91
+ networks:
92
+ directus:
93
+ driver: bridge
@@ -0,0 +1,13 @@
1
+ import chalk from 'chalk';
2
+
3
+ export default function checkRequirements() {
4
+ const nodeVersion = process.versions.node;
5
+ const major = +nodeVersion.split('.')[0];
6
+
7
+ if (major < 12) {
8
+ console.error(`You are running ${chalk.red(`Node ${nodeVersion}`)}.`);
9
+ console.error(`Directus requires ${chalk.green(`Node 12`)} and up.`);
10
+ console.error('Please update your Node version and try again.');
11
+ process.exit(1);
12
+ }
13
+ };
package/lib/index.js ADDED
@@ -0,0 +1,136 @@
1
+ #!/usr/bin/env node
2
+
3
+ import inquirer from 'inquirer';
4
+ import chalk from 'chalk';
5
+ import path from 'path';
6
+ import generator from 'generate-password';
7
+ import * as fs from 'fs';
8
+ import * as dotenv from 'dotenv';
9
+ dotenv.config();
10
+
11
+ import checkRequirements from './check-requirements.js';
12
+ import launchServices from './launch-services.js';
13
+
14
+ const rootPath = path.resolve();
15
+ const envPath = './.env';
16
+
17
+ try {
18
+ console.log(chalk.magentaBright("Configure Directus with MySQL, Adminer, and a GraphQL playground"));
19
+ console.log(chalk.dim("---------------------------------------------\n"));
20
+
21
+ checkRequirements();
22
+
23
+ if (fs.existsSync(envPath) && process.env.ADMIN_EMAIL !== null && process.env.ADMIN_PASSWORD !== null) {
24
+ console.log(chalk.green("Looks like we're already configured, so let's get things up and running."));
25
+ console.log("ps - you can always edit the variables that we've just set in the .env file manually.\n\nAutomatically running docker compose:\n");
26
+ launchServices();
27
+ } else {
28
+ //let's do the configuring!
29
+ console.log("Follow the prompts to configure Directus and MySQL.\n");
30
+
31
+ if (fs.existsSync(path.join(rootPath, 'mysql')) === false) {
32
+ fs.mkdir(path.join(rootPath, 'mysql'), () => {
33
+ //console.log(`'mysql' directory created.`);
34
+ });
35
+ }
36
+
37
+ if (fs.existsSync(path.join(rootPath, 'directus')) === false) {
38
+ fs.mkdir(path.join(rootPath, 'directus'), () => {
39
+ fs.mkdir(path.join(rootPath, 'directus/uploads'), () => {
40
+ //console.log(`'directus' directory created.`);
41
+ });
42
+ });
43
+ }
44
+
45
+ inquirer.prompt([
46
+ {
47
+ name: 'ADMIN_EMAIL',
48
+ message: "Type your email then hit return:",
49
+ validate: (email) => {
50
+ let valid = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)
51
+
52
+ if (valid) {
53
+ return true;
54
+ } else {
55
+ console.log(chalk.red("\nPlease enter a valid email."))
56
+ return false;
57
+ }
58
+ }
59
+ },
60
+ {
61
+ name: 'ADMIN_PASSWORD',
62
+ message: 'Type a password to login with, or hit return to use suggested:',
63
+ default: generator.generate({ length: 14, numbers: true, symbols: false }),
64
+ validate: (password) => {
65
+ if (password === null || password === false || password == '' || password.length < 8) {
66
+ console.log(chalk.red("\nPassword cannot be empty, and must have at least 8 characters."))
67
+ } else {
68
+ return true;
69
+ }
70
+ }
71
+ },
72
+ {
73
+ name: 'MYSQL_USER',
74
+ message: "Specify a user name for your MySQL database:",
75
+ default: 'admin'
76
+ },
77
+ {
78
+ name: 'MYSQL_PASS',
79
+ message: "MySQL Password:",
80
+ default: generator.generate({ length: 14, numbers: true, symbols: false }),
81
+ },
82
+ {
83
+ name: 'MYSQL_DB',
84
+ message: "Give your database a name:",
85
+ default: 'directus'
86
+ },
87
+ {
88
+ name: 'MYSQL_ROOT_PASS',
89
+ message: "Root password:",
90
+ default: generator.generate({ length: 14, numbers: true, symbols: false })
91
+ }
92
+ ]).then(answers => {
93
+ console.log(chalk.magentaBright("\nWriting to environment file. Be sure to take note of your credentials below, you'll need them shortly!\n"));
94
+ console.log(answers);
95
+
96
+ var writeStream = fs.createWriteStream(envPath);
97
+ writeStream.write("# Environment variables\n\n# Email and password to login to Directus:\n");
98
+ writeStream.write(`ADMIN_EMAIL="${answers.ADMIN_EMAIL}"`);
99
+ writeStream.write("\n");
100
+ writeStream.write(`ADMIN_PASSWORD="${answers.ADMIN_PASSWORD}"`);
101
+ writeStream.write("\n");
102
+
103
+ writeStream.write("\n");
104
+ writeStream.write("# Define the mysql user name and password:\n");
105
+
106
+ writeStream.write(`MYSQL_USER="${answers.MYSQL_USER}"`);
107
+ writeStream.write("\n");
108
+ writeStream.write(`MYSQL_PASS="${answers.MYSQL_PASS}"`);
109
+ writeStream.write("\n");
110
+
111
+ writeStream.write("\n");
112
+ writeStream.write("# Give your database a name:\n");
113
+ writeStream.write(`MYSQL_DB="${answers.MYSQL_DB}"`);
114
+ writeStream.write("\n");
115
+
116
+ writeStream.write("\n");
117
+ writeStream.write("# Set a root password for MySQL to something secure:\n");
118
+ writeStream.write(`MYSQL_ROOT_PASS="${answers.MYSQL_ROOT_PASS}"`);
119
+ writeStream.write("\n\n");
120
+
121
+ writeStream.write("# Set the domain for directus to use:\n");
122
+ writeStream.write("# eg, https://mydomain.com\n");
123
+ writeStream.write("# in local dev environments, just leave it set to localhost:port\n");
124
+ writeStream.write("PUBLIC_URL=\"http://localhost:8055\"\n");
125
+ writeStream.write("API_ENDPOINT=\"http://localhost:8055/graphql\"");
126
+
127
+ writeStream.end();
128
+
129
+ console.log(chalk.greenBright("\nAll set!"));
130
+ console.log("You can always edit the variables that we've just set in the .env file manually.\n");
131
+ console.log(`${chalk.greenBright("To launch all services, just run 'npm start' again.")}\n`);
132
+ })
133
+ }
134
+ } catch (err) {
135
+ console.error(err)
136
+ }
@@ -0,0 +1,71 @@
1
+ import chalk from 'chalk';
2
+ import ora from 'ora';
3
+ import waitOn from 'wait-on';
4
+ import { spawn } from 'node:child_process';
5
+ import logUpdate from 'log-update';
6
+
7
+ export default function launchServices() {
8
+ const launch = spawn('docker', ['compose', 'up', 'mysql', '-d']);
9
+
10
+ const opts = {
11
+ resources: [
12
+ 'tcp:127.0.0.1:3306'
13
+ ],
14
+ delay: 1000,
15
+ interval: 100,
16
+ timeout: 30000,
17
+ tcpTimeout: 30000
18
+ }
19
+
20
+ launch.stderr.on('data', (data) => {
21
+ logUpdate(`${data}`);
22
+ });
23
+
24
+ launch.on('close', code => {
25
+ logUpdate.done();
26
+ console.log(`Docker has launched MySQL service (status = ${code}).`);
27
+
28
+ const loader = ora('Waiting for MySQL to be ready').start();
29
+
30
+ waitOn(opts, (err) => {
31
+ if (err) console.log(err);
32
+ // once here, all resources are available
33
+
34
+ const sleeper = spawn('sleep', [5]);
35
+
36
+ sleeper.on('close', code => {
37
+ loader.stop();
38
+ console.log('MySQL ready.');
39
+
40
+ logUpdate("Building and launching containers:\n");
41
+ logUpdate.done();
42
+
43
+ const launch2 = spawn('docker', ['compose', 'up', '-d']);
44
+
45
+ launch2.stderr.on('data', (data) => {
46
+ logUpdate(`${data}`);
47
+ });
48
+
49
+ launch2.on('close', code => {
50
+ logUpdate('Done.');
51
+ logUpdate.done();
52
+
53
+ console.log(chalk.green("\nAll services should be ready. You can access them at the following URLs:\n"));
54
+
55
+ console.log(`Directus CMS: ${chalk.cyan("http://localhost:8055")}`);
56
+ console.log(`Adminer: ${chalk.cyan("http://localhost:8080")}`);
57
+ console.log(`GraphiQL Playground: ${chalk.cyan("http://localhost:4000/graphql")}`);
58
+
59
+ console.log(`\n${chalk.redBright("Note: learn how to avoid CORS errors in the GraphiQL Playground when running on localhost:")}`);
60
+ console.log(`https://github.com/rollmug/directus-mysql-template#cors-problems-on-localhost`)
61
+
62
+ console.log(`\n${chalk.green("Done! When you're finished, you can stop all running containers with:")}\n`);
63
+ console.log(`docker compose down\n`);
64
+ console.log(`${chalk.green("You can start them all back up again with:")}`);
65
+ console.log(`npm start\n`);
66
+ });
67
+ });
68
+
69
+ });
70
+ });
71
+ }
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "create-directus-docker",
3
+ "version": "1.0.6",
4
+ "description": "A helper app to configure Directus on Docker with MySQL, Adminer, and GraphQL",
5
+ "main": "./lib/index.js",
6
+ "type": "module",
7
+ "scripts": {
8
+ "start": "node ./lib/index.js",
9
+ "launch": "node ./lib/index.js",
10
+ "test": "echo \"No test specified\""
11
+ },
12
+ "bin": {
13
+ "create-directus-docker": "./lib/index.js",
14
+ "cdd": "./lib/index.js"
15
+ },
16
+ "keywords": [
17
+ "directus",
18
+ "docker",
19
+ "mysql",
20
+ "installer"
21
+ ],
22
+ "exports": {
23
+ ".": "./lib/index.js",
24
+ "./package.json": "./package.json"
25
+ },
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "git+https://github.com/rollmug/directus-mysql-template.git"
29
+ },
30
+ "author": {
31
+ "name": "Dave Kobrenski",
32
+ "email": "dave@rlmg.com"
33
+ },
34
+ "license": "GPL-3.0-only",
35
+ "bugs": {
36
+ "url": "https://github.com/rollmug/directus-mysql-template/issues"
37
+ },
38
+ "homepage": "https://github.com/rollmug/directus-mysql-template#readme",
39
+ "dependencies": {
40
+ "chalk": "^5.2.0",
41
+ "dotenv": "^16.0.3",
42
+ "generate-password": "^1.7.0",
43
+ "inquirer": "^9.1.4",
44
+ "log-update": "^5.0.1",
45
+ "ora": "^6.1.2",
46
+ "wait-on": "^7.0.1"
47
+ }
48
+ }