blob: 2d2fb6d9b41592324cc0e25c6283208b82285e90 [file] [log] [blame]
David K. Bainbridgef0da8732016-06-01 16:15:37 -07001package main
2
David K. Bainbridge546cdc32016-06-29 15:30:22 -07003import (
4 "fmt"
5 "net/url"
6 "strings"
7)
8
David K. Bainbridgef0da8732016-06-01 16:15:37 -07009type Storage interface {
10 Put(id string, update StatusMsg) error
11 Get(id string) (*StatusMsg, error)
David K. Bainbridge068e87d2016-06-30 13:53:19 -070012 Delete(id string) error
David K. Bainbridgef0da8732016-06-01 16:15:37 -070013 List() ([]StatusMsg, error)
14}
15
David K. Bainbridge546cdc32016-06-29 15:30:22 -070016func NewStorage(spec string) (Storage, error) {
17 conn, err := url.Parse(spec)
18 if err != nil {
19 return nil, err
20 }
21
22 switch strings.ToUpper(conn.Scheme) {
23 case "MEMORY":
24 return NewMemoryStorage(), nil
25 case "CONSUL":
26 return NewConsulStorage(spec)
27 default:
28 return nil, fmt.Errorf("Unknown storage scheme specified, '%s'", conn.Scheme)
29 }
30}
31
David K. Bainbridgef0da8732016-06-01 16:15:37 -070032type MemoryStorage struct {
33 Data map[string]StatusMsg
34}
35
36func NewMemoryStorage() *MemoryStorage {
37 return &MemoryStorage{
38 Data: make(map[string]StatusMsg),
39 }
40}
41
42func (s *MemoryStorage) Put(id string, update StatusMsg) error {
43 s.Data[id] = update
David K. Bainbridgef0da8732016-06-01 16:15:37 -070044 return nil
45}
46
47func (s *MemoryStorage) Get(id string) (*StatusMsg, error) {
48 m, ok := s.Data[id]
49 if !ok {
50 return nil, nil
51 }
52 return &m, nil
53}
54
David K. Bainbridge068e87d2016-06-30 13:53:19 -070055func (s *MemoryStorage) Delete(id string) error {
56 delete(s.Data, id)
57 return nil
58}
59
David K. Bainbridgef0da8732016-06-01 16:15:37 -070060func (s *MemoryStorage) List() ([]StatusMsg, error) {
61 r := make([]StatusMsg, len(s.Data))
62 i := 0
63 for _, v := range s.Data {
64 r[i] = v
David K. Bainbridge97ee8052016-06-14 00:52:07 -070065 i += 1
David K. Bainbridgef0da8732016-06-01 16:15:37 -070066 }
67 return r, nil
68}