當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Marshaler.Marshal方法代碼示例

本文整理匯總了Golang中github.com/maditya/protobuf/jsonpb.Marshaler.Marshal方法的典型用法代碼示例。如果您正苦於以下問題:Golang Marshaler.Marshal方法的具體用法?Golang Marshaler.Marshal怎麽用?Golang Marshaler.Marshal使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/maditya/protobuf/jsonpb.Marshaler的用法示例。


在下文中一共展示了Marshaler.Marshal方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: ServeHTTP

func (h *HTTPFront) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	path := r.URL.Path
	method := r.Method

	// service healthcheck
	if (method == "HEAD" || method == "GET") && (path == "/status" || path == "/lb") {
		if !h.InRotation() {
			http.Error(w, `server out of rotation`, http.StatusNotFound)
			return
		}
		if method == "GET" {
			w.Write([]byte("OK"))
		}
		return
	}

	if method == "GET" && path == "/saml" {
		url, err := h.SAMLRequest()
		if err != nil {
			http.Error(w, err.Error(), http.StatusNotFound)
			return
		}
		http.Redirect(w, r, url, http.StatusFound)
		return
	}
	if method == "GET" && path == "/oidc" {
		u, err := url.ParseQuery(r.URL.RawQuery)
		if err != nil {
			http.Error(w, `error parsing query string`, http.StatusBadRequest)
			return
		}
		d := u.Get("domain")
		if d == "" {
			http.Error(w, `domain not found`, http.StatusBadRequest)
			return
		}

		url, err := h.OIDCRequest(d, "https://"+r.Host+"/oidcsso")
		if err != nil {
			http.Error(w, err.Error(), http.StatusNotFound)
			return
		}
		http.Redirect(w, r, url, http.StatusFound)
	}

	if method != "POST" || (path != "/lookup" && path != "/update") {
		http.Error(w, `this server only supports queries of the POST /lookup or POST /update`, http.StatusNotFound)
		return
	}
	pf := &proto.LookupProof{}
	var err error
	ctx := context.Background()
	if path == "/lookup" {
		pf, err = h.doLookup(r.Body, ctx)
		if err != nil {
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}
	} else if path == "/update" {
		pf, err = h.doUpdate(r.Body, ctx)
		if err != nil {
			status := http.StatusBadRequest
			if h.IsAuthExpired(err) {
				status = http.StatusUnauthorized
			}
			http.Error(w, err.Error(), status)
			return
		}
	}
	// preserve the original field name
	marshaler := jsonpb.Marshaler{OrigName: true}
	err = marshaler.Marshal(w, pf)
	if err != nil {
		http.Error(w, `Internal server error`, http.StatusInternalServerError)
		return
	}
	w.Header().Set("Content-Type", "application/json")
	return
}
開發者ID:yahoo,項目名稱:coname,代碼行數:79,代碼來源:httpfront.go


注:本文中的github.com/maditya/protobuf/jsonpb.Marshaler.Marshal方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。