-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
167 lines (146 loc) · 3.22 KB
/
api.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package apidoc
import (
"bytes"
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"github.com/go-openapi/spec"
"github.com/go-openapi/swag"
"github.com/hopeio/cherry/utils/log"
)
var Doc *spec.Swagger
// 参数为路径和格式
func GetDoc(path string) *spec.Swagger {
if Doc != nil {
return Doc
}
targetPath := "." + string(os.PathSeparator) + "swagger.json"
if path != "" {
targetPath = path
} else {
return generate()
}
realPath, err := filepath.Abs(targetPath)
if err != nil {
log.Error(err)
}
apiType := filepath.Ext(targetPath)
if _, err := os.Stat(realPath); os.IsNotExist(err) {
return generate()
} else {
file, err := os.Open(realPath)
if err != nil {
log.Error(err)
}
defer file.Close()
data, err := ioutil.ReadAll(file)
if err != nil {
log.Error(err)
}
/*var buf bytes.Buffer
err = json.Compact(&buf, data)
if err != nil {
ulog.Error(err)
}*/
if apiType == ".json" {
err = json.Unmarshal(data, &Doc)
if err != nil {
log.Error(err)
}
} else {
/*var v map[string]interface{}//子类型 json: unsupported type: map[interface{}]interface{}
//var v interface{} //json: unsupported type: map[interface{}]interface{}
err = yaml.Unmarshal(data, &v)
b, err := json.Marshal(&v)
if err != nil {
ulog.Error(err)
}
json.Unmarshal(b, &Doc)*/
trimmed := bytes.TrimSpace(data)
if len(trimmed) > 0 {
if trimmed[0] != '{' && trimmed[0] != '[' {
yml, err := swag.BytesToYAMLDoc(trimmed)
if err != nil {
log.Error(err)
}
d, err := swag.YAMLToJSON(yml)
if err != nil {
log.Error(err)
}
if err = json.Unmarshal(d, &Doc); err != nil {
log.Error(err)
}
}
}
}
}
return Doc
}
func generate() *spec.Swagger {
Doc = new(spec.Swagger)
info := new(spec.Info)
Doc.Info = info
Doc.Swagger = "2.0"
Doc.Paths = new(spec.Paths)
Doc.Definitions = make(spec.Definitions)
info.Title = "Title"
info.Description = "Description"
info.Version = "0.01"
info.TermsOfService = "TermsOfService"
var contact spec.ContactInfo
contact.Name = "Contact Name"
contact.Email = "Contact Mail"
contact.URL = "Contact URL"
info.Contact = &contact
var license spec.License
license.Name = "License Name"
license.URL = "License URL"
info.License = &license
Doc.Host = "localhost:80"
Doc.BasePath = "/"
Doc.Schemes = []string{"http", "https"}
Doc.Consumes = []string{"application/json"}
Doc.Produces = []string{"application/json"}
return Doc
}
func WriteToFile(realPath, modName string) {
if Doc == nil {
return
}
if realPath == "" {
realPath = "."
}
realPath = realPath + modName
err := os.MkdirAll(realPath, os.ModePerm)
if err != nil {
log.Error(err)
}
realPath = filepath.Join(realPath, modName+EXT)
if _, err := os.Stat(realPath); err == nil {
os.Remove(realPath)
}
var file *os.File
file, err = os.Create(realPath)
if err != nil {
log.Error(err)
}
defer file.Close()
enc := json.NewEncoder(file)
enc.SetIndent("", " ")
err = enc.Encode(Doc)
if err != nil {
log.Error(err)
}
/*b, err := yaml.Marshal(swag.ToDynamicJSON(Doc))
if err != nil {
log.Error(err)
}
if _, err := file.Write(b); err != nil {
log.Error(err)
}*/
Doc = nil
}
func NilDoc() {
Doc = nil
}