Skip to content

Commit 562f99c

Browse files
author
GuoHaoLan
committed
一些细节优化
1 parent 330fd12 commit 562f99c

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

main.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ type Captcha struct {
6868
cache captcha.Store
6969
}
7070

71-
func (c *Captcha) GenCaptcha(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
71+
func (c Captcha) GenCaptcha(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
7272
imgWidth := r.URL.Query().Get("width")
7373
imgHeight := r.URL.Query().Get("height")
7474
id := uuid.NewString()
@@ -93,6 +93,7 @@ func (c *Captcha) GenCaptcha(w http.ResponseWriter, r *http.Request, _ httproute
9393

9494
if err := jpeg.Encode(&buf, _image.Paletted, nil); err != nil {
9595
ThrowError(w, UNKNOWN_INNER_ERROR, err.Error())
96+
return
9697
}
9798

9899
ret := CaptchaRes{}
@@ -105,12 +106,12 @@ func (c *Captcha) GenCaptcha(w http.ResponseWriter, r *http.Request, _ httproute
105106
WriteResult(w, http.StatusCreated, ret)
106107
}
107108

108-
func (c *Captcha) HandleCaptcha(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
109+
func (c Captcha) HandleCaptcha(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
109110
_captchaID := ps.ByName("captcha_id")
110111

111112
_phrase := r.URL.Query().Get("phrase")
112113

113-
if _phrase == "" {
114+
if _phrase == "" || _captchaID == "" {
114115
ThrowError(w, REQUEST_PARAM_FORMAT_ERROR, "No Enough Params", "phrase")
115116
return
116117
}
@@ -197,7 +198,7 @@ func main() {
197198
sig := make(chan struct{})
198199
router := httprouter.New()
199200

200-
C := &Captcha{
201+
C := Captcha{
201202
cache: captcha.NewMemoryStore(100, 10*time.Minute),
202203
}
203204
router.GET("/captcha", C.GenCaptcha)

0 commit comments

Comments
 (0)