-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
49 lines (42 loc) · 1009 Bytes
/
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
package main
import (
"context"
"encoding/json"
"log"
"net/http"
"path"
"github.com/badgerodon/simulator/builder/builderpb"
"google.golang.org/grpc"
)
func init() {
http.HandleFunc("/api/build", handleAPIBuild)
}
func handleAPIBuild(w http.ResponseWriter, r *http.Request) {
importPath := r.URL.Query().Get("import_path")
branch := r.URL.Query().Get("branch")
if branch == "" {
branch = "master"
}
cc, err := grpc.Dial("127.0.0.1:5001", grpc.WithInsecure())
if err != nil {
http.Error(w, err.Error(), 500)
return
}
defer cc.Close()
c := builderpb.NewServiceClient(cc)
res, err := c.Build(context.Background(), &builderpb.BuildRequest{
ImportPath: importPath,
Branch: branch,
})
if err != nil {
log.Printf("failed to build: %v\n", err)
http.Error(w, err.Error(), 500)
return
}
var buildResult = struct {
Location string `json:"location"`
}{
Location: path.Join("/srv", res.GetLocation()[len(builderDataDir):]),
}
json.NewEncoder(w).Encode(buildResult)
}