forked from kyma-project/kyma
/
application.go
68 lines (58 loc) · 2.09 KB
/
application.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
package application
import (
"log"
"net/http"
"github.com/kyma-project/kyma/components/event-bus/internal/sv"
"github.com/kyma-project/kyma/components/event-bus/internal/sv/opts"
"k8s.io/client-go/tools/cache"
)
// SubscriptionValidatorApplication ...
type SubscriptionValidatorApplication struct {
eaController *ea.EventActivationsController
subController *ea.SubscriptionsController
ServerMux *http.ServeMux
}
// NewSubscriptionValidatorApplication ...
// func NewSubscriptionValidatorApplication(eaOpts *ea.Options, informer ...cache.SharedIndexInformer) *SubscriptionValidatorApplication {
func NewSubscriptionValidatorApplication(svOpts *opts.Options, informer ...cache.SharedIndexInformer) *SubscriptionValidatorApplication {
log.Println("Subscription Validator :: Initializing application")
var eaController *ea.EventActivationsController
if len(informer) > 0 {
eaController = ea.StartEventActivationsControllerWithInformer(informer[0])
} else {
eaController = ea.StartEventActivationsController()
}
subController := ea.StartSubscriptionsController()
serveMux := http.NewServeMux()
serveMux.Handle("/v1/status/live", statusLiveHandler(eaController))
serveMux.Handle("/v1/status/ready", statusLiveHandler(eaController))
return &SubscriptionValidatorApplication{
eaController: eaController,
subController: subController,
ServerMux: serveMux,
}
}
// Stop ...
func (a *SubscriptionValidatorApplication) Stop() {
a.eaController.Stop()
a.subController.Stop()
}
func statusLiveHandler(controller *ea.EventActivationsController) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if controller != nil && controller.IsRunning() {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusBadGateway)
}
})
}
// TODO more complex readness tests
func statusReadyHandler(controller *ea.EventActivationsController) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if controller != nil && controller.IsRunning() {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusBadGateway)
}
})
}