Skip to content
This repository was archived by the owner on Jan 24, 2023. It is now read-only.

Commit a92ddd1

Browse files
committed
feat: added docker support
1 parent b8c79d8 commit a92ddd1

9 files changed

Lines changed: 143 additions & 16 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM nginx
2+
USER root
3+
COPY ./dist /usr/share/nginx/html
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM tomcat:alpine
2+
USER root
3+
RUN rm -rf /usr/local/tomcat/webapps/
4+
COPY ./target/<%-options.serverSetup.serverName%>.war /usr/local/tomcat/webapps/ROOT.war
5+
CMD ["catalina.sh", "run"]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
server:
2+
image: ftgibran/simpli-tomcat:latest
3+
container_name: tomcat-container-<%-options.serverSetup.connection.database%>
4+
ports:
5+
- "8080:8080"
6+
volumes:
7+
- ./target/<%-options.serverSetup.serverName%>.war:/usr/local/tomcat/webapps/ROOT.war
8+
links:
9+
- db
10+
db:
11+
image: mysql:5.7
12+
container_name: mysql-container-<%-options.serverSetup.connection.database%>
13+
environment:
14+
MYSQL_USER: <%-options.serverSetup.localhostConnection.user%>
15+
MYSQL_PASSWORD: <%-options.serverSetup.localhostConnection.password%>
16+
MYSQL_DATABASE: <%-options.serverSetup.connection.database%>
17+
expose:
18+
- "<%-options.serverSetup.localhostConnection.port%>"
19+
volumes:
20+
- ./src/test/resources/database/create.sql:/docker-entrypoint-initdb.d/create.sql
21+
- ./src/test/resources/database/data.sql:/docker-entrypoint-initdb.d/data.sql

packages/@simpli/cli-server/generator/template/pom.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1212
<kotlin.version>1.2.71</kotlin.version>
1313
</properties>
14+
1415
<dependencies>
1516
<dependency>
1617
<groupId>junit</groupId>
@@ -93,6 +94,7 @@
9394
<version>1.11.302</version>
9495
</dependency>
9596
</dependencies>
97+
9698
<build>
9799
<finalName><%-options.serverSetup.serverName%></finalName>
98100
<plugins>
@@ -190,7 +192,6 @@
190192
<configuration>
191193
<sourceDirs>
192194
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
193-
<sourceDir>${project.basedir}/src/main/java</sourceDir>
194195
</sourceDirs>
195196
</configuration>
196197
</execution>
@@ -202,12 +203,13 @@
202203
<configuration>
203204
<sourceDirs>
204205
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
205-
<sourceDir>${project.basedir}/src/test/java</sourceDir>
206206
</sourceDirs>
207207
</configuration>
208208
</execution>
209+
209210
</executions>
210211
</plugin>
211212
</plugins>
212213
</build>
214+
213215
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env bash
2+
3+
simpli server:seed --localhost
4+
mvn clean package
5+
docker-compose stop
6+
docker-compose up -d
7+
8+
sleep 10
9+
10+
echo ""
11+
tput setaf 2; echo "The mysql database container is now running"
12+
tput setaf 2; echo "The tomcat server container is now running"
13+
echo ""
14+
tput setaf 3; echo "=> Run 'docker-compose stop' to stop the server"
15+
tput setaf 3; echo "=> Run 'docker-compose up' to run the server"
16+
tput setaf 3; echo "=> Run 'docker-compose up -d' to run the server in the background"
17+
tput setaf 3; echo "=> Run 'docker-compose down' to close the server and destroy the database"
18+
echo ""
19+
tput setaf 3; echo "Whenever you make changes, run 'mvn clean package' and then 'docker-compose restart'"
20+
echo ""
21+
tput setaf 5; echo "Check the swagger info in http://localhost:8080/"
22+
tput sgr0; echo ""
23+
24+
sleep 5
25+
26+
open http://localhost:8080/
27+
28+
read -p "Press enter to exit" nothing
29+

packages/@simpli/cli-server/generator/template/src/main/webapp/META-INF/context.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
<Context antiJARLocking="true" path="/<%-options.serverSetup.serverName%>">
33
<Resource
44
name="jdbc/<%-options.serverSetup.connection.database%>DS"
5-
url="jdbc:mysql://<%-options.serverSetup.connection.host%>:<%-options.serverSetup.connection.port%>/<%-options.serverSetup.connection.database%>"
6-
username="<%-options.serverSetup.connection.user%>"
7-
password="<%-options.serverSetup.connection.password%>"
5+
url="jdbc:mysql://db:<%-options.serverSetup.localhostConnection.port%>/<%-options.serverSetup.connection.database%>"
6+
username="<%-options.serverSetup.localhostConnection.user%>"
7+
password="<%-options.serverSetup.localhostConnection.password%>"
88
auth="Container" driverClassName="com.mysql.jdbc.Driver"
99
factory="org.apache.commons.dbcp.BasicDataSourceFactory"
1010
initialSize="1" maxActive="3" maxIdle="1"
1111
maxWait="20000" minEvictableIdleTimeMillis="3000" minIdle="1"
1212
removeAbandonedTimeout="5" timeBetweenEvictionRunsMillis="30000"
1313
type="javax.sql.DataSource"
1414
/>
15-
</Context>
15+
</Context>

packages/@simpli/cli/lib/server/Database.js

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ module.exports = class Database {
8383
}
8484

8585
const dataTables = []
86+
const connection = { host, port, user, password, database }
8687

