-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
148 lines (118 loc) · 2.99 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
package app
import (
"dockside/app/logger"
"dockside/app/util/json2"
"errors"
"github.com/maddalax/htmgo/framework/service"
"github.com/nats-io/nats.go"
"slices"
"strings"
"time"
)
type ServerPutOpts struct {
Id string `json:"id"`
Name string `json:"name"`
LocalIpAddress string `json:"ip_address"`
RemoteIpAddress string `json:"remote_ip_address"`
HostName string `json:"host_name"`
LastSeen time.Time `json:"last_seen"`
Os string `json:"os"`
}
func ServerPut(locator *service.Locator, opts ServerPutOpts) error {
client := service.Get[KvClient](locator)
if opts.Id == "" {
return errors.New("server id is required")
}
var server *Server
server, err := ServerGet(locator, opts.Id)
if err != nil {
if errors.Is(err, nats.ErrKeyNotFound) {
server = nil
} else {
logger.ErrorWithFields("Error getting server", err, map[string]interface{}{
"id": opts.Id,
})
return err
}
}
// server exists already
if server != nil {
// server id might have changed, update it
server.Id = opts.Id
server.LocalIpAddress = opts.LocalIpAddress
server.RemoteIpAddress = opts.RemoteIpAddress
server.HostName = opts.HostName
server.LastSeen = opts.LastSeen
server.Os = opts.Os
if opts.Name != "" {
server.Name = opts.Name
}
} else {
server = &Server{
Id: opts.Id,
LocalIpAddress: opts.LocalIpAddress,
RemoteIpAddress: opts.RemoteIpAddress,
Name: opts.Name,
HostName: opts.HostName,
LastSeen: opts.LastSeen,
Os: opts.Os,
}
logger.InfoWithFields("Creating new server", map[string]interface{}{
"id": opts.Id,
"host_name": opts.HostName,
})
}
bucket, err := client.GetOrCreateBucket(&nats.KeyValueConfig{
Bucket: "servers",
})
if err != nil {
return err
}
err = client.PutJson(bucket, server.Id, server)
if err != nil {
return err
}
return nil
}
func ServerGet(locator *service.Locator, id string) (*Server, error) {
client := service.Get[KvClient](locator)
bucket, err := client.GetOrCreateBucket(&nats.KeyValueConfig{
Bucket: "servers",
})
if err != nil {
return nil, err
}
server, err := bucket.Get(id)
if err != nil || server == nil {
return nil, err
}
return json2.Deserialize[Server](server.Value())
}
func ServerList(locator *service.Locator) ([]*Server, error) {
client := service.Get[KvClient](locator)
bucket, err := client.GetOrCreateBucket(&nats.KeyValueConfig{
Bucket: "servers",
})
if err != nil {
return nil, err
}
var servers []*Server
keys, err := bucket.ListKeys()
if err != nil {
return nil, err
}
for s := range keys.Keys() {
server, err := ServerGet(locator, s)
if err != nil {
logger.ErrorWithFields("Error getting server", err, map[string]interface{}{
"id": s,
})
continue
}
servers = append(servers, server)
}
slices.SortFunc(servers, func(a, b *Server) int {
return strings.Compare(a.HostName, b.HostName)
})
return servers, nil
}