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


Golang web.Response類代碼示例

本文整理匯總了Golang中github.com/garyburd/indigo/web.Response的典型用法代碼示例。如果您正苦於以下問題:Golang Response類的具體用法?Golang Response怎麽用?Golang Response使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


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

示例1: handleError

func handleError(resp web.Response, req *web.Request, status int, err error, r interface{}) {
	if err != nil {
		var buf bytes.Buffer
		fmt.Fprintf(&buf, "Error serving %s: %v\n", req.URL, err)
		if r != nil {
			fmt.Fprintln(&buf, r)
			buf.Write(debug.Stack())
		}
		log.Print(buf.String())
	}
	switch status {
	case 0:
		// nothing to do
	case web.StatusNotFound:
		executeTemplate(resp, "notfound"+templateExt(req), status, nil)
	default:
		s := web.StatusText(status)
		if err == errUpdateTimeout {
			s = "Timeout getting package files from the version control system."
		} else if e, ok := err.(doc.GetError); ok {
			s = "Error getting package files from " + e.Host + "."
		}
		w := resp.Start(web.StatusInternalServerError, web.Header{web.HeaderContentType: {"text/plan; charset=uft-8"}})
		io.WriteString(w, s)
	}
}
開發者ID:jeffallen,項目名稱:gopkgdoc,代碼行數:26,代碼來源:main.go

示例2: executeTemplate

func executeTemplate(resp web.Response, name string, status int, data interface{}) error {
	contentType, ok := contentTypes[path.Ext(name)]
	if !ok {
		contentType = "text/plain; charset=utf-8"
	}

	w := resp.Start(status, web.Header{web.HeaderContentType: {contentType}})
	return templateSet.ExecuteTemplate(w, name, data)
}
開發者ID:jeffallen,項目名稱:gopkgdoc,代碼行數:9,代碼來源:template.go

示例3: serveCompile

func serveCompile(resp web.Response, req *web.Request) error {
	r, err := http.PostForm("http://golang.org/compile", req.Form)
	if err != nil {
		return err
	}
	defer r.Body.Close()
	_, err = io.Copy(
		resp.Start(web.StatusOK, web.Header{web.HeaderContentType: r.Header[web.HeaderContentType]}),
		r.Body)
	return err
}
開發者ID:AlekSi,項目名稱:gddo,代碼行數:11,代碼來源:main.go

示例4: serveAPIPackages

func serveAPIPackages(resp web.Response, req *web.Request) error {
	pkgs, err := db.AllPackages()
	if err != nil {
		return err
	}
	var data struct {
		Results []database.Package `json:"results"`
	}
	data.Results = pkgs
	w := resp.Start(web.StatusOK, web.Header{web.HeaderContentType: {"application/json; charset=uft-8"}})
	return json.NewEncoder(w).Encode(&data)
}
開發者ID:AlekSi,項目名稱:gddo,代碼行數:12,代碼來源:main.go

示例5: executeTemplate

func executeTemplate(resp web.Response, name string, status int, data interface{}) error {
	contentType, ok := contentTypes[path.Ext(name)]
	if !ok {
		contentType = "text/plain; charset=utf-8"
	}
	t := templates[name]
	if t == nil {
		return fmt.Errorf("Template %s not found", name)
	}
	w := resp.Start(status, web.Header{web.HeaderContentType: {contentType}})
	return t.Execute(w, data)
}
開發者ID:sbryant,項目名稱:gopkgdoc,代碼行數:12,代碼來源:template.go

示例6: serveTypeahead

func serveTypeahead(resp web.Response, req *web.Request) error {
	pkgs, err := db.Popular(1000)
	if err != nil {
		return err
	}
	items := make([]string, len(pkgs))
	for i, pkg := range pkgs {
		items[i] = pkg.Path
	}
	data := map[string]interface{}{"items": items}
	w := resp.Start(web.StatusOK, web.Header{web.HeaderContentType: {"application/json; charset=uft-8"}})
	return json.NewEncoder(w).Encode(data)
}
開發者ID:AlekSi,項目名稱:gddo,代碼行數:13,代碼來源:main.go

示例7: serveAPISearch

