-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirectadmin.go
More file actions
79 lines (67 loc) · 1.76 KB
/
directadmin.go
File metadata and controls
79 lines (67 loc) · 1.76 KB
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
package directadmin
import (
"errors"
"net/http"
"net/url"
"sync"
"time"
)
const (
AccountRoleAdmin = "admin"
AccountRoleReseller = "reseller"
AccountRoleUser = "user"
)
type API struct {
cache struct {
domains map[string]Domain // Domain name is key.
domainsMutex *sync.Mutex
emailAccounts map[string]EmailAccount // Domain name is key.
emailAccountsMutex *sync.Mutex
packages map[string]Package // Package name is key.
packagesMutex *sync.Mutex
users map[string]User // Username is key.
usersMutex *sync.Mutex
}
cacheEnabled bool
debug bool
httpClient *http.Client
parsedURL *url.URL
url string
}
type (
apiGenericResponse struct {
Error string `json:"error,omitempty"`
Result string `json:"result,omitempty"`
Success string `json:"success,omitempty"`
}
apiGenericResponseNew struct {
Message string `json:"message"`
Type string `json:"type"`
}
)
func New(serverURL string, timeout time.Duration, cacheEnabled bool, debug bool) (*API, error) {
parsedURL, err := url.ParseRequestURI(serverURL)
if err != nil {
return nil, err
}
if parsedURL.Host == "" {
return nil, errors.New("invalid host provided, ensure that the host is a full URL e.g. https://your-ip-address:2222")
}
api := API{
cacheEnabled: cacheEnabled,
debug: debug,
parsedURL: parsedURL,
url: parsedURL.String(),
}
if cacheEnabled {
api.cache.domains = make(map[string]Domain)
api.cache.emailAccounts = make(map[string]EmailAccount)
api.cache.packages = make(map[string]Package)
api.cache.users = make(map[string]User)
}
api.httpClient = &http.Client{Timeout: timeout}
return &api, nil
}
func (a *API) GetURL() string {
return a.url
}