-
Notifications
You must be signed in to change notification settings - Fork 0
/
swagger.go
63 lines (49 loc) · 1.16 KB
/
swagger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package controllers
import (
"io"
"net/http"
"os"
"path"
"github.com/labstack/echo/v4"
)
type SwaggerApi struct {
}
func AttachSwaggerApi(g *echo.Group) *echo.Group {
c := &SwaggerApi{}
g.GET("", c.Get)
g.GET("/openapi.yml", c.GetDocument)
return g
}
func (el SwaggerApi) Get(e echo.Context) error {
basePath, err := os.Getwd()
if err != nil {
return e.HTML(http.StatusNotFound, "Not found")
}
index := path.Join(basePath, "assets", "swagger.html")
f, err := os.Open(index)
if err != nil {
return e.HTML(http.StatusNotFound, "Not found")
}
html, err := io.ReadAll(f)
if err != nil {
return e.HTML(http.StatusNotFound, "Not found")
}
return e.HTML(200, string(html))
}
func (el SwaggerApi) GetDocument(e echo.Context) error {
NotFountError := "Not found"
basePath, err := os.Getwd()
if err != nil {
return e.HTML(http.StatusNotFound, NotFountError)
}
index := path.Join(basePath, "docs", "swagger.yml")
f, err := os.Open(index)
if err != nil {
return e.HTML(http.StatusNotFound, NotFountError)
}
data, err := io.ReadAll(f)
if err != nil {
return e.HTML(http.StatusNotFound, NotFountError)
}
return e.String(200, string(data))
}