-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.go
152 lines (135 loc) · 3.48 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
)
type apiConfig struct {
ListenAddress string `json:"listenAddress"`
Backend backendConfig `json:"backendConfig"`
MonitoringConfig monitoringConfiguration `json:"monitoringConfig"`
}
type api struct {
pipelineManager *pipelineManager
Router *mux.Router
httpServer *http.Server
mService monitoringService
apiReady bool
}
// Start starts the API server and blocks
func (a *api) Start(config apiConfig) {
a.pipelineManager = &pipelineManager{
backendConfig: config.Backend,
}
err := a.pipelineManager.Init()
if err != nil {
log.Fatal(err)
}
a.Router = mux.NewRouter()
a.httpServer = &http.Server{
Addr: config.ListenAddress,
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: a.Router,
}
a.mService, err = config.MonitoringConfig.init(a.Router)
if err != nil {
log.Fatal(err)
}
a.Router.Path("/pipelines/{id}").Methods("GET").HandlerFunc(a.GetPipelines)
a.Router.Path("/pipelines").Methods("POST").HandlerFunc(a.CreatePipeline)
go func(a *api) {
err = a.httpServer.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}(a)
a.apiReady = true
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
for sig := range c {
log.Infof("Received %s signal... exiting\n", sig)
break
}
a.Shutdown()
}
// Shutdown the API Server
func (a *api) Shutdown() {
log.Info("Shutting down API Server")
a.httpServer.Shutdown(context.Background())
}
// GetPipelines gets a list of Pipelines
func (a *api) GetPipelines(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
pipelineID := vars["id"]
// Validation of incoming variable?
pipeline, err := a.pipelineManager.Get([]byte(pipelineID))
if err != nil {
w.WriteHeader(500)
// Should probably wrap that err
w.Write([]byte(err.Error()))
}
log.Debugf("Response from pipeline manager for %s: %s", pipelineID, pipeline)
if len(pipeline) == 0 {
w.WriteHeader(404)
return
}
w.Write(pipeline)
}
// CreatePipeline creates a new Pipeline and returns the UUID
func (a *api) CreatePipeline(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Errorln("Error reading body", err)
w.WriteHeader(400)
w.Write([]byte(err.Error()))
return
}
if len(body) == 0 {
log.Errorln("Empty body received")
w.WriteHeader(400)
w.Write([]byte("No pipeline config received"))
return
}
log.Debugln("Creating pipeline with config", string(body))
pipeline, err := a.pipelineManager.NewPipeline(body, a.mService)
if err != nil {
log.Errorln("Error creating pipeline", err)
w.WriteHeader(400)
w.Write([]byte(err.Error()))
return
}
log.Debugln("Created pipeline", pipeline.ID)
err = a.pipelineManager.Store(pipeline)
if err != nil {
log.Errorln("Error storing pipeline", err)
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
go func() {
err := pipeline.StartPipeline()
if err != nil {
log.Errorln("Pipeline failed:", err)
}
}()
a.mService.incrPipelines(pipeline.Name)
w.WriteHeader(201)
w.Write([]byte(pipeline.ID.String()))
}
func parseAPIServerConfig(config []byte) apiConfig {
var c apiConfig
json.Unmarshal(config, &c)
if c.ListenAddress == "" {
c.ListenAddress = ":8000"
}
return c
}