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


Golang echo.Context類代碼示例

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


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

示例1: Expired

func (c *Config) Expired(rule *Rule, ctx echo.Context, saveFile string) (int64, bool) {
	var expired int64
	if rule.ExpireTime > 0 {
		expired = int64(rule.ExpireTime)
	} else if rule.ExpireFunc != nil {
		return rule.ExpireFunc(saveFile, ctx)
	} else {
		switch c.HtmlCacheTime.(type) {
		case int:
			expired = int64(c.HtmlCacheTime.(int))
		case int64:
			expired = c.HtmlCacheTime.(int64)
		case func(string, echo.Context) (int64, bool):
			fn := c.HtmlCacheTime.(func(string, echo.Context) (int64, bool))
			return fn(saveFile, ctx)
		}
	}
	mtime, err := com.FileMTime(saveFile)
	if err != nil {
		ctx.Object().Echo().Logger().Debug(err)
	}
	if mtime == 0 {
		return mtime, true
	}
	if time.Now().Local().Unix() > mtime+expired {
		return mtime, true
	}
	return mtime, false
}
開發者ID:webx-top,項目名稱:webx,代碼行數:29,代碼來源:config.go

示例2: OverrideRequestMethod

// OverrideRequestMethod overrides the http
// request's method with the specified method.
func OverrideRequestMethod(c echo.Context, method string) error {
	if !isValidOverrideMethod(method) {
		return ErrInvalidOverrideMethod
	}
	c.Request().Header().Set(HeaderHTTPMethodOverride, method)
	return nil
}
開發者ID:webx-top,項目名稱:webx,代碼行數:9,代碼來源:override.go

示例3: sleep

func sleep(e echo.Context, d time.Duration) {
	w := e.Response()
	var clientGone <-chan bool
	if cn, ok := w.(http.CloseNotifier); ok {
		clientGone = cn.CloseNotify()
	}
	select {
	case <-time.After(d):
	case <-clientGone:
	}
}
開發者ID:webx-top,項目名稱:webx,代碼行數:11,代碼來源:pprof.go

示例4: Write

func (c *Config) Write(b []byte, ctx echo.Context) bool {
	if !c.HtmlCacheOn || ctx.Request().Method() != `GET` || X.X(ctx).Code != http.StatusOK {
		return false
	}
	tmpl := X.MustString(ctx, `webx:saveHtmlFile`)
	if tmpl == `` {
		return false
	}
	if err := com.WriteFile(tmpl, b); err != nil {
		ctx.Object().Echo().Logger().Debug(err)
	}
	return true
}
開發者ID:webx-top,項目名稱:webx,代碼行數:13,代碼來源:config.go

示例5: HttpCache

func HttpCache(ctx echo.Context, eTag interface{}, etagValidator func(oldEtag, newEtag string) bool) bool {
	var etag string
	if eTag == nil {
		etag = fmt.Sprintf(`%v`, time.Now().UTC().Unix())
	} else {
		etag = fmt.Sprintf(`%v`, eTag)
	}
	resp := ctx.Response()
	//resp.Header().Set(`Connection`, `keep-alive`)
	resp.Header().Set(`X-Cache`, `HIT from Webx-Page-Cache`)
	if inm := ctx.Request().Header().Get("If-None-Match"); inm != `` {
		var valid bool
		if etagValidator != nil {
			valid = etagValidator(inm, etag)
		} else {
			valid = inm == etag
		}
		if valid {
			resp.Header().Del(`Content-Type`)
			resp.Header().Del(`Content-Length`)
			resp.WriteHeader(http.StatusNotModified)
			ctx.Object().Echo().Logger().Debugf(`%v is not modified.`, ctx.Path())
			return true
		}
	}
	resp.Header().Set(`Etag`, etag)
	resp.Header().Set(`Cache-Control`, `public,max-age=1`)
	return false
}
開發者ID:webx-top,項目名稱:webx,代碼行數:29,代碼來源:rule.go

示例6: Index

// Index responds with the pprof-formatted profile named by the request.
// For example, "/debug/pprof/heap" serves the "heap" profile.
// Index responds to a request for "/debug/pprof/" with an HTML page
// listing the available profiles.
func Index(e echo.Context) {
	r := e.Request()
	w := e.Response()
	if strings.HasPrefix(r.URL().Path(), "/debug/pprof/") {
		name := strings.TrimPrefix(r.URL().Path(), "/debug/pprof/")
		if name != "" {
			handler(name).ServeHTTP(e)
			return
		}
	}

	profiles := pprof.Profiles()
	if err := indexTmpl.Execute(w, profiles); err != nil {
		log.Print(err)
	}
}
開發者ID:webx-top,項目名稱:webx,代碼行數:20,代碼來源:pprof.go

示例7: Symbol