8788
try {
88-
const pool = mysql.createPool({ host, port, user, password, database })
89+
const pool = mysql.createPool(connection)
8990

9091
const getFirstKey = (obj) => Object.keys(obj)[0] || null
9192

@@ -158,9 +159,69 @@ module.exports = class Database {
158159
process.exit(1)
159160
}
160161

161-
const connection = { host, port, user, password, database }
162+
const localhostHost = 'localhost'
163+
let localhostPort = port
164+
let localhostUser = user
165+
let localhostPassword = password
166+
167+
if (host !== 'localhost' && host !== '127.0.0.1' && host !== '0.0.0.0') {
168+
const { port } = defaultConfig.port ? defaultConfig
169+
: await inquirer.prompt([
170+
{
171+
name: 'port',
172+
type: 'input',
173+
message: 'Enter the MYSQL port of the localhost',
174+
default: 3306
175+
}
176+
])
177+
if (!port) {
178+
error('You must define the MYSQL port of the localhost')
179+
process.exit(1)
180+
}
181+
182+
const { user } = defaultConfig.user ? defaultConfig
183+
: await inquirer.prompt([
184+
{
185+
name: 'user',
186+
type: 'input',
187+
message: 'Enter the MYSQL user of the localhost',
188+
default: 'root'
189+
}
190+
])
191+
if (!user) {
192+
error('You must define the MYSQL user of the localhost')
193+
process.exit(1)
194+
}
195+
196+
const { password } = defaultConfig.password ? defaultConfig
197+
: await inquirer.prompt([
198+
{
199+
name: 'password',
200+
type: 'password',
201+
message: 'Enter the MYSQL password of the localhost'
202+
}
203+
])
204+
if (!password) {
205+
error('You must define the MYSQL password of the localhost')
206+
process.exit(1)
207+
}
208+
209+
localhostPort = port
210+
localhostUser = user
211+
localhostPassword = password
212+
}
213+
214+
const localhostConnection = { host: localhostHost, port: localhostPort, user: localhostUser, password: localhostPassword }
215+
try {
216+
const pool = mysql.createPool(localhostConnection)
217+
await pool.query(`SHOW DATABASES;`)
218+
pool.end()
219+
} catch (e) {
220+
error(e.sqlMessage || e)
221+
process.exit(1)
222+
}
162223

163-
return { connection, dataTables, availableTables, allTables, createSQL }
224+
return { connection, localhostConnection, dataTables, availableTables, allTables, createSQL }
164225
}
165226

166227
static async requestServerName (defaultName) {

packages/@simpli/cli/lib/server/Server.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ module.exports = class Server {
4949

5050
async databaseSetup () {
5151
// Get data normalize database
52-
const { connection, availableTables, createSQL } = await Database.requestConnection(this.serverSetup)
52+
const { connection, localhostConnection, availableTables, createSQL } = await Database.requestConnection(this.serverSetup)
5353

5454
// Set the server name
5555
const { serverName } = await Database.requestServerName(this.name)
@@ -77,6 +77,7 @@ module.exports = class Server {
7777
this.serverSetup.packageAddress = packageAddress
7878

7979
this.serverSetup.connection = connection
80+
this.serverSetup.localhostConnection = localhostConnection
8081

8182
this.serverSetup.userTable = userTable
8283
this.serverSetup.accountColumn = accountColumn
@@ -166,11 +167,9 @@ module.exports = class Server {
166167
log()
167168
log(`🎉 Successfully created server project ${chalk.yellow(name)}.`)
168169
log(`👉 Go to ${chalk.cyan(`cd ${name}`)}`)
169-
log(`👉 Seed your database: ${chalk.cyan(`simpli server:seed`)}`)
170-
log(`👉 Generate the WAR file: ${chalk.cyan('mvn package')}`)
171-
log(`👉 Initialize Tomcat: ${chalk.cyan('tomcat start')}`)
172-
log(`👉 Move the WAR file to tomcat folder to deploy it: ${chalk.cyan('mv <warfile> <tomcat-webapps-location>')}`)
173-
log(`👉 Go to ${chalk.cyan('localhost:8080/[WAR-file-name]')}`)
170+
log(`👉 Make sure the port ${chalk.cyan(`8080`)} is not in use`)
171+
log(`👉 Run ${chalk.yellow(`sh serve.sh`)}`)
172+
log(`👉 Go to ${chalk.cyan('http://localhost:8080')}`)
174173
log()
175174

176175
generator.printExitLogs()
@@ -253,7 +252,7 @@ module.exports = class Server {
253252
}
254253

255254
// Get data normalize database
256-
const { connection, availableTables, createSQL } = await Database.requestConnection(this.serverSetup, defaultConnection)
255+
const { connection, localhostConnection, availableTables, createSQL } = await Database.requestConnection(this.serverSetup, defaultConnection)
257256
// Select tables to be added
258257
const { filteredTables } = await Database.requestTables(availableTables, this.serverSetup)
259258

@@ -270,6 +269,7 @@ module.exports = class Server {
270269
await Database.confirm()
271270

272271
this.serverSetup.connection = connection
272+
this.serverSetup.localhostConnection = localhostConnection
273273
this.serverSetup.seedSamples = seedSamples
274274
this.serverSetup.createSQL = createSQL
275275

packages/@simpli/cli/lib/server/setup/ServerSetup.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ module.exports = class ServerSetup {
2121
password: null,
2222
database: null
2323
}
24+
this.localhostConnection = {
25+
host: null,
26+
port: null,
27+
user: null,
28+
password: null
29+
}
2430
this.userTable = new Table() // The user table used for authentication (e.g. user)
2531
this.accountColumn = new Column() // The account column used for authentication (e.g. email)
2632
this.passwordColumn = new Column() // The password column used for authentication (e.g. password)

0 commit comments

Comments
 (0)