func main() { martini.Env = martini.Prod m := martini.Classic() m.Use(martini.Static("../../admin/static/")) m.Use(martini.Static(APP_STORE_DIR)) m.Use(sessions.Sessions("compass_session", sessions.NewCookieStore([]byte("compass_session_cookie")))) m.Get("/", func(w http.ResponseWriter) string { w.Header().Set("content-type", "text/html") return SCRIPT_LOGIN }) m.Get("/checkcode", func(r *http.Request, w http.ResponseWriter, s sessions.Session) { code := captcha.NewLen(4) s.Set("checkcode", code) captcha.WriteImage(w, code, 110, 40) }) showTemplate([]string{"footer", "form", "index", "left", "login", "main", "right", "top"}, m) for actionName, actionHandler := range admin.ActionHandlers { m.Post(fmt.Sprintf("/action/%s", actionName), actionHandler) } m.RunOnAddr(SERVER_ADDR) }
func imageCaptcha(writer http.ResponseWriter, request *http.Request) { vars := mux.Vars(request) captchaId := vars["captchaId"] width, err := strconv.Atoi(request.URL.Query().Get("w")) if err != nil { width = 200 } height, err := strconv.Atoi(request.URL.Query().Get("h")) if err != nil { height = 100 } writer.Header().Set("Content-Type", "image/png") captcha.WriteImage(writer, captchaId, width, height) }
/* Serves a CAPTCHA image Parameters: ctx: The context of the http request id: The ID of the captcha to serve */ func serveCaptchaImage(ctx *web.Context, id string) { width, err := strconv.Atoi(ctx.Params["width"]) if err != nil { logger.Printf("Error: could not parse captcha image width of '%v'\n%v\n", ctx.Params["width"], err.Error()) width = captcha.StdWidth } height, err := strconv.Atoi(ctx.Params["height"]) if err != nil { logger.Printf("Error: could not parse captcha image height of '%v'\n%v\n", ctx.Params["height"], err.Error()) height = captcha.StdHeight } //tell the user's browser not to cache the image file ctx.SetHeader("Cache-Control", "no-cache", true) err = captcha.WriteImage(ctx, id, width, height) if err != nil { logger.Println("Error, could not write CAPTCHA image") logger.Println(err.Error()) } }
func (controller *Captcha) serve(w http.ResponseWriter, r *http.Request, id, ext, lang string, download bool) error { w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") w.Header().Set("Pragma", "no-cache") w.Header().Set("Expires", "0") var content bytes.Buffer switch ext { case ".png": w.Header().Set("Content-Type", "image/png") captcha.WriteImage(&content, id, captcha.StdWidth, captcha.StdHeight) case ".wav": w.Header().Set("Content-Type", "audio/x-wav") captcha.WriteAudio(&content, id, lang) default: return captcha.ErrNotFound } if download { w.Header().Set("Content-Type", "application/octet-stream") } http.ServeContent(w, r, id+ext, time.Time{}, bytes.NewReader(content.Bytes())) return nil }
//首页 func (c *Captcha) Index() revel.Result { captcha.Server(250, 62) var CaptchaId string = c.Params.Get("CaptchaId") captcha.WriteImage(c.Response.Out, CaptchaId, 250, 62) return nil }
// 验证图片 func (this *Captcha) Image(params martini.Params, w http.ResponseWriter) { w.Header().Set("Content-Type", "image/png") captcha.WriteImage(w, params["id"], 240, 80) }
func (o *Captcha) MarshalPNG(w io.Writer) locale.Error { // TODO: generate PNG and write it to w return locale.UntranslatedError(captcha.WriteImage(w, o.ID, defaultWidth, defaultHeight)) }
// 生成验证码 func (this *UserController) NewCaptcha() { captchaStr := captcha.New() //生成唯一的id this.SetSession("captchaStr", captchaStr) //将该字符串放入session中 captcha.WriteImage(this.Ctx.ResponseWriter, captchaStr, captcha.StdWidth, captcha.StdHeight) //将验证码输出到浏览器 return }
//首页 func (c *Captcha) Index() revel.Result { captcha.Server(250, 62) CaptchaId := captcha.NewLen(6) captcha.WriteImage(c.Response.Out, CaptchaId, 250, 62) return nil }