forked from cschleiden/go-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diag.go
128 lines (105 loc) · 2.81 KB
/
diag.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
package diag
import (
"embed"
"encoding/json"
"io/fs"
"net/http"
"strconv"
"strings"
)
//go:embed app/build
var embeddedFiles embed.FS
// NewServeMux returns an *http.ServeMux that serves the diagnostics web app at / and the diagnostics API at /api which is
// used by the web app.
func NewServeMux(backend Backend) *http.ServeMux {
mux := http.NewServeMux()
// API
mux.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
// Only support GET requests
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
relativeURL := strings.TrimPrefix(r.URL.Path, "/api/")
// /api/
if relativeURL == "" {
// Index
query := r.URL.Query()
count := 25
countStr := query.Get("count")
if countStr != "" {
var err error
count, err = strconv.Atoi(countStr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
}
instances, err := backend.GetWorkflowInstances(r.Context(), query.Get("after"), count)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(instances); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
return
}
segments := strings.Split(relativeURL, "/")
// /api/{instanceID}
if len(segments) == 1 {
instanceID := segments[0]
instance, err := backend.GetWorkflowInstance(r.Context(), instanceID)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
if instance == nil {
w.WriteHeader(http.StatusNotFound)
return
}
history, err := backend.GetWorkflowInstanceHistory(r.Context(), instance.Instance, nil)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
newHistory := make([]*Event, 0)
for _, event := range history {
newHistory = append(newHistory, &Event{
ID: event.ID,
SequenceID: event.SequenceID,
Type: event.Type.String(),
Timestamp: event.Timestamp,
ScheduleEventID: event.ScheduleEventID,
Attributes: event.Attributes,
VisibleAt: event.VisibleAt,
})
}
result := &WorkflowInstanceInfo{
WorkflowInstanceRef: instance,
History: newHistory,
}
w.Header().Add("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(result); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
return
}
})
// App
mux.Handle("/", http.FileServer(getFileSystem()))
return mux
}
func getFileSystem() http.FileSystem {
// Get the build subdirectory as the
// root directory so that it can be passed
// to the http.FileServer
fsys, err := fs.Sub(embeddedFiles, "app/build")
if err != nil {
panic(err)
}
return http.FS(fsys)
}