-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.go
151 lines (130 loc) · 4.11 KB
/
admin.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
149
150
151
package main
import (
"errors"
"fmt"
"net/http"
"sync"
"github.com/labstack/echo/v4"
)
const AdminHost = "admin.cless.cloud"
const AdminPort = 1323
const HostNameTemplate = "%s.cless.cloud"
var ErrServiceNotFound = errors.New("service not found")
var ErrServiceAlreadyExists = errors.New("service already exists")
type ServiceDefinition struct {
Name string `json:"name"`
ImageName string `json:"image_name"`
ImageTag string `json:"image_tag"`
Port int `json:"port"`
Host string `json:"host"`
}
func (sDef *ServiceDefinition) isValid() bool {
return (sDef.Name != "" && sDef.Name != "admin") && sDef.ImageName != "" && sDef.ImageTag != "" && sDef.Port > 0
}
type ServiceDefinitionRepository interface {
GetAll() ([]ServiceDefinition, error)
GetByName(name string) (*ServiceDefinition, error)
Create(service ServiceDefinition) error
Update(service ServiceDefinition) error
}
type InMemoryServiceDefinitionRepository struct {
services map[string]ServiceDefinition
mutex *sync.Mutex
}
func (r *InMemoryServiceDefinitionRepository) GetAll() ([]ServiceDefinition, error) {
r.mutex.Lock()
defer r.mutex.Unlock()
services := make([]ServiceDefinition, 0)
for _, service := range r.services {
services = append(services, service)
}
return services, nil
}
func (r *InMemoryServiceDefinitionRepository) GetByName(name string) (*ServiceDefinition, error) {
r.mutex.Lock()
defer r.mutex.Unlock()
service, ok := r.services[name]
if !ok {
return nil, ErrServiceNotFound
}
return &service, nil
}
func (r *InMemoryServiceDefinitionRepository) Create(service ServiceDefinition) error {
r.mutex.Lock()
defer r.mutex.Unlock()
_, ok := r.services[service.Name]
if ok {
return ErrServiceAlreadyExists
}
r.services[service.Name] = service
return nil
}
func (r *InMemoryServiceDefinitionRepository) Update(service ServiceDefinition) error {
r.mutex.Lock()
defer r.mutex.Unlock()
_, ok := r.services[service.Name]
if !ok {
return ErrServiceNotFound
}
r.services[service.Name] = service
return nil
}
type ServiceDefinitionManager struct {
repo ServiceDefinitionRepository
}
func NewServiceDefinitionManager(repo ServiceDefinitionRepository) *ServiceDefinitionManager {
return &ServiceDefinitionManager{
repo: repo,
}
}
func (m *ServiceDefinitionManager) RegisterServiceDefinition(name string, imageName string, imageTag string, port int) error {
service := ServiceDefinition{
Name: name,
ImageName: imageName,
ImageTag: imageTag,
Port: port,
}
service.Host = fmt.Sprintf(HostNameTemplate, name)
return m.repo.Create(service)
}
func (m *ServiceDefinitionManager) ListAllServiceDefinitions() ([]ServiceDefinition, error) {
return m.repo.GetAll()
}
func (m *ServiceDefinitionManager) GetServiceDefinitionByName(name string) (*ServiceDefinition, error) {
return m.repo.GetByName(name)
}
func StartAdminServer(manager *ServiceDefinitionManager) {
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Admin server is running")
})
e.GET("/serviceDefinitions", func(c echo.Context) error {
services, err := manager.ListAllServiceDefinitions()
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, services)
})
e.GET("/serviceDefinitions/:name", func(c echo.Context) error {
name := c.Param("name")
service, err := manager.GetServiceDefinitionByName(name)
if err == ErrServiceNotFound {
return c.String(http.StatusNotFound, err.Error())
}
return c.JSON(http.StatusOK, service)
})
e.POST("/serviceDefinitions", func(c echo.Context) error {
service := new(ServiceDefinition)
if err := c.Bind(service); err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
if !service.isValid() {
return c.String(http.StatusBadRequest, "Invalid service definition")
}
if err := manager.RegisterServiceDefinition(service.Name, service.ImageName, service.ImageTag, service.Port); err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
return c.String(http.StatusCreated, "Service definition created")
})
e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", AdminPort)))
}