/
requestHandler.go
113 lines (92 loc) · 3.39 KB
/
requestHandler.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
package service
import (
"net/http"
"net/url"
"time"
"github.com/patrickmn/go-cache"
"github.com/go-ocf/cloud/cloud2cloud-connector/events"
"github.com/go-ocf/cloud/cloud2cloud-connector/store"
"github.com/go-ocf/cloud/cloud2cloud-connector/uri"
"github.com/go-ocf/kit/log"
projectionRA "github.com/go-ocf/cloud/resource-aggregate/cqrs/projection"
router "github.com/gorilla/mux"
pbAS "github.com/go-ocf/cloud/authorization/pb"
pbRA "github.com/go-ocf/cloud/resource-aggregate/pb"
)
const linkedCloudIdKey = "linkedCloudId"
const linkedAccountIdKey = "linkedCloudId"
//RequestHandler for handling incoming request
type RequestHandler struct {
originCloud store.LinkedCloud
oauthCallback string
resourceProjection *projectionRA.Projection
store store.Store
asClient pbAS.AuthorizationServiceClient
raClient pbRA.ResourceAggregateClient
provisionCache *cache.Cache
subManager *SubscribeManager
}
func logAndWriteErrorResponse(err error, statusCode int, w http.ResponseWriter) {
log.Errorf("%v", err)
w.Header().Set(events.ContentTypeKey, "text/plain")
w.WriteHeader(statusCode)
w.Write([]byte(err.Error()))
}
//NewRequestHandler factory for new RequestHandler
func NewRequestHandler(
originCloud store.LinkedCloud,
oauthCallback string,
subManager *SubscribeManager,
asClient pbAS.AuthorizationServiceClient,
raClient pbRA.ResourceAggregateClient,
resourceProjection *projectionRA.Projection,
store store.Store,
) *RequestHandler {
return &RequestHandler{
originCloud: originCloud,
oauthCallback: oauthCallback,
subManager: subManager,
asClient: asClient,
raClient: raClient,
resourceProjection: resourceProjection,
store: store,
provisionCache: cache.New(5*time.Minute, 10*time.Minute),
}
}
func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Debugf("%v %v", r.Method, r.RequestURI)
// Call the next handler, which can be another middleware in the chain, or the final handler.
next.ServeHTTP(w, r)
})
}
func healthCheck(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
// NewHTTP returns HTTP server
func NewHTTP(requestHandler *RequestHandler) *http.Server {
r := router.NewRouter()
r.Use(loggingMiddleware)
// health check
r.HandleFunc("/", healthCheck).Methods("GET")
s := r.PathPrefix(uri.LinkedClouds).Subrouter()
// retrieve all linked clouds
s.HandleFunc("", requestHandler.RetrieveLinkedClouds).Methods("GET")
// add linked cloud
s.HandleFunc("", requestHandler.AddLinkedCloud).Methods("POST")
// delete linked cloud
s.HandleFunc("/{"+linkedCloudIdKey+"}", requestHandler.DeleteLinkedCloud).Methods("DELETE")
s = r.PathPrefix(uri.LinkedAccounts).Subrouter()
// add linked account
s.HandleFunc("", requestHandler.AddLinkedAccount).Methods("GET")
// retrieve all linked accounts
s.HandleFunc("/retrieve", requestHandler.RetrieveLinkedAccounts).Methods("GET")
// delete linked cloud
s.HandleFunc("/{"+linkedAccountIdKey+"}", requestHandler.DeleteLinkedAccount).Methods("DELETE")
// notify linked cloud
r.HandleFunc(uri.NotifyLinkedAccount, requestHandler.NotifyLinkedAccount).Methods("POST")
// OAuthCallback
oauthURL, _ := url.Parse(requestHandler.oauthCallback)
r.HandleFunc(oauthURL.Path, requestHandler.OAuthCallback).Methods("GET")
return &http.Server{Handler: r}
}