Skip to content

Commit ad4b942

Browse files
authored
Merge pull request #108 from nimblehq/feature/105-add-mock-server-add-on
[#105] [Part3] Add OpenAPI to support creating API documentation with OpenAPI
2 parents 768bdb9 + b0da17a commit ad4b942

File tree

8 files changed

+141
-5
lines changed

8 files changed

+141
-5
lines changed

cmd/create_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,4 +1142,74 @@ var _ = Describe("Create template", func() {
11421142
Expect(content).To(ContainSubstring(expectedContent))
11431143
})
11441144
})
1145+
1146+
Context("given mock_server add-on", func() {
1147+
It("contains deploy_mock_server.yml file", func() {
1148+
cookiecutter := tests.Cookiecutter{
1149+
AppName: "test-gin-templates",
1150+
UseMockServer: tests.Yes,
1151+
}
1152+
cookiecutter.CreateProjectFromGinTemplate(currentTemplatePath)
1153+
_, err := os.Stat(".github/workflows/deploy_mock_server.yml")
1154+
1155+
Expect(os.IsNotExist(err)).To(BeFalse())
1156+
})
1157+
1158+
It("contains Dockerfile.mock file", func() {
1159+
cookiecutter := tests.Cookiecutter{
1160+
AppName: "test-gin-templates",
1161+
UseMockServer: tests.Yes,
1162+
}
1163+
cookiecutter.CreateProjectFromGinTemplate(currentTemplatePath)
1164+
_, err := os.Stat("Dockerfile.mock")
1165+
1166+
Expect(os.IsNotExist(err)).To(BeFalse())
1167+
})
1168+
1169+
It("contains fly.toml file", func() {
1170+
cookiecutter := tests.Cookiecutter{
1171+
AppName: "test-gin-templates",
1172+
UseMockServer: tests.Yes,
1173+
}
1174+
cookiecutter.CreateProjectFromGinTemplate(currentTemplatePath)
1175+
_, err := os.Stat("fly.toml")
1176+
1177+
Expect(os.IsNotExist(err)).To(BeFalse())
1178+
})
1179+
})
1180+
1181+
Context("given NO mock_server add-on", func() {
1182+
It("does NOT contains deploy_mock_server.yml file", func() {
1183+
cookiecutter := tests.Cookiecutter{
1184+
AppName: "test-gin-templates",
1185+
UseMockServer: tests.No,
1186+
}
1187+
cookiecutter.CreateProjectFromGinTemplate(currentTemplatePath)
1188+
_, err := os.Stat(".github/workflows/deploy_mock_server.yml")
1189+
1190+
Expect(os.IsNotExist(err)).To(BeTrue())
1191+
})
1192+
1193+
It("does NOT contains Dockerfile.mock file", func() {
1194+
cookiecutter := tests.Cookiecutter{
1195+
AppName: "test-gin-templates",
1196+
UseMockServer: tests.No,
1197+
}
1198+
cookiecutter.CreateProjectFromGinTemplate(currentTemplatePath)
1199+
_, err := os.Stat("Dockerfile.mock")
1200+
1201+
Expect(os.IsNotExist(err)).To(BeTrue())
1202+
})
1203+
1204+
It("does NOT contains fly.toml file", func() {
1205+
cookiecutter := tests.Cookiecutter{
1206+
AppName: "test-gin-templates",
1207+
UseMockServer: tests.No,
1208+
}
1209+
cookiecutter.CreateProjectFromGinTemplate(currentTemplatePath)
1210+
_, err := os.Stat("fly.toml")
1211+
1212+
Expect(os.IsNotExist(err)).To(BeTrue())
1213+
})
1214+
})
11451215
})

cookiecutter.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"css_addon": ["Bootstrap", "Tailwind", "None"],
55
"use_logrus": ["yes", "no"],
66
"use_heroku": ["yes", "no"],
7+
"use_mock_server": ["yes", "no"],
78

89
"_api_variant": "no",
910
"_web_variant": "no",

hooks/post_gen_project.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ def init_git(message):
109109
remove_file(".spectral.yaml")
110110
remove_file(".github/workflows/lint_docs.yml")
111111

112+
# Remove mock_server if not seleted
113+
if '{{ cookiecutter.use_mock_server }}' == 'no':
114+
print_log('Removing mock_server')
115+
116+
# mock_server related files
117+
remove_file("Dockerfile.mock")
118+
remove_file("fly.toml")
119+
remove_file(".github/workflows/deploy_mock_server.yml")
120+
112121
# Download the missing dependencies
113122
print_log('Downloading dependencies')
114123
subprocess.call(['go', 'mod', 'tidy'])

hooks/pre_gen_project.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,13 @@
2828
{{ cookiecutter.update({ '_tailwind_addon': 'yes' }) }}
2929
{% endif %}
3030
{% endif %}
31+
32+
-----------
33+
Mock Server
34+
cookiecutter.use_mock_server
35+
-----------
36+
Only project with openapi:
37+
{% if cookiecutter._api_variant == 'no' %}
38+
{{ cookiecutter.update({ 'use_mock_server': 'no' }) }}
39+
{% endif %}
3140
'''

tests/cookiecutter.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ const (
2525

2626
// Field and order MUST be the same as cookiecutter.json
2727
type Cookiecutter struct {
28-
AppName string
29-
Variant Variants
30-
CssAddon CssAddons
31-
UseLogrus Choices
32-
UseHeroku Choices
28+
AppName string
29+
Variant Variants
30+
CssAddon CssAddons
31+
UseLogrus Choices
32+
UseHeroku Choices
33+
UseMockServer Choices
3334
}
3435

3536
func (c *Cookiecutter) fillDefaultValue() {
@@ -45,6 +46,10 @@ func (c *Cookiecutter) fillDefaultValue() {
4546
c.UseHeroku = No
4647
}
4748

49+
if c.UseMockServer != Yes && c.UseMockServer != No {
50+
c.UseMockServer = No
51+
}
52+
4853
if c.CssAddon != Bootstrap && c.CssAddon != Tailwind && c.CssAddon != None {
4954
c.CssAddon = None
5055
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Deploy Mock Server
2+
3+
on:
4+
push:
5+
branches:
6+
- develop
7+
8+
jobs:
9+
deploy:
10+
name: Deploy mock server
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v3
15+
16+
- name: Set up flyctl
17+
uses: superfly/flyctl-actions/setup-flyctl@master
18+
19+
- name: Deploy to fly
20+
run: flyctl deploy --remote-only
21+
env:
22+
FLY_API_TOKEN: {{ "${{ secrets.FLY_API_TOKEN }}" }}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM stoplight/prism:latest
2+
3+
COPY /docs /usr/src/prism/packages/cli/docs/
4+
5+
EXPOSE 80
6+
7+
CMD ["mock", "-h", "0.0.0.0", "-p", "80", "docs/openapi/openapi.yml"]

{{cookiecutter.app_name}}/fly.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
app = "api-mock-server"
2+
primary_region = "sin"
3+
4+
[build]
5+
dockerfile = "Dockerfile.mock"
6+
7+
[http_service]
8+
internal_port = 80
9+
force_https = true
10+
auto_stop_machines = true
11+
auto_start_machines = true
12+
min_machines_running = 0
13+
processes = ["app"]

0 commit comments

Comments
 (0)