This repository has been archived by the owner on Jan 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.go
83 lines (71 loc) · 2.32 KB
/
service.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
package service
import (
"crypto/tls"
"net/http"
"github.com/julienschmidt/httprouter"
newrelic "github.com/newrelic/go-agent"
"github.com/opsee/basic/grpcutil"
opsee "github.com/opsee/basic/service"
"github.com/opsee/basic/tp"
"github.com/opsee/cats/checks/results"
"github.com/opsee/cats/store"
sluice "github.com/opsee/gmunch/client"
log "github.com/opsee/logrus"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
type service struct {
httpServer *http.Server
checkStore store.CheckStore
teamStore store.TeamStore
resultStore results.Store
sluiceClient sluice.Client
newrelicAgent newrelic.Application
}
func New(pgConn string, resultStore results.Store, newrelicAgent newrelic.Application) (*service, error) {
db, err := store.NewPostgres(pgConn)
if err != nil {
return nil, err
}
sluiceClient, err := sluice.New("sluice.in.opsee.com:8443", sluice.Config{
TLSConfig: tls.Config{},
})
if err != nil {
return nil, err
}
svc := &service{
checkStore: store.NewCheckStore(db),
teamStore: store.NewTeamStore(db),
resultStore: resultStore,
sluiceClient: sluiceClient,
newrelicAgent: newrelicAgent,
}
return svc, nil
}
// http / grpc multiplexer for http health checks
func (s *service) StartMux(addr, certfile, certkeyfile string) error {
// The grpc service
server := grpc.NewServer()
opsee.RegisterCatsServer(server, s)
log.Infof("starting cats service at %s", addr)
s.httpServer = &http.Server{
Addr: addr,
Handler: grpcutil.GRPCHandlerFunc(server, s.NewHandler()),
TLSConfig: &tls.Config{},
}
return s.httpServer.ListenAndServeTLS(certfile, certkeyfile)
}
// Returns a new service http.Handler for testing. Sets up an opsee/tp router, which comes with a basic health endpoint for free.
// It's going to have at least another endpoint for stripe webhooks
func (s *service) NewHandler() http.Handler {
router := tp.NewHTTPRouter(context.Background())
router.Handle("POST", "/hooks/stripe", []tp.DecodeFunc{s.httpLogger(), s.stripeHookDecoder()}, s.stripeHookHandler())
return router
}
// HTTP decoder that logs requests
func (s *service) httpLogger() tp.DecodeFunc {
return func(ctx context.Context, rw http.ResponseWriter, r *http.Request, p httprouter.Params) (context.Context, int, error) {
log.Info("http request: ", r.URL.String())
return ctx, 0, nil
}
}