-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmikrus.go
216 lines (195 loc) · 5.38 KB
/
mikrus.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
// Package mikrus is a client library for Mikrus VPS provider.
package mikrus
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io"
"net/http"
"net/url"
"strings"
"time"
)
// Client represents Mikrus client.
type Client struct {
apiKey string
serverID string
URL string
HTTPClient *http.Client
}
// New creates and returns new Mikrus client.
func New(apiKey, srvID string) Client {
return Client{
apiKey: apiKey,
serverID: srvID,
URL: "https://api.mikr.us",
HTTPClient: &http.Client{
Timeout: 10 * time.Second,
},
}
}
// Info returns information about server associated with the API Key and ServerID.
func (c *Client) Info() (Server, error) {
res := Server{}
if err := c.callAPI("info", &res); err != nil {
return Server{}, err
}
return res, nil
}
// Servers returns short information about all servers associated
// with the API Key and ServerID.
func (c *Client) Servers() (Servers, error) {
servers := Servers{}
if err := c.callAPI("serwery", &servers); err != nil {
return Servers{}, err
}
return servers, nil
}
// Logs returns lats 10 log entries from the server associated
// with the API Key and ServerID.
func (c *Client) Logs() (Logs, error) {
logs := Logs{}
if err := c.callAPI("logs", &logs); err != nil {
return Logs{}, err
}
return logs, nil
}
func (c *Client) callAPI(verb string, res any) error {
requestURL := c.URL + "/" + verb
val := url.Values{
"key": []string{c.apiKey},
"srv": []string{c.serverID},
}
resp, err := c.HTTPClient.PostForm(requestURL, val)
if err != nil {
return err
}
defer resp.Body.Close()
respBytes, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("reading response body: %w", err)
}
resp.Body.Close()
respString := string(respBytes)
resp.Body = io.NopCloser(strings.NewReader(respString))
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected response status %d: %q", resp.StatusCode, respString)
}
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
return fmt.Errorf("decoding error for %q: %w", respString, err)
}
return nil
}
// ServerShort represents short server description.
type ServerShort struct {
ServerID string `json:"server_id"`
ServerName string `json:"server_name"`
Expires string `json:"expires"`
ParamRam string `json:"param_ram"`
ParamDisk string `json:"param_disk"`
}
const serversTemplate = `{{ range . }}
Server ID: {{ .ServerID }}
Server name: {{ .ServerName }}
Expiration date: {{ .Expires }}
RAM size: {{ .ParamRam }}
ParamDisk: {{ .ParamDisk }}
{{ end }}`
// Servers is a list of servers in a short form.
type Servers []ServerShort
// String implements stringer interface.
func (s Servers) String() string {
out, err := render(serversTemplate, s)
if err != nil {
return fmt.Sprintln(err.Error())
}
return out
}
// Server represents information about Mirkus server.
type Server struct {
ServerID string `json:"server_id"`
ServerName string `json:"server_name,omitempty"`
Expires string `json:"expires"`
ExpiresCytrus string `json:"expires_cytrus,omitempty"`
ExpiresStorage string `json:"expires_storage,omitempty"`
ParamRam string `json:"param_ram"`
ParamDisk string `json:"param_disk"`
LastLogPanel string `json:"lastlog_panel"`
MikrusPro string `json:"mikrus_pro"`
}
const serverTemplate = `ServerID: {{ .ServerID }}
Server name: {{ .ServerName }}
Expiration date: {{ .Expires }}
Cytrus expiration date: {{ .ExpiresCytrus }}
Storage expiration date: {{ .ExpiresStorage }}
RAM size: {{ .ParamRam }}
Disk size: {{ .ParamDisk }}
Last log time: {{ .LastLogPanel }}
Is Pro service: {{ .MikrusPro | toEng }}`
// String implements stringer interface.
func (s Server) String() string {
out, err := render(serverTemplate, s)
if err != nil {
return fmt.Sprintln(err.Error())
}
return out
}
// Log represents a server log information.
type Log struct {
ID string `json:"id"`
ServerID string `json:"server_id"`
Task string `json:"task"`
WhenCreated string `json:"when_created"`
WhenDone string `json:"when_done"`
Output string `json:"output"`
}
const logsTemplate = `{{ range .}}
ID: {{ .ID }}
Server ID: {{ .ServerID }}
Task: {{ .Task | toEng }}
Created: {{ .WhenCreated }}
Done: {{ .WhenDone }}
Output: {{ .Output | cleanup | toEng }}
{{ end }}`
// Logs represents a list of server logs.
type Logs []Log
// String implements stringer interface.
func (l Logs) String() string {
out, err := render(logsTemplate, l)
if err != nil {
return fmt.Sprintln(err.Error())
}
return out
}
// render takes a template and a data value, and returns
// the string result of executing the template.
func render(templateName string, value any) (string, error) {
tmpl, err := template.New("").Funcs(template.FuncMap{"cleanup": cleanup, "toEng": toEng}).Parse(templateName)
if err != nil {
return "", err
}
var output bytes.Buffer
err = tmpl.Execute(&output, value)
if err != nil {
return "", err
}
return output.String(), nil
}
// cleanup makes a log line a one-liner.
func cleanup(logLine string) string {
r := strings.NewReplacer("\n", " ", "+", "")
return r.Replace(logLine)
}
// toEng translates from Polish to English.
//
// This is a temp solution before applying a proper
// localisation to the entire program.
func toEng(s string) string {
r := strings.NewReplacer(
"nie", "no",
"Wrzuciłem klucz SSH", "Uploaded SSH key",
"kluczssh", "sshkey",
)
return r.Replace(s)
}