github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/api/server/router/system/system_routes.go (about) 1 package system 2 3 import ( 4 "encoding/json" 5 "net/http" 6 "time" 7 8 "github.com/Sirupsen/logrus" 9 "github.com/docker/docker/api" 10 "github.com/docker/docker/api/server/httputils" 11 "github.com/docker/docker/pkg/ioutils" 12 "github.com/docker/engine-api/types" 13 "github.com/docker/engine-api/types/events" 14 "github.com/docker/engine-api/types/filters" 15 timetypes "github.com/docker/engine-api/types/time" 16 "golang.org/x/net/context" 17 ) 18 19 func optionsHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { 20 w.WriteHeader(http.StatusOK) 21 return nil 22 } 23 24 func pingHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { 25 _, err := w.Write([]byte{'O', 'K'}) 26 return err 27 } 28 29 func (s *systemRouter) getInfo(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { 30 info, err := s.backend.SystemInfo() 31 if err != nil { 32 return err 33 } 34 35 return httputils.WriteJSON(w, http.StatusOK, info) 36 } 37 38 func (s *systemRouter) getVersion(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { 39 info := s.backend.SystemVersion() 40 info.APIVersion = api.DefaultVersion.String() 41 42 return httputils.WriteJSON(w, http.StatusOK, info) 43 } 44 45 func (s *systemRouter) getEvents(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { 46 if err := httputils.ParseForm(r); err != nil { 47 return err 48 } 49 since, sinceNano, err := timetypes.ParseTimestamps(r.Form.Get("since"), -1) 50 if err != nil { 51 return err 52 } 53 until, untilNano, err := timetypes.ParseTimestamps(r.Form.Get("until"), -1) 54 if err != nil { 55 return err 56 } 57 58 timer := time.NewTimer(0) 59 timer.Stop() 60 if until > 0 || untilNano > 0 { 61 dur := time.Unix(until, untilNano).Sub(time.Now()) 62 timer = time.NewTimer(dur) 63 } 64 65 ef, err := filters.FromParam(r.Form.Get("filters")) 66 if err != nil { 67 return err 68 } 69 70 w.Header().Set("Content-Type", "application/json") 71 72 // This is to ensure that the HTTP status code is sent immediately, 73 // so that it will not block the receiver. 74 w.WriteHeader(http.StatusOK) 75 if flusher, ok := w.(http.Flusher); ok { 76 flusher.Flush() 77 } 78 79 output := ioutils.NewWriteFlusher(w) 80 defer output.Close() 81 82 enc := json.NewEncoder(output) 83 84 buffered, l := s.backend.SubscribeToEvents(since, sinceNano, ef) 85 defer s.backend.UnsubscribeFromEvents(l) 86 87 for _, ev := range buffered { 88 if err := enc.Encode(ev); err != nil { 89 return err 90 } 91 } 92 93 var closeNotify <-chan bool 94 if closeNotifier, ok := w.(http.CloseNotifier); ok { 95 closeNotify = closeNotifier.CloseNotify() 96 } 97 98 for { 99 select { 100 case ev := <-l: 101 jev, ok := ev.(events.Message) 102 if !ok { 103 logrus.Warnf("unexpected event message: %q", ev) 104 continue 105 } 106 if err := enc.Encode(jev); err != nil { 107 return err 108 } 109 case <-timer.C: 110 return nil 111 case <-closeNotify: 112 logrus.Debug("Client disconnected, stop sending events") 113 return nil 114 } 115 } 116 } 117 118 func (s *systemRouter) postAuth(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { 119 var config *types.AuthConfig 120 err := json.NewDecoder(r.Body).Decode(&config) 121 r.Body.Close() 122 if err != nil { 123 return err 124 } 125 status, err := s.backend.AuthenticateToRegistry(config) 126 if err != nil { 127 return err 128 } 129 return httputils.WriteJSON(w, http.StatusOK, &types.AuthResponse{ 130 Status: status, 131 }) 132 }