func serveAPISearch(resp web.Response, req *web.Request) error {
	q := strings.TrimSpace(req.Form.Get("q"))
	pkgs, err := db.Query(q)
	if err != nil {
		return err
	}

	var data struct {
		Results []database.Package `json:"results"`
	}
	data.Results = pkgs
	w := resp.Start(web.StatusOK, web.Header{web.HeaderContentType: {"application/json; charset=uft-8"}})
	return json.NewEncoder(w).Encode(&data)
}
開發者ID:AlekSi,項目名稱:gddo,代碼行數:14,代碼來源:main.go

示例8: handleAPIError

func handleAPIError(resp web.Response, req *web.Request, status int, err error, r interface{}) {
	logError(req, err, r)
	switch status {
	case 0:
		// nothing to do
	default:
		var data struct {
			Error struct {
				Message string `json:"message"`
			} `json:"error"`
		}
		data.Error.Message = web.StatusText(status)
		w := resp.Start(status, web.Header{web.HeaderContentType: {"application/json; charset=uft-8"}})
		json.NewEncoder(w).Encode(&data)
	}
}
開發者ID:AlekSi,項目名稱:gddo,代碼行數:16,代碼來源:main.go

示例9: renderPresentation

func renderPresentation(resp web.Response, fname string, doc *present.Doc) error {
	t := presentTemplates[path.Ext(fname)]
	data := struct {
		*present.Doc
		Template    *template.Template
		PlayEnabled bool
	}{
		doc,
		t,
		true,
	}

	return t.Execute(
		resp.Start(web.StatusOK, web.Header{web.HeaderContentType: {"text/html; charset=utf8"}}),
		&data)
}
開發者ID:AlekSi,項目名稱:gddo,代碼行數:16,代碼來源:main.go

示例10: handleError

func handleError(resp web.Response, req *web.Request, status int, err error, r interface{}) {
	logError(req, err, r)
	switch status {
	case 0:
		// nothing to do
	case web.StatusNotFound:
		executeTemplate(resp, "notfound"+templateExt(req), status, nil, nil)
	default:
		s := web.StatusText(status)
		if err == errUpdateTimeout {
			s = "Timeout getting package files from the version control system."
		} else if e, ok := err.(*doc.RemoteError); ok {
			s = "Error getting package files from " + e.Host + "."
		}
		w := resp.Start(web.StatusInternalServerError, web.Header{web.HeaderContentType: {"text/plan; charset=uft-8"}})
		io.WriteString(w, s)
	}
}
開發者ID:AlekSi,項目名稱:gddo,代碼行數:18,代碼來源:main.go

示例11: handlePresentError

func handlePresentError(resp web.Response, req *web.Request, status int, err error, r interface{}) {
	logError(req, err, r)
	switch status {
	case 0:
		// nothing to do
	default:
		s := web.StatusText(status)
		if doc.IsNotFound(err) {
			s = web.StatusText(web.StatusNotFound)
			status = web.StatusNotFound
		} else if err == errUpdateTimeout {
			s = "Timeout getting package files from the version control system."
		} else if e, ok := err.(*doc.RemoteError); ok {
			s = "Error getting package files from " + e.Host + "."
		}
		w := resp.Start(status, web.Header{web.HeaderContentType: {"text/plan; charset=uft-8"}})
		io.WriteString(w, s)
	}
}
開發者ID:AlekSi,項目名稱:gddo,代碼行數:19,代碼來源:main.go

示例12: rootHandler

func rootHandler(resp web.Response, req *web.Request) error {

	key := req.URL.Path[1:]
	if key == "" {
		key = config.Index
	}

	var status = web.StatusOK
	b, mimeType, err := readKey(key)
	if err != nil && config.Error != "" {
		status = web.StatusNotFound
		b, mimeType, err = readKey(config.Error)
	}
	if err != nil {
		return &web.Error{Status: web.StatusNotFound, Reason: err}
	}

	w := resp.Start(status, web.Header{
		web.HeaderContentLength: {strconv.Itoa(len(b))},
		web.HeaderContentType:   {mimeType},
	})
	_, err = w.Write(b)
	return err
}
開發者ID:rayleyva,項目名稱:s3web,代碼行數:24,代碼來源:s3web.go


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