-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
217 lines (184 loc) · 6.34 KB
/
server.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* Copyright (c) 2018 Yamagishi Kazutoshi
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package mirakurun
import (
"bytes"
"context"
"io"
"net/http"
)
// ServerConfig represents a Mirakurun server config.
type ServerConfig struct {
Path string `json:"path,omitempty"`
Port int `json:"port,omitempty"`
DisableIPv6 bool `json:"disableIPv6,omitempty"`
LogLevel int `json:"logLevel,omitempty"`
MaxLogHistory int `json:"maxLogHistory,omitempty"`
HighWaterMark int `json:"highWaterMark,omitempty"`
OverflowTimeLimit int `json:"overflowTimeLimit,omitempty"`
MaxBufferBytesBeforeReady int `json:"maxBufferBytesBeforeReady,omitempty"`
EventEndTimeout int `json:"eventEndTimeout,omitempty"`
ProgramGCInterval int `json:"programGCInterval,omitempty"`
EPGGatheringInterval int `json:"epgGatheringInterval,omitempty"`
EPGRetrievalTime int `json:"epgRetrievalTime,omitempty"`
}
// GetServerConfig fetches a server config.
func (c *Client) GetServerConfig(ctx context.Context) (*ServerConfig, *http.Response, error) {
req, err := c.NewRequest("GET", "config/server", nil)
if err != nil {
return nil, nil, err
}
config := new(ServerConfig)
resp, err := c.Do(ctx, req, config)
if err != nil {
return nil, resp, err
}
return config, resp, nil
}
// UpdateServerConfig updates a server config.
func (c *Client) UpdateServerConfig(ctx context.Context, body *ServerConfig) (*ServerConfig, *http.Response, error) {
req, err := c.NewRequest("PUT", "config/server", body)
if err != nil {
return nil, nil, err
}
config := new(ServerConfig)
resp, err := c.Do(ctx, req, config)
if err != nil {
return nil, resp, err
}
return config, resp, nil
}
// GetLog fetches a log.
func (c *Client) GetLog(ctx context.Context) (*bytes.Buffer, *http.Response, error) {
req, err := c.NewRequest("GET", "log", nil)
if err != nil {
return nil, nil, err
}
buf := new(bytes.Buffer)
resp, err := c.Do(ctx, req, buf)
if err != nil {
return nil, resp, err
}
return buf, resp, nil
}
// GetLogStream fetches a log stream.
func (c *Client) GetLogStream(ctx context.Context) (io.ReadCloser, *http.Response, error) {
return c.requestStream(ctx, "PUT", "log/stream")
}
// Version represents a Mirakurun version.
type Version struct {
Current string `json:"current"`
Latest string `json:"latest"`
}
// CheckVersion fetches a Mirakurun version.
func (c *Client) CheckVersion(ctx context.Context) (*Version, *http.Response, error) {
req, err := c.NewRequest("GET", "version", nil)
if err != nil {
return nil, nil, err
}
version := new(Version)
resp, err := c.Do(ctx, req, version)
if err != nil {
return nil, resp, err
}
return version, resp, nil
}
// VersionUpdateOptions specifies the optional parameters to the Client.UpdateVersion method.
type VersionUpdateOptions struct {
Force bool `url:"force,omitempty"`
}
// UpdateVersion updates a Mirakurun version.
func (c *Client) UpdateVersion(ctx context.Context, opt *VersionUpdateOptions) (io.ReadCloser, *http.Response, error) {
u, err := addOptions("version/update", opt)
if err != nil {
return nil, nil, err
}
req, err := c.NewRequest("PUT", u, nil)
if err != nil {
return nil, nil, err
}
req = req.WithContext(ctx)
resp, err := c.client.Do(req)
if err != nil {
return nil, resp, err
}
return resp.Body, resp, nil
}
// Status represents a Mirakurun status.
type Status struct {
Version string `json:"version"`
Process ProcessStatus `json:"process"`
EPG EPGStatus `json:"epg"`
StreamCount map[string]int `json:"streamCount"`
ErrorCount map[string]int `json:"errorCount"`
TimerAccuracy TimerAccuracyStatus `json:"timerAccuracy"`
}
// ProcessStatus represents a Mirakurun process status.
type ProcessStatus struct {
Arch string `json:"arch"`
Platform string `json:"platform"`
Versions map[string]string `json:"versions"`
PID int `json:"pid"`
MemoryUsage map[string]int `json:"memoryUsage"`
}
// EPGStatus represents a Mirakurun EPG status.
type EPGStatus struct {
GatheringNetworks []int `json:"gatheringNetworks"`
StoredEvents int `json:"storedEvents"`
}
// TimerAccuracyStatus represents a Mirakurun time accuracy status.
type TimerAccuracyStatus struct {
Last float64 `json:"last"`
M1 map[string]float64 `json:"m1"`
M5 map[string]float64 `json:"m5"`
M15 map[string]float64 `json:"m15"`
}
// GetStatus fetches a status.
func (c *Client) GetStatus(ctx context.Context) (*Status, *http.Response, error) {
req, err := c.NewRequest("GET", "status", nil)
if err != nil {
return nil, nil, err
}
status := new(Status)
resp, err := c.Do(ctx, req, status)
if err != nil {
return nil, resp, err
}
return status, resp, nil
}
// RestartResponse represents a Mirakurun restart response.
type RestartResponse struct {
PID int `json:"_cmd_pid"`
}
// Restart a Mirakurun process.
func (c *Client) Restart(ctx context.Context) (*RestartResponse, *http.Response, error) {
req, err := c.NewRequest("PUT", "restart", nil)
if err != nil {
return nil, nil, err
}
restart := new(RestartResponse)
resp, err := c.Do(ctx, req, restart)
if err != nil {
return nil, resp, err
}
return restart, resp, nil
}