本文整理汇总了Golang中github.com/lunny/tango.Context类的典型用法代码示例。如果您正苦于以下问题:Golang Context类的具体用法?Golang Context怎么用?Golang Context使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Context类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: serveFile
func (s *Server) serveFile(ctx *tango.Context, file string) bool {
if com.IsFile(file) {
ctx.ServeFile(file)
return true
}
return false
}
示例2: LoginUserFromRememberCookie
func LoginUserFromRememberCookie(user *models.User, ctx *tango.Context, session *session.Session) (success bool) {
userName := GetCookie(ctx.Req(), setting.CookieUserName)
if len(userName) == 0 {
return false
}
defer func() {
if !success {
DeleteRememberCookie(ctx)
}
}()
var err error
if user, err = models.GetUserByName(userName); err != nil {
return false
}
secret := utils.EncodeMd5(user.Rands + user.Password)
value, _ := GetSecureCookie(ctx.Req(), secret, setting.CookieRememberName)
if value != userName {
return false
}
LoginUser(user, ctx, session, true)
return true
}
示例3: serveFile
func (s *Server) serveFile(ctx *tango.Context, file string) bool {
log15.Debug("Dest.File." + file)
if com.IsFile(file) {
ctx.ServeFile(file)
return true
}
return false
}
示例4: Handle
// Handle implement tango.Handle
func (c *Caches) Handle(ctx *tango.Context) {
if action := ctx.Action(); ctx != nil {
if s, ok := action.(Cacher); ok {
s.SetCaches(c)
}
}
ctx.Next()
}
示例5: GetLoginRedirect
// get login redirect url from cookie
func GetLoginRedirect(ctx *tango.Context) string {
loginRedirect := strings.TrimSpace(GetCookie(ctx.Req(), "login_to"))
if utils.IsMatchHost(loginRedirect) == false {
loginRedirect = "/"
} else {
SetCookie(ctx, "login_to", "", -1, "/")
}
return loginRedirect
}
示例6: Image
func Image(ctx *tango.Context) {
token := ctx.Params().Get(":path")
// split token and file ext
var filePath string
if i := strings.IndexRune(token, '.'); i == -1 {
return
} else {
filePath = token[i+1:]
token = token[:i]
}
// decode token to file path
var image models.Image
if err := image.DecodeToken(token); err != nil {
log.Info(err)
return
}
// file real path
filePath = attachment.GenImagePath(&image) + filePath
// if x-send on then set header and http status
// fall back use proxy serve file
if setting.ImageXSend {
//ext := filepath.Ext(filePath)
// TODO:
//ctx.Header().ContentType(ext)
ctx.Header().Set(setting.ImageXSendHeader, "/"+filePath)
ctx.WriteHeader(http.StatusOK)
} else {
// direct serve file use go
ctx.ServeFile(filePath)
}
}
示例7: Handle
func (h *TimeHandler) Handle(ctx *tango.Context) {
t1 := time.Now()
ctx.Next()
ctx.Logger.Infof("Completed %v %v %v in %v for %v",
ctx.Req().Method,
ctx.Req().URL.Path,
ctx.Status(),
time.Since(t1),
ctx.Req().RemoteAddr,
)
}
示例8: globalHandler
func (s *Server) globalHandler(ctx *tango.Context) {
param := ctx.Param("*name")
if path.Ext(param) == "" {
if s.serveFile(ctx, path.Join(s.dstDir, param, "index.html")) {
return
}
}
if !strings.HasSuffix(param, "/") {
if s.serveFile(ctx, path.Join(s.dstDir, param, ".html")) {
return
}
}
if s.serveFile(ctx, path.Join(s.dstDir, param)) {
return
}
ctx.Redirect("/")
}
示例9: QiniuImage
func QiniuImage(ctx *tango.Context) {
var imageName = ctx.Params().Get(":path")
var imageKey string
var imageSize string
if i := strings.IndexRune(imageName, '.'); i == -1 {
return
} else {
imageSize = imageName[i+1:]
if j := strings.IndexRune(imageSize, '.'); j == -1 {
imageSize = "full"
} else {
imageSize = imageSize[:j]
}
imageKey = imageName[:i]
}
var image = models.Image{
Token: imageKey,
}
err := models.GetByExample(&image)
if err != nil {
return
}
var imageWidth = image.Width
var imageHeight = image.Height
var zoomRatio = setting.ImageSizeMiddle / imageWidth
if imageWidth > setting.ImageSizeMiddle {
imageWidth = setting.ImageSizeMiddle
}
imageHeight *= zoomRatio
var imageUrl = utils.GetQiniuPublicDownloadUrl(setting.QiniuPostDomain, imageKey)
var zoomImageUrl = utils.GetQiniuZoomViewUrl(imageUrl, imageWidth, imageHeight)
resp, err := http.Get(zoomImageUrl)
if err != nil {
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
ctx.ResponseWriter.Write(body)
}
示例10: Handle
func (rh *RecoveryHandler) Handle(ctx *tango.Context) {
// capture render-controller error
if render, ok := ctx.Action().(ITheme); ok {
if err, ok := ctx.Result.(tango.AbortError); ok {
render.RenderError(err.Code(), err)
return
}
if err, ok := ctx.Result.(error); ok {
ctx.WriteHeader(500)
render.RenderError(ctx.Status(), err)
return
}
}
// capture abort error
/*
if err, ok := ctx.Result.(tango.AbortError); ok {
ctx.WriteHeader(err.Code())
theme := new(ThemeRender)
theme.SetTheme(nil)
theme.RenderError(err.Code(), err)
return
}*/
// unexpected error
tango.Errors()(ctx)
}
示例11: getContextLanguage
// get language from context
func getContextLanguage(ctx *tango.Context) string {
// get from cookie
lang := ctx.Cookie(langCookieName)
// get from header
if lang == "" {
al := ctx.Req().Header.Get("Accept-Language")
if len(al) > 4 {
lang = al[:5] // Only compare first 5 letters.
}
}
// get from query param
if lang == "" {
lang = ctx.Param(langParamName)
}
// get default if not find in context
lang = strings.ToLower(lang)
if !i18n.IsExist(lang) {
lang = i18n.GetLangByIndex(0)
}
return lang
}
示例12: GetAuthToken
// get auth token via http header, cookie or form value
func (ar *AuthRouter) GetAuthToken(ctx *tango.Context) string {
var token string
if token = ctx.Header().Get("X-Token"); token != "" {
return token
}
if token = ctx.Cookie("x-token"); token != "" {
return token
}
return ctx.Form("x-token")
}
示例13: globalHandler
func (s *Server) globalHandler(ctx *tango.Context) {
param := ctx.Param("*name")
if param == "favicon.ico" || param == "robots.txt" {
if !s.serveFiles(ctx, param) {
ctx.NotFound()
}
return
}
if !strings.HasPrefix("/"+param, s.prefix) {
ctx.Redirect(s.prefix)
return
}
param = strings.TrimPrefix("/"+param, s.prefix)
s.serveFiles(ctx, param)
}
示例14: tangoHandler
//Tango
func tangoHandler(ctx *tango.Context) {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
ctx.Write(message)
}
示例15: Handle
// Handle implements tango handler,
// copy from tango static.go
func (s *Static) Handle(ctx *tango.Context) {
if ctx.Req().Method != "GET" && ctx.Req().Method != "HEAD" {
ctx.Next()
return
}
opt := prepareStaticOptions(s.Options)
var rPath = ctx.Req().URL.Path
// if defined prefix, then only check prefix
if opt.Prefix != "" {
if !strings.HasPrefix(ctx.Req().URL.Path, opt.Prefix) {
ctx.Next()
return
} else {
if len(opt.Prefix) == len(ctx.Req().URL.Path) {
rPath = ""
} else {
rPath = ctx.Req().URL.Path[len(opt.Prefix):]
}
}
}
fPath, _ := filepath.Abs(filepath.Join(opt.RootPath, rPath))
finfo, err := os.Stat(fPath)
if err != nil {
if !os.IsNotExist(err) {
ctx.Result = tango.InternalServerError(err.Error())
ctx.HandleError()
return
}
} else if !finfo.IsDir() {
if len(opt.FilterExts) > 0 {
var matched bool
for _, ext := range opt.FilterExts {
if filepath.Ext(fPath) == ext {
matched = true
break
}
}
if !matched {
ctx.Next()
return
}
}
err := ctx.ServeFile(fPath)
if err != nil {
ctx.Result = tango.InternalServerError(err.Error())
ctx.HandleError()
}
return
} else {
// try serving index.html or index.htm
if len(opt.IndexFiles) > 0 {
for _, index := range opt.IndexFiles {
nPath := filepath.Join(fPath, index)
finfo, err = os.Stat(nPath)
if err != nil {
if !os.IsNotExist(err) {
ctx.Result = tango.InternalServerError(err.Error())
ctx.HandleError()
return
}
} else if !finfo.IsDir() {
err = ctx.ServeFile(nPath)
if err != nil {
ctx.Result = tango.InternalServerError(err.Error())
ctx.HandleError()
}
return
}
}
}
// list dir files
if opt.ListDir {
ctx.Header().Set("Content-Type", "text/html; charset=UTF-8")
ctx.Write([]byte(`<ul style="list-style-type:none;line-height:32px;">`))
rootPath, _ := filepath.Abs(opt.RootPath)
rPath, _ := filepath.Rel(rootPath, fPath)
if fPath != rootPath {
ctx.Write([]byte(`<li> <a href="/` + path.Join(opt.Prefix, filepath.Dir(rPath)) + `">..</a></li>`))
}
err = filepath.Walk(fPath, func(p string, fi os.FileInfo, err error) error {
rPath, _ := filepath.Rel(fPath, p)
if rPath == "." || len(strings.Split(rPath, string(filepath.Separator))) > 1 {
return nil
}
rPath, _ = filepath.Rel(rootPath, p)
ps, _ := os.Stat(p)
if ps.IsDir() {
ctx.Write([]byte(`<li>┖ <a href="/` + path.Join(opt.Prefix, rPath) + `">` + filepath.Base(p) + `</a></li>`))
} else {
if len(opt.FilterExts) > 0 {
var matched bool
for _, ext := range opt.FilterExts {
if filepath.Ext(p) == ext {
//.........这里部分代码省略.........