-
Notifications
You must be signed in to change notification settings - Fork 25
/
server.go
113 lines (100 loc) · 2.68 KB
/
server.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 cost_exporter
import (
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"os"
"time"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
componentbaseconfig "k8s.io/component-base/config"
"k8s.io/klog/v2"
"github.com/gocrane/fadvisor/pkg/cost-exporter/cloudcost"
"github.com/gocrane/fadvisor/pkg/util"
)
func NewServer(model cloudcost.CostModel, bind string, debugging componentbaseconfig.DebuggingConfiguration) *Server {
return &Server{
model: model,
bind: bind,
debugging: debugging,
server: &http.Server{},
}
}
type Server struct {
model cloudcost.CostModel
bind string
server *http.Server
debugging componentbaseconfig.DebuggingConfiguration
}
func (s *Server) RegisterHandlers() {
baseHandler := util.NewBaseHandler("fadvisor", s.debugging)
baseHandler.Handle("/nodes/cost", s.NodesCostHandler())
baseHandler.Handle("/nodes/pricing", s.NodesPriceHandler())
handler := util.BuildHandlerChain(baseHandler, nil, nil)
s.server.Handler = handler
}
func (s *Server) Serve(stopCh <-chan struct{}) <-chan struct{} {
// Shutdown server gracefully.
stoppedCh := make(chan struct{})
go func() {
defer utilruntime.HandleCrash()
defer close(stoppedCh)
<-stopCh
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
_ = s.server.Shutdown(ctx)
cancel()
}()
go func() {
defer utilruntime.HandleCrash()
l, err := net.Listen("tcp", s.bind)
if err != nil {
klog.Errorf("Failed to start server: %v", err)
os.Exit(-1)
}
// block until shutdown or err
err = s.server.Serve(l)
msg := fmt.Sprintf("Stopped listening on %s", l.Addr().String())
select {
case <-stopCh:
klog.Info(msg)
default:
panic(fmt.Sprintf("%s due to error: %v", msg, err))
}
}()
return stoppedCh
}
func (s *Server) NodesCostHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
costs, err := s.model.GetNodesCost()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
return
}
data, err := json.Marshal(costs)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
} else {
_, _ = w.Write(data)
}
})
}
func (s *Server) NodesPriceHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
pricing, err := s.model.GetNodesPricing()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
return
}
data, err := json.Marshal(pricing)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
} else {
_, _ = w.Write(data)
}
})
}