Example #1
0
// Handler beego filter handler for serve captcha image
func (c *Captcha) Handler(ctx *context.Context) {
	var chars []byte

	id := path.Base(ctx.Request.RequestURI)
	if i := strings.Index(id, "."); i != -1 {
		id = id[:i]
	}

	key := c.key(id)

	if len(ctx.Input.Query("reload")) > 0 {
		chars = c.genRandChars()
		if err := c.store.Put(key, chars, c.Expiration); err != nil {
			ctx.Output.SetStatus(500)
			ctx.WriteString("captcha reload error")
			beego.Error("Reload Create Captcha Error:", err)
			return
		}
	} else {
		if v, ok := c.store.Get(key).([]byte); ok {
			chars = v
		} else {
			ctx.Output.SetStatus(404)
			ctx.WriteString("captcha not found")
			return
		}
	}

	img := NewImage(chars, c.StdWidth, c.StdHeight)
	if _, err := img.WriteTo(ctx.ResponseWriter); err != nil {
		beego.Error("Write Captcha Image Error:", err)
	}
}
Example #2
0
func (p *ControllerRegistor) recoverPanic(context *beecontext.Context) {
	if err := recover(); err != nil {
		if err == USERSTOPRUN {
			return
		}
		if RunMode == "dev" {
			if !RecoverPanic {
				panic(err)
			} else {
				if ErrorsShow {
					if handler, ok := ErrorMaps[fmt.Sprint(err)]; ok {
						executeError(handler, context)
						return
					}
				}
				var stack string
				Critical("the request url is ", context.Input.Url())
				Critical("Handler crashed with error", err)
				for i := 1; ; i++ {
					_, file, line, ok := runtime.Caller(i)
					if !ok {
						break
					}
					Critical(fmt.Sprintf("%s:%d", file, line))
					stack = stack + fmt.Sprintln(fmt.Sprintf("%s:%d", file, line))
				}
				showErr(err, context, stack)
			}
		} else {
			if !RecoverPanic {
				panic(err)
			} else {
				// in production model show all infomation
				if ErrorsShow {
					if handler, ok := ErrorMaps[fmt.Sprint(err)]; ok {
						executeError(handler, context)
						return
					} else if handler, ok := ErrorMaps["503"]; ok {
						executeError(handler, context)
						return
					} else {
						context.WriteString(fmt.Sprint(err))
					}
				} else {
					Critical("the request url is ", context.Input.Url())
					Critical("Handler crashed with error", err)
					for i := 1; ; i++ {
						_, file, line, ok := runtime.Caller(i)
						if !ok {
							break
						}
						Critical(fmt.Sprintf("%s:%d", file, line))
					}
				}
			}
		}
	}
}
Example #3
0
func WriteJson(ctx *context.Context, i interface{}) error {
	data, err := json.Marshal(i)
	if err != nil {
		fmt.Println(err)
		return err
	}
	ctx.WriteString(string(data))
	return nil
}
Example #4
0
File: code.go Project: tcdog001/ums
func (me *StatusCode) Write(ctx *context.Context, code EUmsError, err error) {
	me.Code = int(code)

	j, _ := json.Marshal(me)

	ctx.WriteString(string(j))

	beego.Info(string(j), code.ToString(), err)
}
Example #5
0
// show error string as simple text message.
// if error string is empty, show 500 error as default.
func exception(errcode string, ctx *context.Context) {
	code, err := strconv.Atoi(errcode)
	if err != nil {
		code = 503
	}
	if h, ok := ErrorMaps[errcode]; ok {
		executeError(h, ctx, code)
		return
	} else if h, ok := ErrorMaps["503"]; ok {
		executeError(h, ctx, code)
		return
	} else {
		ctx.ResponseWriter.WriteHeader(code)
		ctx.WriteString(errcode)
	}
}
Example #6
0
// show error string as simple text message.
// if error string is empty, show 503 or 500 error as default.
func exception(errCode string, ctx *context.Context) {
	atoi := func(code string) int {
		v, err := strconv.Atoi(code)
		if err == nil {
			return v
		}
		return 503
	}

	for _, ec := range []string{errCode, "503", "500"} {
		if h, ok := ErrorMaps[ec]; ok {
			executeError(h, ctx, atoi(ec))
			return
		}
	}
	//if 50x error has been removed from errorMap
	ctx.ResponseWriter.WriteHeader(atoi(errCode))
	ctx.WriteString(errCode)
}
Example #7
0
func DocsStatic(ctx *context.Context) {
	if uri := ctx.Input.Param(":all"); len(uri) > 0 {
		lang := ctx.GetCookie("lang")
		if !i18n.IsExist(lang) {
			lang = "en-US"
		}

		f, err := os.Open("docs/" + lang + "/" + "images/" + uri)
		if err != nil {
			ctx.WriteString(err.Error())
			return
		}
		defer f.Close()

		_, err = io.Copy(ctx.ResponseWriter, f)
		if err != nil {
			ctx.WriteString(err.Error())
			return
		}
	}
}
//beego
func beegoHandler(ctx *context.Context) {
	if sleepTime > 0 {
		time.Sleep(sleepTimeDuration)
	}
	ctx.WriteString(messageStr)
}
Example #9
0
func beegoFilterFunc(ctx *context.Context) {
	ctx.WriteString("hello")
}
Example #10
0
func beegoFinishRouter2(ctx *context.Context) {
	ctx.WriteString("|FinishRouter2")
}
Example #11
0
func beegoAfterExec2(ctx *context.Context) {
	ctx.WriteString("|AfterExec2")
}
Example #12
0
func beegoBeforeExec2(ctx *context.Context) {
	ctx.WriteString("|BeforeExec2")
}
Example #13
0
func beegoBeforeRouter2(ctx *context.Context) {
	ctx.WriteString("|BeforeRouter2")
}
func beegoHandlerWrite(ctx *context.Context) {
	ctx.WriteString(ctx.Input.Param(":name"))
}