func routeWatcher() { conn := g.GetRedisConn() defer g.ReleaseRedisConn(conn) subs := gore.NewSubscriptions(conn) defer subs.Close() subKey := fmt.Sprintf("eru:agent:%s:route", g.Config.HostName) logs.Debug("API route subscribe", subKey) subs.Subscribe(subKey) for message := range subs.Message() { if message == nil { logs.Info("API route watcher shutdown") break } command := string(message.Message) logs.Debug("API route watcher get", command) parser := strings.Split(command, "|") if len(parser) != 2 { logs.Info("API route watcher command invaild", command) continue } cid, gateway := parser[0], parser[1] if !network.SetDefaultRoute(cid, gateway) { logs.Info("Set default route failed") } } }
// URL /api/container/:container_id/setroute/ func setRouteForContainer(req *Request) (int, interface{}) { type Gateway struct { IP string } cid := req.URL.Query().Get(":container_id") data := &Gateway{} decoder := json.NewDecoder(req.Body) if err := decoder.Decode(data); err != nil { return http.StatusBadRequest, JSON{"message": "wrong JSON format"} } if !network.SetDefaultRoute(cid, data.IP) { logs.Info("Set default route failed") return http.StatusServiceUnavailable, JSON{"message": "set default route failed"} } return http.StatusOK, JSON{"message": "ok"} }