-
Notifications
You must be signed in to change notification settings - Fork 5
/
proxy.go
87 lines (72 loc) · 1.79 KB
/
proxy.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
package braintree
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"path/filepath"
hhttp "github.com/harrowio/harrow/http"
"github.com/harrowio/harrow/stores"
braintreeAPI "github.com/lionelbarrow/braintree-go"
)
var (
ErrCacheMiss = errors.New("braintree: cache miss")
ErrCacheInternal = errors.New("braintree: cache internal error")
)
type Cache interface {
Clear()
// Entries returns all cache entries and ErrCacheMiss if the
// cache is currently empty.
Entries() ([]*braintreeAPI.Plan, error)
// Set sets the entries in the cache.
Set(entries []*braintreeAPI.Plan) error
}
type Proxy struct {
cache Cache
client stores.Braintree
}
func NewProxy(cache Cache, apiClient stores.Braintree) *Proxy {
return &Proxy{
cache: cache,
client: apiClient,
}
}
func (self *Proxy) FindAllPlans() ([]*braintreeAPI.Plan, error) {
plans, err := self.cache.Entries()
if err == ErrCacheMiss {
plans, err = self.client.FindAllPlans()
if err != nil {
return nil, err
}
if err := self.cache.Set(plans); err != nil {
return nil, err
}
}
return self.cache.Entries()
}
func (self *Proxy) ClearCache() {
self.cache.Clear()
}
func (self *Proxy) ServeHTTP(w http.ResponseWriter, req *http.Request) {
action := fmt.Sprintf("%s %s",
req.Method,
filepath.Clean(req.URL.Path),
)
switch action {
case "DELETE /cache":
self.ClearCache()
case "GET /":
plans, err := self.FindAllPlans()
if err != nil {
self.respondWithJSON(w, hhttp.NewError(http.StatusInternalServerError, "internal", err.Error()))
} else {
self.respondWithJSON(w, plans)
}
default:
self.respondWithJSON(w, hhttp.ErrNotFound)
}
}
func (self *Proxy) respondWithJSON(w http.ResponseWriter, thing interface{}) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(thing)
}