wip: grpc api
Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>
This commit is contained in:
parent
1dd748e3f2
commit
2eba8d6511
174 changed files with 22012 additions and 11410 deletions
22
datastore/memory/memory.go
Normal file
22
datastore/memory/memory.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package memory
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/ehazlett/element/api/types"
|
||||
"github.com/ehazlett/element/proxy"
|
||||
)
|
||||
|
||||
type Memory struct {
|
||||
proxy map[string]*proxy.Config
|
||||
service map[string]*types.Service
|
||||
m sync.Mutex
|
||||
}
|
||||
|
||||
func NewMemory() (*Memory, error) {
|
||||
return &Memory{
|
||||
proxy: map[string]*proxy.Config{},
|
||||
service: map[string]*types.Service{},
|
||||
m: sync.Mutex{},
|
||||
}, nil
|
||||
}
|
28
datastore/memory/proxy.go
Normal file
28
datastore/memory/proxy.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
package memory
|
||||
|
||||
import "github.com/ehazlett/element/proxy"
|
||||
|
||||
func (m *Memory) SaveProxyConfig(id string, cfg *proxy.Config) error {
|
||||
m.m.Lock()
|
||||
m.proxy[id] = cfg
|
||||
m.m.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Memory) DeleteProxyConfig(id string) error {
|
||||
m.m.Lock()
|
||||
if _, exists := m.proxy[id]; exists {
|
||||
delete(m.proxy, id)
|
||||
}
|
||||
m.m.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Memory) GetProxyConfigs() ([]*proxy.Config, error) {
|
||||
c := []*proxy.Config{}
|
||||
for _, v := range m.proxy {
|
||||
c = append(c, v)
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
49
datastore/memory/service.go
Normal file
49
datastore/memory/service.go
Normal file
|
@ -0,0 +1,49 @@
|
|||
package memory
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/ehazlett/element/api/types"
|
||||
)
|
||||
|
||||
func (m *Memory) SaveService(service *types.Service) error {
|
||||
m.m.Lock()
|
||||
m.service[service.ID] = service
|
||||
m.m.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Memory) DeleteService(id string) error {
|
||||
m.m.Lock()
|
||||
if _, exists := m.service[id]; exists {
|
||||
delete(m.service, id)
|
||||
}
|
||||
m.m.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Memory) GetServices() ([]*types.Service, error) {
|
||||
s := []*types.Service{}
|
||||
for _, v := range m.service {
|
||||
s = append(s, v)
|
||||
}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (m *Memory) GetServiceByHost(host string) (*types.Service, error) {
|
||||
services, err := m.GetServices()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, service := range services {
|
||||
for _, h := range service.Hosts {
|
||||
if strings.Index(h, host) > -1 {
|
||||
return service, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue