Skip to content

Commit 768bdb9

Browse files
authored
Merge pull request #107 from nimblehq/feature/105-add-endpoint-for-openapi-docs
[#105] [Part 2] Add OpenAPI to support creating API documentation with OpenAPI
2 parents 897237a + 17addfa commit 768bdb9

File tree

9 files changed

+90
-4
lines changed

9 files changed

+90
-4
lines changed

cmd/create_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,5 +1107,39 @@ var _ = Describe("Create template", func() {
11071107

11081108
Expect(content).To(ContainSubstring(expectedContent))
11091109
})
1110+
1111+
It("contains lib/api/docs folder", func() {
1112+
cookiecutter := tests.Cookiecutter{
1113+
AppName: "test-gin-templates",
1114+
}
1115+
cookiecutter.CreateProjectFromGinTemplate(currentTemplatePath)
1116+
_, err := os.Stat("lib/api/docs")
1117+
1118+
Expect(os.IsNotExist(err)).To(BeFalse())
1119+
})
1120+
1121+
It("contains openapi requirement in go.mod", func() {
1122+
cookiecutter := tests.Cookiecutter{
1123+
AppName: "test-gin-templates",
1124+
}
1125+
cookiecutter.CreateProjectFromGinTemplate(currentTemplatePath)
1126+
content := tests.ReadFile("go.mod")
1127+
1128+
expectedContent := "github.com/gin-gonic/contrib"
1129+
1130+
Expect(content).To(ContainSubstring(expectedContent))
1131+
})
1132+
1133+
It("contains openapi requirement in router.go", func() {
1134+
cookiecutter := tests.Cookiecutter{
1135+
AppName: "test-gin-templates",
1136+
}
1137+
cookiecutter.CreateProjectFromGinTemplate(currentTemplatePath)
1138+
content := tests.ReadFile("bootstrap/router.go")
1139+
1140+
expectedContent := "apidocsrouter.CombineRoutes(r)"
1141+
1142+
Expect(content).To(ContainSubstring(expectedContent))
1143+
})
11101144
})
11111145
})

hooks/post_gen_project.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def init_git(message):
101101

102102
# docs folder
103103
remove_files("docs")
104+
remove_files("lib/api")
104105

105106
# openapi related files
106107
remove_file(".dockerignore")
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
package bootstrap
22

33
import (
4+
{%- if cookiecutter._api_variant == "yes" -%}apidocsrouter "github.com/nimblehq/{{cookiecutter.app_name}}/lib/api/docs/routers"{%- endif %}
45
apiv1router "github.com/nimblehq/{{cookiecutter.app_name}}/lib/api/v1/routers"
56
{% if cookiecutter._web_variant == "yes" %}webrouter "github.com/nimblehq/{{cookiecutter.app_name}}/lib/web/routers"
67
{% endif %}
8+
9+
{%- if cookiecutter._api_variant == "yes" -%}"github.com/gin-gonic/contrib/static"{%- endif %}
710
"github.com/gin-gonic/gin"
811
)
912

1013
func SetupRouter() *gin.Engine {
1114
r := gin.Default()
1215

13-
apiv1router.ComebineRoutes(r)
14-
{% if cookiecutter._web_variant == "yes" %}webrouter.ComebineRoutes(r)
16+
{% if cookiecutter._api_variant == "yes" -%}r.Use(static.Serve("/", static.LocalFile("./public", true))){%- endif %}
17+
18+
apiv1router.CombineRoutes(r)
19+
{% if cookiecutter._api_variant == "yes" -%}apidocsrouter.CombineRoutes(r){%- endif %}
20+
{% if cookiecutter._web_variant == "yes" %}webrouter.CombineRoutes(r)
1521
{% endif %}
1622
return r
1723
}

{{cookiecutter.app_name}}/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ require (
1111
gorm.io/driver/postgres v1.0.8
1212
gorm.io/gorm v1.20.12
1313
{% if cookiecutter.use_logrus == "yes" -%}github.com/sirupsen/logrus v1.8.1{%- endif %}
14+
{% if cookiecutter._api_variant == "yes" -%}github.com/gin-gonic/contrib v0.0.0-20221130124618-7e01895a63f2{%- endif %}
1415
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package controllers
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/gin-gonic/gin"
7+
)
8+
9+
type OpenAPIController struct{}
10+
11+
func (OpenAPIController) Show(c *gin.Context) {
12+
c.HTML(http.StatusOK, "show.html", gin.H{})
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package routers
2+
3+
import (
4+
"github.com/nimblehq/{{cookiecutter.app_name}}/lib/api/docs/controllers"
5+
6+
"github.com/gin-gonic/gin"
7+
)
8+
9+
func CombineRoutes(engine *gin.Engine) {
10+
docs := engine.Group("/api/docs")
11+
12+
engine.LoadHTMLFiles("lib/api/docs/views/openapi/show.html")
13+
docs.GET("/openapi", controllers.OpenAPIController{}.Show)
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!doctype html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
7+
<title>API documentation</title>
8+
<!-- Embed Elements via Web Component -->
9+
<script src="https://unpkg.com/@stoplight/elements/web-components.min.js"></script>
10+
<link rel="stylesheet" href="https://unpkg.com/@stoplight/elements/styles.min.css">
11+
</head>
12+
13+
<body>
14+
<elements-api apiDescriptionUrl="/openapi.yml" router="hash" layout="sidebar" />
15+
</body>
16+
17+
</html>

{{cookiecutter.app_name}}/lib/api/v1/routers/router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/gin-gonic/gin"
77
)
88

9-
func ComebineRoutes(engine *gin.Engine) {
9+
func CombineRoutes(engine *gin.Engine) {
1010
v1 := engine.Group("/api/v1")
1111

1212
v1.GET("/health", controllers.HealthController{}.HealthStatus)

{{cookiecutter.app_name}}/lib/web/routers/router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
eztemplate "github.com/michelloworld/ez-gin-template"
1111
)
1212

13-
func ComebineRoutes(engine *gin.Engine) {
13+
func CombineRoutes(engine *gin.Engine) {
1414
// Register HTML renderer
1515
htmlRender := eztemplate.New()
1616
htmlRender.Debug = gin.IsDebugging()

0 commit comments

Comments
 (0)