-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
179 lines (156 loc) · 5.11 KB
/
http.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"fmt"
"io/ioutil"
"net/http"
"sort"
"strconv"
"time"
"github.com/antongulenko/golib"
"github.com/gin-gonic/gin"
"github.com/kballard/go-shellquote"
log "github.com/sirupsen/logrus"
)
const (
DefaultNewPipelineDelay = 200 * time.Millisecond
NewPipelineDelayQuery = "delay"
NewPipelineParametersQuery = "params"
)
func (engine *SubprocessEngine) ServeHttp(endpoint string) error {
g := golib.NewGinEngine()
g.GET("/ping", engine.servePing)
g.GET("/info", engine.serveInfo)
g.GET("/capabilities", engine.serveCapabilities)
g.GET("/pipelines", engine.servePipelines)
g.GET("/running", engine.serveRunningPipelines)
g.POST("/pipeline", engine.serveNewPipeline)
g.GET("/pipeline/:id", engine.serveGetPipeline)
g.GET("/pipeline/:id/out", engine.serveGetPipelineOutput)
g.DELETE("/pipeline/:id", engine.serveKillPipeline)
return g.Run(endpoint)
}
func (engine *SubprocessEngine) replyString(c *gin.Context, code int, format string, args ...interface{}) {
c.Status(code)
c.Writer.WriteString(fmt.Sprintf(format+"\n", args...))
}
func (engine *SubprocessEngine) servePing(c *gin.Context) {
engine.replyString(c, http.StatusOK, "pong")
}
func (engine *SubprocessEngine) serveInfo(c *gin.Context) {
c.JSON(http.StatusOK, engine.getInfo())
}
func (engine *SubprocessEngine) serveCapabilities(c *gin.Context) {
c.JSON(http.StatusOK, engine.capabilities)
}
func (engine *SubprocessEngine) serveFilteredPipelineIds(c *gin.Context, accept func(*RunningPipeline) bool) {
engine.pipelinesLock.Lock()
response := make([]int, 0, len(engine.pipelines))
for _, pipe := range engine.pipelines {
if accept(pipe) {
response = append(response, pipe.Id)
}
}
engine.pipelinesLock.Unlock()
sort.Ints(response)
c.JSON(http.StatusOK, response)
}
func (engine *SubprocessEngine) servePipelines(c *gin.Context) {
engine.serveFilteredPipelineIds(c, func(*RunningPipeline) bool {
return true
})
}
func (engine *SubprocessEngine) serveRunningPipelines(c *gin.Context) {
engine.serveFilteredPipelineIds(c, func(pipe *RunningPipeline) bool {
return pipe.Status == StatusRunning
})
}
func (engine *SubprocessEngine) pipelineResponse(pipe *RunningPipeline) interface{} {
// TODO maybe don't serve the entire internal struct?
return pipe
}
func (engine *SubprocessEngine) serveNewPipeline(c *gin.Context) {
defer func() {
err := c.Request.Body.Close()
if err != nil {
log.Warnln("Error closing POST request body:", err)
}
}()
script, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
engine.replyString(c, http.StatusInternalServerError, "Failed to read request body: "+err.Error())
return
}
if len(script) == 0 {
engine.replyString(c, http.StatusBadRequest, "Provide the Bitflow script for the new pipeline as the POST request body.")
return
}
delay := DefaultNewPipelineDelay
if delayStr := c.Query(NewPipelineDelayQuery); delayStr != "" {
parsedDelay, err := time.ParseDuration(delayStr)
if err != nil {
engine.replyString(c, http.StatusBadRequest, "The parameter '%v' could not be parsed to a duration: %v. Example format: 500ms",
NewPipelineDelayQuery, err)
return
}
delay = parsedDelay
}
var extraParams []string
if extraParamString := c.Query(NewPipelineParametersQuery); extraParamString != "" {
extraParamsSplit, err := shellquote.Split(extraParamString)
if err != nil {
engine.replyString(c, http.StatusBadRequest, "The parameter '%v' could not be split into shell parameters: %v. The quote syntax must follow /bin/sh.",
NewPipelineParametersQuery, err)
return
}
extraParams = extraParamsSplit
}
pipeline, err := engine.NewPipeline(string(script), delay, extraParams)
if err != nil {
engine.replyString(c, http.StatusPreconditionFailed, "Error starting pipeline %v: %v", pipeline.Id, err.Error())
} else {
c.JSON(http.StatusCreated, engine.pipelineResponse(pipeline))
}
}
func (engine *SubprocessEngine) serveGetPipeline(c *gin.Context) {
pipe := engine.getPipeline(c)
if pipe != nil {
c.JSON(http.StatusOK, engine.pipelineResponse(pipe))
}
}
func (engine *SubprocessEngine) serveGetPipelineOutput(c *gin.Context) {
pipe := engine.getPipeline(c)
if pipe != nil {
out, err := pipe.GetOutput()
if err == nil {
c.Status(http.StatusOK)
c.Writer.Write(out)
} else {
engine.replyString(c, http.StatusInternalServerError, "Error obtaining output of pipeline %v", pipe.Id)
}
}
}
func (engine *SubprocessEngine) serveKillPipeline(c *gin.Context) {
pipe := engine.getPipeline(c)
if pipe != nil {
err := pipe.Kill()
if err != nil {
engine.replyString(c, http.StatusInternalServerError, "Error killing pipeline %v: %v", pipe.Id, err)
} else {
c.JSON(http.StatusOK, pipe)
}
}
}
func (engine *SubprocessEngine) getPipeline(c *gin.Context) *RunningPipeline {
idStr := c.Param("id")
id, err := strconv.Atoi(idStr)
if err != nil {
engine.replyString(c, http.StatusBadRequest, "Failed to parse parameter '%v' to int: %v", idStr, err)
}
engine.pipelinesLock.Lock()
pipeline, exists := engine.pipelines[id]
engine.pipelinesLock.Unlock()
if !exists {
engine.replyString(c, http.StatusNotFound, "Pipeline does not exist: "+idStr)
}
return pipeline
}