Skip to content

Commit 24f9023

Browse files
authored
Merge pull request #78 from create-go-app/dev
Add Postgres and Redis roles; Fix proxy 'none' case
2 parents 798a461 + 4201b7c commit 24f9023

File tree

11 files changed

+254
-83
lines changed

11 files changed

+254
-83
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
</h1>
55
<p align="center">Create a new production-ready project with <b>backend</b> (Golang), <b>frontend</b> (JavaScript, TypeScript)<br/>and <b>deploy automation</b> (Ansible, Docker) by running one CLI command.<br/><br/>Focus on <b>writing</b> code and <b>thinking</b> of business-logic! The CLI will take care of the rest.</p>
66

7-
<p align="center"><a href="https://github.com/create-go-app/cli/releases" target="_blank"><img src="https://img.shields.io/badge/version-v2.0.0-blue?style=for-the-badge&logo=none" alt="cli version" /></a>&nbsp;<a href="https://pkg.go.dev/github.com/create-go-app/cli?tab=doc" target="_blank"><img src="https://img.shields.io/badge/Go-1.16+-00ADD8?style=for-the-badge&logo=go" alt="go version" /></a>&nbsp;<a href="https://gocover.io/github.com/create-go-app/cli/pkg/cgapp" target="_blank"><img src="https://img.shields.io/badge/Go_Cover-89%25-success?style=for-the-badge&logo=none" alt="go cover" /></a>&nbsp;<a href="https://goreportcard.com/report/github.com/create-go-app/cli" target="_blank"><img src="https://img.shields.io/badge/Go_report-A+-success?style=for-the-badge&logo=none" alt="go report" /></a>&nbsp;<img src="https://img.shields.io/badge/license-apache_2.0-red?style=for-the-badge&logo=none" alt="license" /></p>
7+
<p align="center"><a href="https://github.com/create-go-app/cli/releases" target="_blank"><img src="https://img.shields.io/badge/version-v2.1.0-blue?style=for-the-badge&logo=none" alt="cli version" /></a>&nbsp;<a href="https://pkg.go.dev/github.com/create-go-app/cli?tab=doc" target="_blank"><img src="https://img.shields.io/badge/Go-1.16+-00ADD8?style=for-the-badge&logo=go" alt="go version" /></a>&nbsp;<a href="https://gocover.io/github.com/create-go-app/cli/pkg/cgapp" target="_blank"><img src="https://img.shields.io/badge/Go_Cover-89%25-success?style=for-the-badge&logo=none" alt="go cover" /></a>&nbsp;<a href="https://goreportcard.com/report/github.com/create-go-app/cli" target="_blank"><img src="https://img.shields.io/badge/Go_report-A+-success?style=for-the-badge&logo=none" alt="go report" /></a>&nbsp;<img src="https://img.shields.io/badge/license-apache_2.0-red?style=for-the-badge&logo=none" alt="license" /></p>
88

99
## ⚡️ Quick start
1010

@@ -132,6 +132,16 @@ cgapp deploy [OPTION]
132132