// Symbol looks up the program counters listed in the request,
// responding with a table mapping program counters to function names.
// The package initialization registers it as /debug/pprof/symbol.
func Symbol(e echo.Context) {
	w := e.Response()
	r := e.Request()
	w.Header().Set("Content-Type", "text/plain; charset=utf-8")

	// We have to read the whole POST body before
	// writing any output.  Buffer the output here.
	var buf bytes.Buffer

	// We don't know how many symbols we have, but we
	// do have symbol information.  Pprof only cares whether
	// this number is 0 (no symbols available) or > 0.
	fmt.Fprintf(&buf, "num_symbols: 1\n")

	var b *bufio.Reader
	if r.Method() == "POST" {
		b = bufio.NewReader(r.Body())
	} else {
		b = bufio.NewReader(strings.NewReader(r.URL().RawQuery()))
	}

	for {
		word, err := b.ReadSlice('+')
		if err == nil {
			word = word[0 : len(word)-1] // trim +
		}
		pc, _ := strconv.ParseUint(string(word), 0, 64)
		if pc != 0 {
			f := runtime.FuncForPC(uintptr(pc))
			if f != nil {
				fmt.Fprintf(&buf, "%#x %s\n", pc, f.Name())
			}
		}

		// Wait until here to check for err; the last
		// symbol will have an err because it doesn't end in +.
		if err != nil {
			if err != io.EOF {
				fmt.Fprintf(&buf, "reading request: %v\n", err)
			}
			break
		}
	}

	w.Write(buf.Bytes())
}
開發者ID:webx-top,項目名稱:webx,代碼行數:49,代碼來源:pprof.go

示例8: ServeHTTP

func (name handler) ServeHTTP(e echo.Context) {
	w := e.Response()
	r := e.Request()
	w.Header().Set("Content-Type", "text/plain; charset=utf-8")
	debug, _ := strconv.Atoi(r.FormValue("debug"))
	p := pprof.Lookup(string(name))
	if p == nil {
		w.WriteHeader(404)
		fmt.Fprintf(w, "Unknown profile: %s\n", name)
		return
	}
	gc, _ := strconv.Atoi(r.FormValue("gc"))
	if name == "heap" && gc > 0 {
		runtime.GC()
	}
	p.WriteTo(w, debug)
	return
}
開發者ID:webx-top,項目名稱:webx,代碼行數:18,代碼來源:pprof.go

示例9: Trace

// Trace responds with the execution trace in binary form.
// Tracing lasts for duration specified in seconds GET parameter, or for 1 second if not specified.
// The package initialization registers it as /debug/pprof/trace.
func Trace(e echo.Context) {
	w := e.Response()
	r := e.Request()
	sec, _ := strconv.ParseInt(r.FormValue("seconds"), 10, 64)
	if sec == 0 {
		sec = 1
	}

	// Set Content Type assuming trace.Start will work,
	// because if it does it starts writing.
	w.Header().Set("Content-Type", "application/octet-stream")
	if err := trace.Start(w); err != nil {
		// trace.Start failed, so no writes yet.
		// Can change header back to text content and send error code.
		w.Header().Set("Content-Type", "text/plain; charset=utf-8")
		w.WriteHeader(http.StatusInternalServerError)
		fmt.Fprintf(w, "Could not enable tracing: %s\n", err)
		return
	}
	sleep(e, time.Duration(sec)*time.Second)
	trace.Stop()
}
開發者ID:webx-top,項目名稱:webx,代碼行數:25,代碼來源:pprof.go

示例10: Register

func (c *Xsrf) Register(ctx echo.Context) {
	ctx.SetFunc("XsrfForm", func() template.HTML {
		return c.Form(ctx)
	})
	ctx.SetFunc("XsrfValue", func() string {
		return c.Value(ctx)
	})
	ctx.SetFunc("XsrfName", func() string {
		return c.FieldName
	})
}
開發者ID:webx-top,項目名稱:webx,代碼行數:11,代碼來源:xsrf.go

示例11: MustInt64

func MustInt64(c echo.Context, k string) (r int64) {
	if v, ok := c.Get(k).(int64); ok {
		r = v
	}
	return
}
開發者ID:webx-top,項目名稱:webx,代碼行數:6,代碼來源:helper.go

示例12: MustBool

func MustBool(c echo.Context, k string) (r bool) {
	if v, ok := c.Get(k).(bool); ok {
		r = v
	}
	return
}
開發者ID:webx-top,項目名稱:webx,代碼行數:6,代碼來源:helper.go

示例13: MustString

func MustString(c echo.Context, k string) (r string) {
	if v, ok := c.Get(k).(string); ok {
		r = v
	}
	return
}
開發者ID:webx-top,項目名稱:webx,代碼行數:6,代碼來源:helper.go

示例14: Ignore

func (j *JWT) Ignore(on bool, c echo.Context) {
	c.Set(`webx:ignoreJwt`, on)
}
開發者ID:webx-top,項目名稱:webx,代碼行數:3,代碼來源:jwt.go

示例15: Claims

func (j *JWT) Claims(c echo.Context) map[string]interface{} {
	r, _ := c.Get(`webx:jwtClaims`).(map[string]interface{})
	return r
}
開發者ID:webx-top,項目名稱:webx,代碼行數:4,代碼來源:jwt.go


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