-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.go
59 lines (49 loc) · 1.28 KB
/
lib.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
// this package 'flaarum' is the golang library for communicating with the flaarum server.
package flaarum
import (
"crypto/tls"
"fmt"
"io"
"net/http"
"net/url"
)
var httpCl *http.Client
func init() {
config := &tls.Config{InsecureSkipVerify: true}
tr := &http.Transport{TLSClientConfig: config}
httpCl = &http.Client{Transport: tr}
}
type Client struct {
Addr string
KeyStr string
ProjName string
}
func NewClient(ip, keyStr, projName string) Client {
return Client{"https://" + ip + ":22318/", keyStr, projName}
}
// Used whenever you changed the default port
func NewClientCustomPort(ip, keyStr, projName string, port int) Client {
return Client{"https://" + ip + fmt.Sprintf(":%d/", port), keyStr, projName}
}
func (cl *Client) Ping() error {
urlValues := url.Values{}
urlValues.Set("key-str", cl.KeyStr)
resp, err := httpCl.PostForm(cl.Addr+"is-flaarum", urlValues)
if err != nil {
return retError(10, err.Error())
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return retError(10, err.Error())
}
if resp.StatusCode == 200 {
if string(body) == "yeah-flaarum" {
return nil
} else {
return retError(10, "Unexpected Error in confirming that the server is a flaarum store.")
}
} else {
return retError(10, string(body))
}
}