Skip to content

Commit de753f1

Browse files
committed
add image pull socket connection
1 parent d72b4c1 commit de753f1

2 files changed

Lines changed: 33 additions & 28 deletions

File tree

controllers/image_pull.go

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/json"
77
"fmt"
88
"github.com/docker/docker/api/types"
9-
"github.com/gorilla/mux"
109
"github.com/gorilla/websocket"
1110
"io"
1211
"log"
@@ -17,11 +16,6 @@ import (
1716
type HandleImagePull struct {
1817
}
1918

20-
type longLatStruct struct {
21-
Long float64 `json:"longitude"`
22-
Lat float64 `json:"latitude"`
23-
}
24-
2519
type Event struct {
2620
Status string `json:"status"`
2721
Error string `json:"error"`
@@ -32,20 +26,36 @@ type Event struct {
3226
} `json:"progressDetails"`
3327
}
3428

29+
type ImageData struct {
30+
Name string `json:"name"`
31+
}
32+
3533
var clients = make(map[*websocket.Conn]bool)
36-
var broadcast = make(chan *longLatStruct)
34+
var broadcast = make(chan *Event)
3735
var upgrade = websocket.Upgrader{
3836
CheckOrigin: func(r *http.Request) bool {
3937
return true
4038
},
4139
}
40+
var echoValue = false
4241

4342
func (HandleImagePull) ServeHTTP(w http.ResponseWriter, r *http.Request) {
4443
cli := adapters.GetClient()
4544
ctx := context.Background()
46-
imageName := mux.Vars(r)["image"]
45+
decoder := json.NewDecoder(r.Body)
46+
var imageData ImageData
47+
err := decoder.Decode(&imageData)
4748

48-
events, err := cli.ImagePull(ctx, imageName, types.ImagePullOptions{})
49+
if err != nil {
50+
panic(err)
51+
}
52+
53+
if !echoValue {
54+
go Echo()
55+
echoValue = true
56+
}
57+
58+
events, err := cli.ImagePull(ctx, imageData.Name, types.ImagePullOptions{})
4959
if err != nil {
5060
panic(err)
5161
}
@@ -61,35 +71,30 @@ func (HandleImagePull) ServeHTTP(w http.ResponseWriter, r *http.Request) {
6171
}
6272
panic(err)
6373
}
74+
75+
var eventData Event
76+
77+
if err := json.NewDecoder(r.Body).Decode(&eventData); err != nil {
78+
log.Printf("ERROR: %s", err)
79+
}
80+
go write(event)
6481
fmt.Printf("EVENT: %+v\n", event)
6582
}
6683

6784
if event != nil {
68-
if strings.Contains(event.Status, fmt.Sprintf("Downloaded new image of %s", imageName)){
85+
if strings.Contains(event.Status, fmt.Sprintf("Downloaded new image of %s", imageData.Name)) {
6986
fmt.Println("new")
7087
}
7188

72-
if strings.Contains(event.Status, fmt.Sprintf("Image is uptodate for %s", imageName)){
89+
if strings.Contains(event.Status, fmt.Sprintf("Image is uptodate for %s", imageData.Name)) {
7390
fmt.Println("up-to-date")
7491
}
7592
}
7693

7794
}
7895

79-
func LongLatHandler(w http.ResponseWriter, r *http.Request) {
80-
var coodinates longLatStruct
81-
if err := json.NewDecoder(r.Body).Decode(&coodinates); err != nil {
82-
log.Printf("ERROR: %s", err)
83-
}
84-
defer r.Body.Close()
85-
go write(&coodinates)
86-
if len(clients) > 0 {
87-
go Echo()
88-
}
89-
}
90-
91-
func write(coord *longLatStruct) {
92-
broadcast <- coord
96+
func write(event *Event) {
97+
broadcast <- event
9398
}
9499

95100
func WsHandler(w http.ResponseWriter, r *http.Request) {
@@ -103,9 +108,9 @@ func WsHandler(w http.ResponseWriter, r *http.Request) {
103108
func Echo() {
104109
for {
105110
val := <-broadcast
106-
latlong := fmt.Sprintf("%f %f", val.Lat, val.Long)
111+
event := fmt.Sprintf("%s %s %d %d %s", val.Status, val.Progress, val.ProgressDetails.Current, val.ProgressDetails.Total, val.Error)
107112
for client := range clients {
108-
err := client.WriteMessage(websocket.TextMessage, []byte(latlong))
113+
err := client.WriteMessage(websocket.TextMessage, []byte(event))
109114
if err != nil {
110115
log.Printf("websocket error: %s", err)
111116
client.Close()

router/router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func GetRouter() *mux.Router {
2121
Router.Handle("/containers/{id}/pause", controllers.HandleContainerPause{})
2222

2323
//websocket
24-
Router.HandleFunc("/longlat", controllers.LongLatHandler).Methods("POST")
24+
//Router.HandleFunc("/longlat", controllers.LongLatHandler).Methods("POST")
2525
Router.HandleFunc("/ws", controllers.WsHandler)
2626
return Router
2727
}

0 commit comments

Comments
 (0)