-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathapi.go
60 lines (53 loc) · 1.61 KB
/
api.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
package api
import (
"context"
"fmt"
"net/http"
"time"
"github.com/cybertec-postgresql/pg_timetable/internal/config"
"github.com/cybertec-postgresql/pg_timetable/internal/log"
)
// RestHandler is a common interface describing the current status of a connection
type RestHandler interface {
IsReady() bool
StartChain(context.Context, int) error
StopChain(context.Context, int) error
}
type RestAPIServer struct {
APIHandler RestHandler
l log.LoggerIface
http.Server
}
func Init(opts config.RestAPIOpts, logger log.LoggerIface) *RestAPIServer {
s := &RestAPIServer{
nil,
logger,
http.Server{
Addr: fmt.Sprintf(":%d", opts.Port),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
},
}
http.HandleFunc("/liveness", s.livenessHandler)
http.HandleFunc("/readiness", s.readinessHandler)
http.HandleFunc("/startchain", s.chainHandler)
http.HandleFunc("/stopchain", s.chainHandler)
if opts.Port != 0 {
logger.WithField("port", opts.Port).Info("Starting REST API server...")
go func() { logger.Error(s.ListenAndServe()) }()
}
return s
}
func (Server *RestAPIServer) livenessHandler(w http.ResponseWriter, _ *http.Request) {
Server.l.Debug("Received /liveness REST API request")
w.WriteHeader(http.StatusOK) // i'm serving hence I'm alive
}
func (Server *RestAPIServer) readinessHandler(w http.ResponseWriter, _ *http.Request) {
Server.l.Debug("Received /readiness REST API request")
if Server.APIHandler == nil || !Server.APIHandler.IsReady() {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
w.WriteHeader(http.StatusOK)
}