-
Notifications
You must be signed in to change notification settings - Fork 1
/
services.go
50 lines (40 loc) · 1.38 KB
/
services.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
package proxy
import (
"fmt"
"github.com/forsam-education/cerberus/models"
"github.com/forsam-education/cerberus/utils"
"github.com/spf13/viper"
"time"
)
// MemoryCachedService represents a service cached in hot memory with an expiration.
type MemoryCachedService struct {
Service *models.Service
ExpirationTime time.Time
}
// IsExpired returns true if the cache has expired, false otherwise.
func (service *MemoryCachedService) IsExpired() bool {
return service.ExpirationTime.After(time.Now())
}
var services = make(map[string]MemoryCachedService)
// LoadServices gets services from the database and sets them to the services variable.
func LoadServices() error {
utils.Logger.Info("Loading services from database...", nil)
rawServices, err := models.Services().AllG()
if err != nil {
return err
}
for _, service := range rawServices {
utils.Logger.Info(fmt.Sprintf("Loaded service %s - %s - %s", service.Name, service.ServicePath, service.TargetHost), nil)
services[service.ServicePath] = MemoryCachedService{
Service: service,
ExpirationTime: time.Now().Add(viper.GetDuration(utils.ServiceHotMemoryTTL) * time.Second),
}
}
return nil
}
func cacheService(service *models.Service) {
services[service.ServicePath] = MemoryCachedService{
Service: service,
ExpirationTime: time.Now().Add(viper.GetDuration(utils.ServiceHotMemoryTTL) * time.Second),
}
}