-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathknife4gf.go
132 lines (115 loc) · 3.45 KB
/
knife4gf.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package knife4gf
import (
"context"
"fmt"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
"github.com/iasuma/knife4gf/internal/service"
"github.com/iasuma/knife4gf/packed"
"time"
)
// Knife4gf is the struct for swagger feature management.
type Knife4gf struct {
Info SwaggerInfo // Swagger information.
Schemes []string // Supported schemes of the swagger API like "http", "https".
Host string // The host of the swagger APi like "127.0.0.1", "www.mydomain.com"
BasicPath string // The URI for the swagger API like "/", "v1", "v2".
BasicAuthUser string `c:"user"` // HTTP basic authentication username.
BasicAuthPass string `c:"pass"` // HTTP basic authentication password.
}
// SwaggerInfo is the information field for swagger.
type SwaggerInfo struct {
Title string // Title of the swagger API.
Version string // Version of the swagger API.
TermsOfService string // As the attribute name.
Description string // Detail description of the swagger API.
}
const (
Name = "knife4gf"
Author = "[email protected]"
Version = "v1.0.0"
Description = "knife4gf is knife4j for GoFrame GoFrame project. https://github.com/iasuma/knife4gf"
MaxAuthAttempts = 10 // Max authentication count for failure try.
AuthFailedInterval = time.Minute // Authentication retry interval after last failed.
)
const (
docPath = "/kdoc"
)
// Name returns the name of the plugin.
func (kf *Knife4gf) Name() string {
return Name
}
// Author returns the author of the plugin.
func (kf *Knife4gf) Author() string {
return Author
}
// Version returns the version of the plugin.
func (kf *Knife4gf) Version() string {
return Version
}
// Description returns the description of the plugin.
func (kf *Knife4gf) Description() string {
return Description
}
// Install installs the swagger to server as a plugin.
// It implements the interface ghttp.Plugin.
func (kf *Knife4gf) Install(s *ghttp.Server) error {
var (
ctx = gctx.New()
//oai = s.GetOpenApi()
)
packed.Init()
// Retrieve the configuration map and assign it to swagger object.
m := g.Cfg().MustGet(ctx, "swagger").Map()
if m != nil {
if err := gconv.Struct(m, kf); err != nil {
s.Logger().Fatal(ctx, err)
}
}
var kdocPath string
kdocPath = g.Cfg().MustGet(ctx, "server.docPath").String()
if kdocPath == "" {
kdocPath = docPath
}
// The swagger resource files are served as static file service.
s.AddStaticPath(kdocPath, "resource/swagger")
s.SetRewrite(kdocPath+"/api.json", "/api.json")
s.BindHandler(kdocPath+"/services", func(r *ghttp.Request) {
var (
err error
)
content := service.ApiServices(s)
err = r.Response.WriteJsonExit(content)
if err != nil {
s.Logger().Error(ctx, err)
}
})
s.Logger().Infof(
ctx,
`knife4gf ui is serving at address: %s%s/`,
kf.getListenAddress(ctx),
kdocPath,
)
return nil
}
// Remove uninstalls swagger feature from server.
func (kf Knife4gf) Remove() error {
return nil
}
func (kf *Knife4gf) getListenAddress(ctx context.Context) string {
var (
array = gstr.SplitAndTrim(g.Cfg().MustGet(ctx, "server.address").String(), ":")
host = `127.0.0.1`
port = 0
)
if len(array) > 1 {
host = array[0]
port = gconv.Int(array[1])
} else {
port = gconv.Int(array[0])
}
return fmt.Sprintf(`http://%s:%d`, host, port)
}