-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswagger.go
115 lines (112 loc) · 2.85 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
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
package gasync
import (
"fmt"
"net/url"
"github.com/gorchestrate/async"
)
func SwaggerDoc(baseurl string, wfName string, wf func() async.WorkflowState) (interface{}, error) {
url, err := url.Parse(baseurl)
if err != nil {
return nil, err
}
definitions := map[string]interface{}{}
endpoints := map[string]interface{}{}
docs := map[string]interface{}{
"definitions": definitions,
"swagger": "2.0",
"info": map[string]interface{}{
"title": wfName,
"version": "0.0.1",
"description": `<img src="` + baseurl + `/graph/pizza?format=svg" style="width:400px;" />`,
},
"host": url.Host,
"basePath": "/",
"schemes": []string{url.Scheme},
"paths": endpoints,
}
endpoints["/wf/"+wfName+"/{id}"] = map[string]interface{}{
"post": map[string]interface{}{
"consumes": []string{"application/json"},
"produces": []string{"application/json"},
"tags": []string{wfName},
"parameters": []map[string]interface{}{
{
"name": "id",
"in": "path",
"description": "workflow id",
"required": true,
"type": "string",
},
},
"responses": map[string]interface{}{
"200": map[string]interface{}{
"description": "success",
},
},
},
}
var oErr error
_, err = async.Walk(wf().Definition(), func(s async.Stmt) bool {
switch x := s.(type) {
case async.WaitEventsStmt:
for _, v := range x.Cases {
h, ok := v.Handler.(*async.ReflectEvent)
if !ok {
continue
}
in, out, err := h.Schemas()
if err != nil {
oErr = err
panic(err)
}
for name, def := range in.Definitions {
definitions[name] = def
}
for name, def := range out.Definitions {
definitions[name] = def
}
endpoints["/wf/"+wfName+"/{id}/"+v.Callback.Name] = map[string]interface{}{
"post": map[string]interface{}{
"consumes": []string{"application/json"},
"produces": []string{"application/json"},
"tags": []string{wfName},
"parameters": []map[string]interface{}{
{
"name": "id",
"in": "path",
"description": "workflow id",
"required": true,
"type": "string",
},
{
"name": "body",
"in": "body",
"description": "event data",
"required": true,
"schema": map[string]interface{}{
"$ref": in.Ref,
},
},
},
"responses": map[string]interface{}{
"200": map[string]interface{}{
"description": "success",
"schema": map[string]interface{}{
"$ref": out.Ref,
},
},
},
},
}
}
}
return false
})
if err != nil {
return nil, fmt.Errorf("err swaggering workflow: %v", wfName)
}
if oErr != nil {
return nil, fmt.Errorf("err during swaggering workflow: %v", wfName)
}
return docs, nil
}