handler.go 1.9 KB
Newer Older
Z
Zhengyi Lai 已提交
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
package v1alpha2

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"

	restful "github.com/emicklei/go-restful"
)

const ScopeQueryUrl = "http://weave-scope-app.weave.svc/api/topology/services"

func getNamespaceTopology(request *restful.Request, response *restful.Response) {
	var query = url.Values{
		"namespace": []string{request.PathParameter("namespace")},
		"timestamp": request.QueryParameters("timestamp"),
	}
	var u = fmt.Sprintf("%s?%s", ScopeQueryUrl, query.Encode())

	resp, err := http.Get(u)

	if err != nil {
		log.Printf("query scope faile with err %v", err)
		_ = response.WriteError(http.StatusInternalServerError, err)
		return
	}

	body, err := ioutil.ReadAll(resp.Body)
	defer resp.Body.Close()
	if err != nil {
		log.Printf("read response error : %v", err)
		_ = response.WriteError(http.StatusInternalServerError, err)
		return
	}

	// need to set header for proper response
	response.Header().Set("Content-Type", "application/json")
	_, err = response.Write(body)

	if err != nil {
		log.Printf("write response failed %v", err)
	}
}

func getNamespaceNodeTopology(request *restful.Request, response *restful.Response) {
	var query = url.Values{
		"namespace": []string{request.PathParameter("namespace")},
		"timestamp": request.QueryParameters("timestamp"),
	}
	var u = fmt.Sprintf("%s/%s?%s", ScopeQueryUrl, request.PathParameter("node_id"), query.Encode())

	resp, err := http.Get(u)

	if err != nil {
		log.Printf("query scope faile with err %v", err)
		_ = response.WriteError(http.StatusInternalServerError, err)
		return
	}

	body, err := ioutil.ReadAll(resp.Body)
	defer resp.Body.Close()
	if err != nil {
		log.Printf("read response error : %v", err)
		_ = response.WriteError(http.StatusInternalServerError, err)
		return
	}

	// need to set header for proper response
	response.Header().Set("Content-Type", "application/json")
	_, err = response.Write(body)

	if err != nil {
		log.Printf("write response failed %v", err)
	}
}