-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectadmin.go
82 lines (69 loc) · 2.04 KB
/
directadmin.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
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
timeout time.Duration
url string
}
type apiGenericResponse struct {
Error string `json:"error,omitempty"`
Result string `json:"result,omitempty"`
Success string `json:"success,omitempty"`
}
type apiGenericResponseN struct {
Message string `json:"message"`
Type string `json:"type"`
}
// TODO: implement caching layer which can be enabled/disabled on New()
// essentially, for domains it'd have map[string]Domain at the API level
// then if any user called from the API, it would check the cache first
// would either need a cache lifetime field added to domains, or
// add an additional map for lifetime checks
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,
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
}