133133
> ✌️ Since Create Go App CLI `v2.0.0`, we're recommend to use **Traefik Proxy** as default proxy server for your projects. The main reason: this proxy provides _automatic_ SSL certificates from Let's Encrypt out of the box. Also, Traefik was built on the Docker ecosystem and has a _really good looking_ and _useful_ Web UI.
134134
135+
### Database
136+
137+
- Roles for run Docker container with [PostgreSQL](https://postgresql.org/):
138+
- `postgres` — configured PostgreSQL container with apply migrations for backend.
139+
140+
### Cache (key-value storage)
141+
142+
- Roles for run Docker container with [Redis](https://redis.io/):
143+
- `redis` — configured Redis container for backend.
144+
135145
## ⭐️ Project assistance
136146

137147
If you want to say **thank you** or/and support active development of `Create Go App CLI`:

cmd/create.go

Lines changed: 68 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
6060
return cgapp.ShowError(err.Error())
6161
}
6262

63-
// Cleanup project.
64-
cgapp.RemoveFolders("backend", []string{".git", ".github"})
65-
6663
// Show success report.
6764
cgapp.ShowMessage(
6865
"success",
@@ -96,66 +93,60 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
9693
The project's webserver part creation.
9794
*/
9895

99-
if proxy != "none" {
100-
// Copy Ansible roles from embedded file system.
101-
if err := cgapp.CopyFromEmbeddedFS(
102-
&cgapp.EmbeddedFileSystem{
103-
Name: registry.EmbedRoles,
104-
RootFolder: "roles",
105-
SkipDir: false,
106-
},
107-
); err != nil {
108-
return cgapp.ShowError(err.Error())
109-
}
110-
111-
// Copy Ansible playbook, inventory and roles from embedded file system.
112-
if err := cgapp.CopyFromEmbeddedFS(
113-
&cgapp.EmbeddedFileSystem{
114-
Name: registry.EmbedTemplates,
115-
RootFolder: "templates",
116-
SkipDir: true,
117-
},
118-
); err != nil {
119-
return cgapp.ShowError(err.Error())
120-
}
96+
// Copy Ansible playbook, inventory and roles from embedded file system.
97+
if err := cgapp.CopyFromEmbeddedFS(
98+
&cgapp.EmbeddedFileSystem{
99+
Name: registry.EmbedTemplates,
100+
RootFolder: "templates",
101+
SkipDir: true,
102+
},
103+
); err != nil {
104+
return cgapp.ShowError(err.Error())
105+
}
121106

122-
// Set template variables for Ansible playbook and inventory files.
123-
inventory = registry.AnsibleInventoryVariables[proxy].List
124-
playbook = registry.AnsiblePlaybookVariables[proxy].List
107+
// Set template variables for Ansible playbook and inventory files.
108+
inventory = registry.AnsibleInventoryVariables[proxy].List
109+
playbook = registry.AnsiblePlaybookVariables[proxy].List
125110

126-
// Generate Ansible inventory file.
127-
if err := cgapp.GenerateFileFromTemplate("hosts.ini.tmpl", inventory); err != nil {
128-
return cgapp.ShowError(err.Error())
129-
}
111+
// Generate Ansible inventory file.
112+
if err := cgapp.GenerateFileFromTemplate("hosts.ini.tmpl", inventory); err != nil {
113+
return cgapp.ShowError(err.Error())
114+
}
130115

131-
// Generate Ansible playbook file.
132-
if err := cgapp.GenerateFileFromTemplate("playbook.yml.tmpl", playbook); err != nil {
133-
return cgapp.ShowError(err.Error())
134-
}
116+
// Generate Ansible playbook file.
117+
if err := cgapp.GenerateFileFromTemplate("playbook.yml.tmpl", playbook); err != nil {
118+
return cgapp.ShowError(err.Error())
119+
}
135120

136-
// Set unused proxy roles.
137-
if proxy == "traefik" || proxy == "traefik-acme-dns" {
138-
proxyList = []string{"nginx"}
139-
} else if proxy == "nginx" {
140-
proxyList = []string{"traefik"}
141-
}
121+
// Show success report.
122+
cgapp.ShowMessage(
123+
"success",
124+
fmt.Sprintf("Web/Proxy server configuration for `%v` was created!", proxy),
125+
false, false,
126+
)
142127

143-
// Delete unused proxy and/or frontend roles.
144-
cgapp.RemoveFolders("roles", proxyList)
128+
/*
129+
The project's Ansible roles part creation.
130+
*/
145131

146-
// Success messages.
147-
cgapp.ShowMessage(
148-
"success",
149-
fmt.Sprintf("Web/Proxy server configuration for `%v` was created!", proxy),
150-
false, false,
151-
)
152-
cgapp.ShowMessage(
153-
"success",
154-
"Ansible inventory, playbook and roles for deploying was created!",
155-
false, false,
156-
)
132+
// Copy Ansible roles from embedded file system.
133+
if err := cgapp.CopyFromEmbeddedFS(
134+
&cgapp.EmbeddedFileSystem{
135+
Name: registry.EmbedRoles,
136+
RootFolder: "roles",
137+
SkipDir: false,
138+
},
139+
); err != nil {
140+
return cgapp.ShowError(err.Error())
157141
}
158142

143+
// Show success report.
144+
cgapp.ShowMessage(
145+
"success",
146+
"Ansible inventory, playbook and roles for deploying was created!",
147+
false, false,
148+
)
149+
159150
/*
160151
The project's misc files part creation.
161152
*/
@@ -171,6 +162,23 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
171162
return cgapp.ShowError(err.Error())
172163
}
173164

165+
/*
166+
Cleanup project.
167+
*/
168+
169+
// Set unused proxy roles.
170+
if proxy == "traefik" || proxy == "traefik-acme-dns" {
171+
proxyList = []string{"nginx"}
172+
} else if proxy == "nginx" {
173+
proxyList = []string{"traefik"}
174+
} else {
175+
proxyList = []string{"traefik", "nginx"}
176+
}
177+
178+
// Delete unused roles and backend files.
179+
cgapp.RemoveFolders("roles", proxyList)
180+
cgapp.RemoveFolders("backend", []string{".git", ".github"})
181+
174182
// Stop timer.
175183
stopTimer := cgapp.CalculateDurationTime(startTimer)
176184
cgapp.ShowMessage(
@@ -180,13 +188,11 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
180188
)
181189

182190
// Ending messages.
183-
if proxy != "none" {
184-
cgapp.ShowMessage(
185-
"",
186-
"* Please put credentials into the Ansible inventory file (`hosts.ini`) before you start deploying a project!",
187-
false, false,
188-
)
189-
}
191+
cgapp.ShowMessage(
192+
"",
193+
"* Please put credentials into the Ansible inventory file (`hosts.ini`) before you start deploying a project!",
194+
false, false,
195+
)
190196
if frontend != "none" {
191197
cgapp.ShowMessage(
192198
"",

pkg/registry/defaults.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
// CLIVersion version of Create Go App CLI.
14-
const CLIVersion string = "2.0.0"
14+
const CLIVersion string = "2.1.0"
1515

1616
// Variables struct for Ansible variables (inventory, hosts).
1717
type Variables struct {
@@ -103,6 +103,11 @@ var (
103103

104104
// AnsibleInventoryVariables list of variables for inventory.
105105
AnsibleInventoryVariables = map[string]*Variables{
106+
"none": {
107+
List: map[string]interface{}{
108+
"Proxy": "none",
109+
},
110+
},
106111
"traefik": {
107112
List: map[string]interface{}{
108113
"Proxy": "traefik",
@@ -124,6 +129,11 @@ var (
124129

125130
// AnsiblePlaybookVariables list of variables for playbook.
126131
AnsiblePlaybookVariables = map[string]*Variables{
132+
"none": {
133+
List: map[string]interface{}{
134+
"Proxy": "none",
135+
},
136+
},
127137
"traefik": {
128138
List: map[string]interface{}{
129139
"Proxy": "traefik",

pkg/registry/roles/backend/tasks/main.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# Build backend Docker container.
2626
#
2727
- name: Build Docker image for backend
28-
docker_image:
28+
community.docker.docker_image:
2929
name: cgapp_backend # name of the backend image
3030
build:
3131
path: "{{ server_dir }}/backend" # folder with Dockerfile
@@ -36,7 +36,7 @@
3636
# Run backend container (for Traefik Proxy).
3737
#
3838
- name: Run Docker container with backend (for Traefik Proxy)
39-
docker_container:
39+
community.docker.docker_container:
4040
name: cgapp-backend # name of the backend container
4141
image: cgapp_backend:latest
4242
restart_policy: unless-stopped
@@ -55,7 +55,7 @@
5555
# Run backend container (for Nginx).
5656
#
5757
- name: Run Docker container with backend (for Nginx)
58-
docker_container:
58+
community.docker.docker_container:
5959
name: cgapp-backend # name of the backend container
6060
image: cgapp_backend:latest
6161
restart_policy: unless-stopped

pkg/registry/roles/docker/tasks/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,5 @@
5353
# Create a new Docker network for connect all project elements into one network.
5454
#
5555
- name: Create a new Docker network
56-
docker_network:
56+
community.docker.docker_network:
5757
name: "{{ docker_network }}"

pkg/registry/roles/nginx/tasks/main.yml

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,38 @@
4040
mode: 0600
4141

4242
#
43-
# Run official Nginx Docker container with specified version.
43+
# Run official Nginx Docker container (for HTTP only) with specified version.
4444
#
45-
- name: Run Nginx container
46-
docker_container:
45+
- name: Run Nginx container (for HTTP)
46+
community.docker.docker_container:
4747
name: cgapp-nginx
4848
image: "nginx:{{ nginx_version }}"
4949
restart_policy: unless-stopped
5050
recreate: true
5151
networks:
5252
- name: "{{ docker_network }}"
5353
ports:
54-
- ["80:80", "{{ '443:443' if nginx_use_only_https == 'yes' }}"]
54+
- "80:80"
5555
volumes:
5656
- "{{ server_dir }}/webserver/nginx.conf:/etc/nginx/nginx.conf:ro"
5757
- "{{ server_dir }}/webserver/default.conf:/etc/nginx/conf.d/default.conf:ro"
58+
when: nginx_use_only_https == 'no'
59+
60+
#
61+
# Run official Nginx Docker container (for HTTPS only) with specified version.
62+
#
63+
- name: Run Nginx container (for HTTPS only)
64+
community.docker.docker_container:
65+
name: cgapp-nginx
66+
image: "nginx:{{ nginx_version }}"
67+
restart_policy: unless-stopped
68+
recreate: true
69+
networks:
70+
- name: "{{ docker_network }}"
71+
ports:
72+
- "80:80"
73+
- "443:443"
74+
volumes:
75+
- "{{ server_dir }}/webserver/nginx.conf:/etc/nginx/nginx.conf:ro"
76+
- "{{ server_dir }}/webserver/default.conf:/etc/nginx/conf.d/default.conf:ro"
77+
when: nginx_use_only_https == 'yes'
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Ansible role for deploy the PostgreSQL for the Create Go App project.
2+
# Author: Vic Shóstak <[email protected]> (https://shostak.dev)
3+
# For more information, please visit https://create-go.app/
4+
5+
---
6+
#
7+
# Create folder for PostgreSQL database.
8+
#
9+
- name: Ensures PostgreSQL dir exists
10+
file:
11+
state: directory
12+
path: "{{ server_dir }}/database"
13+
owner: "{{ server_user }}"
14+
group: "{{ server_group }}"
15+
16+
#
17+
# Run official PostgreSQL Docker container with specified version.
18+
#
19+
- name: Run PostgreSQL container
20+
community.docker.docker_container:
21+
name: cgapp-postgres
22+
image: "postgres:{{ postgres_version }}"
23+
restart_policy: unless-stopped
24+
recreate: true
25+
networks:
26+
- name: "{{ docker_network }}"
27+
ports:
28+
- "{{ postgres_port }}:{{ postgres_port }}"
29+
volumes:
30+
- "{{ server_dir }}/database/data/:/var/lib/postgresql/data"
31+
32+
#
33+
# Make DB migration.
34+
#
35+
- name: Make DB migration
36+
community.docker.docker_container:
37+
name: cgapp-db-migration
38+
image: "migrate/migrate"
39+
recreate: true
40+
networks:
41+
- name: "{{ docker_network }}"
42+
command:
43+
[
44+
"-path",
45+
"/migrations",
46+
"-database",
47+
"postgres://{{ postgres_user }}:{{ postgres_password }}@localhost:{{ postgres_port }}/{{ postgres_db }}?sslmode={{ postgres_ssl_mode }}",
48+
"up",
49+
"{{ migrate_number }}",
50+
]
51+
volumes:
52+
- "{{ server_dir }}/backend/platform/migrations/:/migrations"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Ansible role for deploy the Redis for the Create Go App project.
2+
# Author: Vic Shóstak <[email protected]> (https://shostak.dev)
3+
# For more information, please visit https://create-go.app/
4+
5+
---
6+
#
7+
# Create folder for Redis cache.
8+
#
9+
- name: Ensures Redis dir exists
10+
file:
11+
state: directory
12+
path: "{{ server_dir }}/cache"
13+
owner: "{{ server_user }}"
14+
group: "{{ server_group }}"
15+
16+
#
17+
# Run official Redis Docker container with specified version.
18+
#
19+
- name: Run Redis container
20+
community.docker.docker_container:
21+
name: cgapp-redis
22+
image: "redis:{{ redis_version }}"
23+
restart_policy: unless-stopped
24+
recreate: true
25+
networks:
26+
- name: "{{ docker_network }}"
27+
ports:
28+
- "{{ redis_port }}:{{ redis_port }}"
29+
volumes:
30+
- "{{ server_dir }}/cache/data/:/data"

0 commit comments

Comments
 (0)