-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
148 lines (131 loc) · 3.69 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
package middleware
import (
"github.com/gin-gonic/gin"
"github.com/hopeio/cherry/utils/net/http/api/apidoc"
"net/http"
"reflect"
"strings"
"github.com/go-openapi/spec"
reflecti "github.com/hopeio/cherry/utils/reflect"
)
func ApiMiddle(ctx *gin.Context) {
currentRouteName := ctx.Request.RequestURI[len(ctx.Request.Method):]
var pathItem *spec.PathItem
doc := apidoc.GetDoc("../swagger.json")
if doc.Paths != nil && doc.Paths.Paths != nil {
if path, ok := doc.Paths.Paths[currentRouteName]; ok {
pathItem = &path
} else {
pathItem = new(spec.PathItem)
}
} else {
doc.Paths = &spec.Paths{Paths: map[string]spec.PathItem{}}
pathItem = new(spec.PathItem)
}
parameters := make([]spec.Parameter, len(ctx.Params), len(ctx.Params))
params := ctx.Params
for i := range params {
key := params[i].Key
//val := params[i].ValueRaw
parameters[i] = spec.Parameter{
ParamProps: spec.ParamProps{
Name: key,
In: "path",
Description: "Description",
},
}
}
if stop, _ := ctx.GetQuery("apidoc"); stop == "stop" {
defer apidoc.WriteToFile("../", "")
}
var res spec.Responses
op := spec.Operation{
OperationProps: spec.OperationProps{
Description: "Description",
Consumes: []string{"application/x-www-form-urlencoded"},
Tags: []string{"Tags"},
Summary: "Summary",
ID: "currentRouteName" + ctx.Request.Method,
Parameters: parameters,
Responses: &res,
},
}
switch ctx.Request.Method {
case http.MethodGet:
pathItem.Get = &op
case http.MethodPost:
pathItem.Post = &op
case http.MethodPut:
pathItem.Put = &op
case http.MethodDelete:
pathItem.Delete = &op
case http.MethodOptions:
pathItem.Options = &op
case http.MethodPatch:
pathItem.Patch = &op
case http.MethodHead:
pathItem.Head = &op
}
doc.Paths.Paths[currentRouteName] = *pathItem
ctx.Next()
}
func DefinitionsApi(definitions map[string]spec.Schema, v interface{}, exclude []string) {
schema := spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"object"},
Properties: make(map[string]spec.Schema),
},
}
body := reflect.TypeOf(v).Elem()
var typ, subFieldName string
for i := 0; i < body.NumField(); i++ {
json := strings.Split(body.Field(i).Tag.Get("json"), ",")[0]
if json == "" || json == "-" {
continue
}
fieldType := body.Field(i).Type
switch fieldType.Kind() {
case reflect.Struct:
typ = "object"
v = reflect.ValueOf(v).Elem().Field(i).Addr().Interface()
subFieldName = fieldType.Name()
case reflect.Ptr:
typ = "object"
v = reflect.New(fieldType.Elem()).Interface()
subFieldName = fieldType.Elem().Name()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
typ = "integer"
case reflect.Array, reflect.Slice:
typ = "array"
v = reflect.New(reflecti.DerefType(fieldType)).Interface()
subFieldName = reflecti.DerefType(fieldType).Name()
case reflect.Float32, reflect.Float64:
typ = "number"
case reflect.String:
typ = "string"
case reflect.Bool:
typ = "boolean"
}
subSchema := spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{typ},
},
}
if typ == "object" {
subSchema.Ref = spec.MustCreateRef("#/definitions/" + subFieldName)
DefinitionsApi(definitions, v, nil)
}
if typ == "array" {
subSchema.Items = new(spec.SchemaOrArray)
subSchema.Items.Schema = &spec.Schema{}
subSchema.Items.Schema.Ref = spec.MustCreateRef("#/definitions/" + subFieldName)
DefinitionsApi(definitions, v, nil)
}
schema.Properties[json] = subSchema
}
definitions[body.Name()] = schema
}
func genSchema(v interface{}) *spec.Schema {
return nil
}