forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
123 lines (100 loc) · 2.6 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
package diagnostic
import (
"errors"
"fmt"
"net/http"
"strings"
"github.com/influxdata/kapacitor/services/httpd"
"github.com/influxdata/kapacitor/uuid"
)
const (
sessionsPath = "/logs"
)
type Diagnostic interface {
CreatedLogSession(id uuid.UUID, contentType string, tags []tag)
DeletedLogSession(id uuid.UUID, contentType string, tags []tag)
}
type SessionService struct {
diag Diagnostic
routes []httpd.Route
SessionsStore SessionsStore
HTTPDService interface {
AddPreviewRoutes([]httpd.Route) error
DelRoutes([]httpd.Route)
}
}
func NewSessionService() *SessionService {
return &SessionService{
SessionsStore: &sessionsStore{
sessions: make(map[uuid.UUID]*Session),
},
}
}
func (s *SessionService) SetDiagnostic(d Diagnostic) {
s.SessionsStore.SetDiagnostic(d)
}
func (s *SessionService) Close() error {
if s.HTTPDService == nil {
return errors.New("must set HTTPDService")
}
s.HTTPDService.DelRoutes(s.routes)
return nil
}
func (s *SessionService) Open() error {
s.routes = []httpd.Route{
{
Method: "GET",
Pattern: sessionsPath,
HandlerFunc: s.handleSessions,
// NoGzip is true so that clients don't have to specify
// the header "Accept-Encoding: identity"
NoGzip: true,
// Data returned is not necessarily JSON. Server
// sets "Content-Type" appropriately.
NoJSON: true,
},
}
if s.HTTPDService == nil {
return errors.New("must set HTTPDService")
}
if err := s.HTTPDService.AddPreviewRoutes(s.routes); err != nil {
return fmt.Errorf("failed to add routes: %v", err)
}
return nil
}
func (s *SessionService) NewLogger() *sessionsLogger {
return &sessionsLogger{
store: s.SessionsStore,
}
}
func (s *SessionService) handleSessions(w http.ResponseWriter, r *http.Request) {
params := r.URL.Query()
tags := []tag{}
var level Level
for k, v := range params {
if len(v) != 1 {
httpd.HttpError(w, "query params cannot contain duplicate params", true, http.StatusBadRequest)
return
}
if k == "lvl" && strings.HasSuffix(v[0], "+") {
level = logLevelFromName(strings.TrimSuffix(v[0], "+"))
continue
}
tags = append(tags, tag{key: k, value: v[0]})
}
acceptedContentType := r.Header.Get("Accept")
var contentType string
switch acceptedContentType {
case "application/json":
contentType = acceptedContentType
default:
contentType = "application/logfmt"
}
header := w.Header()
header.Add("Transfer-Encoding", "chunked")
header.Add("Content-Type", contentType)
w.WriteHeader(http.StatusOK)
session := s.SessionsStore.Create(w, contentType, level, tags)
defer s.SessionsStore.Delete(session)
<-r.Context().Done()
}