-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstages.go
81 lines (66 loc) · 2.17 KB
/
stages.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
package api
import (
"net/http"
"github.com/nanobox-io/slurp/core"
)
// for whatever reason, these need to be exported so json.[un]marshal can utilize it
type build struct {
OldId string `json:"old-id"` // build to fetch from storage
NewId string `json:"new-id"` // build to stage and store
}
type auth struct {
AuthSecret string `json:"secret"`
}
// addStage prepares a directory for receiving the new build. If an old build is specified,
// that build is fetched from hoarder, otherwise a new directory is created.
func addStage(rw http.ResponseWriter, req *http.Request) {
var stage build
err := parseBody(req, &stage)
if err != nil {
writeBody(rw, req, apiError{err.Error()}, http.StatusBadRequest)
return
}
if stage.NewId == "" {
writeBody(rw, req, apiError{"Missing Payload Data"}, http.StatusInternalServerError)
return
}
// stage the build
err = slurp.AddStage(stage.OldId, stage.NewId)
if err != nil {
writeBody(rw, req, apiError{err.Error()}, http.StatusInternalServerError)
return
}
writeBody(rw, req, auth{stage.NewId}, http.StatusOK)
}
// commitStage is called once the local build is synced with the staged build. It will
// compress and upload the staged build to hoarder. CommitStage will also remove the
// user for security.
func commitStage(rw http.ResponseWriter, req *http.Request) {
// PUT /stages/{buildId}
buildId := req.URL.Query().Get(":buildId")
// commit the staged build
err := slurp.CommitStage(buildId)
if err != nil {
writeBody(rw, req, apiError{err.Error()}, http.StatusInternalServerError)
return
}
// delete the staged build
err = slurp.DeleteStage(buildId)
if err != nil {
writeBody(rw, req, apiError{err.Error()}, http.StatusInternalServerError)
return
}
writeBody(rw, req, apiMsg{"Success"}, http.StatusOK)
}
// deleteStage removes the staged build directory
func deleteStage(rw http.ResponseWriter, req *http.Request) {
// DELETE /stages/{buildId}
buildId := req.URL.Query().Get(":buildId")
// delete the staged build
err := slurp.DeleteStage(buildId)
if err != nil {
writeBody(rw, req, apiError{err.Error()}, http.StatusInternalServerError)
return
}
writeBody(rw, req, apiMsg{"Success"}, http.StatusOK)
}