blob: 9c1b1e911333183439b1181e5fe94aad5e645fd5 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package v4
import (
"github.com/apache/servicecomb-service-center/pkg/log"
pb "github.com/apache/servicecomb-service-center/pkg/registry"
"github.com/apache/servicecomb-service-center/pkg/rest"
"github.com/apache/servicecomb-service-center/server/core"
"github.com/gorilla/websocket"
"net/http"
)
type WatchService struct {
//
}
func (s *WatchService) URLPatterns() []rest.Route {
return []rest.Route{
{Method: rest.HTTPMethodGet, Path: "/v4/:project/registry/microservices/:serviceId/watcher", Func: s.Watch},
{Method: rest.HTTPMethodGet, Path: "/v4/:project/registry/microservices/:serviceId/listwatcher", Func: s.ListAndWatch},
}
}
func upgrade(w http.ResponseWriter, r *http.Request) (*websocket.Conn, error) {
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Error("upgrade failed", err)
}
return conn, err
}
func (s *WatchService) Watch(w http.ResponseWriter, r *http.Request) {
conn, err := upgrade(w, r)
if err != nil {
return
}
defer conn.Close()
r.Method = "WATCH"
core.InstanceAPI.WebSocketWatch(r.Context(), &pb.WatchInstanceRequest{
SelfServiceId: r.URL.Query().Get(":serviceId"),
}, conn)
}
func (s *WatchService) ListAndWatch(w http.ResponseWriter, r *http.Request) {
conn, err := upgrade(w, r)
if err != nil {
return
}
defer conn.Close()
r.Method = "WATCHLIST"
core.InstanceAPI.WebSocketListAndWatch(r.Context(), &pb.WatchInstanceRequest{
SelfServiceId: r.URL.Query().Get(":serviceId"),
}, conn)
}