Merge pull request #1044 from fuxiaohei/develop
code style simplify
Showing
10 changed files
with
66 additions
and
70 deletions
| ... | @@ -155,8 +155,11 @@ func (ctx *Context) CheckXsrfCookie() bool { | ... | @@ -155,8 +155,11 @@ func (ctx *Context) CheckXsrfCookie() bool { |
| 155 | } | 155 | } |
| 156 | if token == "" { | 156 | if token == "" { |
| 157 | ctx.Abort(403, "'_xsrf' argument missing from POST") | 157 | ctx.Abort(403, "'_xsrf' argument missing from POST") |
| 158 | } else if ctx._xsrf_token != token { | 158 | return false |
| 159 | } | ||
| 160 | if ctx._xsrf_token != token { | ||
| 159 | ctx.Abort(403, "XSRF cookie does not match POST argument") | 161 | ctx.Abort(403, "XSRF cookie does not match POST argument") |
| 162 | return false | ||
| 160 | } | 163 | } |
| 161 | return true | 164 | return true |
| 162 | } | 165 | } | ... | ... |
| ... | @@ -72,11 +72,11 @@ func (input *BeegoInput) Site() string { | ... | @@ -72,11 +72,11 @@ func (input *BeegoInput) Site() string { |
| 72 | func (input *BeegoInput) Scheme() string { | 72 | func (input *BeegoInput) Scheme() string { |
| 73 | if input.Request.URL.Scheme != "" { | 73 | if input.Request.URL.Scheme != "" { |
| 74 | return input.Request.URL.Scheme | 74 | return input.Request.URL.Scheme |
| 75 | } else if input.Request.TLS == nil { | 75 | } |
| 76 | if input.Request.TLS == nil { | ||
| 76 | return "http" | 77 | return "http" |
| 77 | } else { | ||
| 78 | return "https" | ||
| 79 | } | 78 | } |
| 79 | return "https" | ||
| 80 | } | 80 | } |
| 81 | 81 | ||
| 82 | // Domain returns host name. | 82 | // Domain returns host name. | ... | ... |
| ... | @@ -253,30 +253,20 @@ func (b *BeegoHttpRequest) Body(data interface{}) *BeegoHttpRequest { | ... | @@ -253,30 +253,20 @@ func (b *BeegoHttpRequest) Body(data interface{}) *BeegoHttpRequest { |
| 253 | return b | 253 | return b |
| 254 | } | 254 | } |
| 255 | 255 | ||
| 256 | func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { | 256 | func (b *BeegoHttpRequest) buildUrl(paramBody string) { |
| 257 | if b.resp.StatusCode != 0 { | 257 | // build GET url with query string |
| 258 | return b.resp, nil | ||
| 259 | } | ||
| 260 | var paramBody string | ||
| 261 | if len(b.params) > 0 { | ||
| 262 | var buf bytes.Buffer | ||
| 263 | for k, v := range b.params { | ||
| 264 | buf.WriteString(url.QueryEscape(k)) | ||
| 265 | buf.WriteByte('=') | ||
| 266 | buf.WriteString(url.QueryEscape(v)) | ||
| 267 | buf.WriteByte('&') | ||
| 268 | } | ||
| 269 | paramBody = buf.String() | ||
| 270 | paramBody = paramBody[0 : len(paramBody)-1] | ||
| 271 | } | ||
| 272 | |||
| 273 | if b.req.Method == "GET" && len(paramBody) > 0 { | 258 | if b.req.Method == "GET" && len(paramBody) > 0 { |
| 274 | if strings.Index(b.url, "?") != -1 { | 259 | if strings.Index(b.url, "?") != -1 { |
| 275 | b.url += "&" + paramBody | 260 | b.url += "&" + paramBody |
| 276 | } else { | 261 | } else { |
| 277 | b.url = b.url + "?" + paramBody | 262 | b.url = b.url + "?" + paramBody |
| 278 | } | 263 | } |
| 279 | } else if b.req.Method == "POST" && b.req.Body == nil { | 264 | return |
| 265 | } | ||
| 266 | |||
| 267 | // build POST url and body | ||
| 268 | if b.req.Method == "POST" && b.req.Body == nil { | ||
| 269 | // with files | ||
| 280 | if len(b.files) > 0 { | 270 | if len(b.files) > 0 { |
| 281 | pr, pw := io.Pipe() | 271 | pr, pw := io.Pipe() |
| 282 | bodyWriter := multipart.NewWriter(pw) | 272 | bodyWriter := multipart.NewWriter(pw) |
| ... | @@ -305,12 +295,35 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { | ... | @@ -305,12 +295,35 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { |
| 305 | }() | 295 | }() |
| 306 | b.Header("Content-Type", bodyWriter.FormDataContentType()) | 296 | b.Header("Content-Type", bodyWriter.FormDataContentType()) |
| 307 | b.req.Body = ioutil.NopCloser(pr) | 297 | b.req.Body = ioutil.NopCloser(pr) |
| 308 | } else if len(paramBody) > 0 { | 298 | return |
| 299 | } | ||
| 300 | |||
| 301 | // with params | ||
| 302 | if len(paramBody) > 0 { | ||
| 309 | b.Header("Content-Type", "application/x-www-form-urlencoded") | 303 | b.Header("Content-Type", "application/x-www-form-urlencoded") |
| 310 | b.Body(paramBody) | 304 | b.Body(paramBody) |
| 311 | } | 305 | } |
| 312 | } | 306 | } |
| 307 | } | ||
| 313 | 308 | ||
| 309 | func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { | ||
| 310 | if b.resp.StatusCode != 0 { | ||
| 311 | return b.resp, nil | ||
| 312 | } | ||
| 313 | var paramBody string | ||
| 314 | if len(b.params) > 0 { | ||
| 315 | var buf bytes.Buffer | ||
| 316 | for k, v := range b.params { | ||
| 317 | buf.WriteString(url.QueryEscape(k)) | ||
| 318 | buf.WriteByte('=') | ||
| 319 | buf.WriteString(url.QueryEscape(v)) | ||
| 320 | buf.WriteByte('&') | ||
| 321 | } | ||
| 322 | paramBody = buf.String() | ||
| 323 | paramBody = paramBody[0 : len(paramBody)-1] | ||
| 324 | } | ||
| 325 | |||
| 326 | b.buildUrl(paramBody) | ||
| 314 | url, err := url.Parse(b.url) | 327 | url, err := url.Parse(b.url) |
| 315 | if err != nil { | 328 | if err != nil { |
| 316 | return nil, err | 329 | return nil, err |
| ... | @@ -342,14 +355,12 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { | ... | @@ -342,14 +355,12 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { |
| 342 | } | 355 | } |
| 343 | } | 356 | } |
| 344 | 357 | ||
| 345 | var jar http.CookieJar | 358 | var jar http.CookieJar = nil |
| 346 | if b.setting.EnableCookie { | 359 | if b.setting.EnableCookie { |
| 347 | if defaultCookieJar == nil { | 360 | if defaultCookieJar == nil { |
| 348 | createDefaultCookie() | 361 | createDefaultCookie() |
| 349 | } | 362 | } |
| 350 | jar = defaultCookieJar | 363 | jar = defaultCookieJar |
| 351 | } else { | ||
| 352 | jar = nil | ||
| 353 | } | 364 | } |
| 354 | 365 | ||
| 355 | client := &http.Client{ | 366 | client := &http.Client{ |
| ... | @@ -402,12 +413,11 @@ func (b *BeegoHttpRequest) Bytes() ([]byte, error) { | ... | @@ -402,12 +413,11 @@ func (b *BeegoHttpRequest) Bytes() ([]byte, error) { |
| 402 | return nil, nil | 413 | return nil, nil |
| 403 | } | 414 | } |
| 404 | defer resp.Body.Close() | 415 | defer resp.Body.Close() |
| 405 | data, err := ioutil.ReadAll(resp.Body) | 416 | b.body, err = ioutil.ReadAll(resp.Body) |
| 406 | if err != nil { | 417 | if err != nil { |
| 407 | return nil, err | 418 | return nil, err |
| 408 | } | 419 | } |
| 409 | b.body = data | 420 | return b.body, nil |
| 410 | return data, nil | ||
| 411 | } | 421 | } |
| 412 | 422 | ||
| 413 | // ToFile saves the body data in response to one file. | 423 | // ToFile saves the body data in response to one file. |
| ... | @@ -438,8 +448,7 @@ func (b *BeegoHttpRequest) ToJson(v interface{}) error { | ... | @@ -438,8 +448,7 @@ func (b *BeegoHttpRequest) ToJson(v interface{}) error { |
| 438 | if err != nil { | 448 | if err != nil { |
| 439 | return err | 449 | return err |
| 440 | } | 450 | } |
| 441 | err = json.Unmarshal(data, v) | 451 | return json.Unmarshal(data, v) |
| 442 | return err | ||
| 443 | } | 452 | } |
| 444 | 453 | ||
| 445 | // ToXml returns the map that marshals from the body bytes as xml in response . | 454 | // ToXml returns the map that marshals from the body bytes as xml in response . |
| ... | @@ -449,8 +458,7 @@ func (b *BeegoHttpRequest) ToXml(v interface{}) error { | ... | @@ -449,8 +458,7 @@ func (b *BeegoHttpRequest) ToXml(v interface{}) error { |
| 449 | if err != nil { | 458 | if err != nil { |
| 450 | return err | 459 | return err |
| 451 | } | 460 | } |
| 452 | err = xml.Unmarshal(data, v) | 461 | return xml.Unmarshal(data, v) |
| 453 | return err | ||
| 454 | } | 462 | } |
| 455 | 463 | ||
| 456 | // Response executes request client gets response mannually. | 464 | // Response executes request client gets response mannually. | ... | ... |
| ... | @@ -43,11 +43,7 @@ func NewConn() LoggerInterface { | ... | @@ -43,11 +43,7 @@ func NewConn() LoggerInterface { |
| 43 | // init connection writer with json config. | 43 | // init connection writer with json config. |
| 44 | // json config only need key "level". | 44 | // json config only need key "level". |
| 45 | func (c *ConnWriter) Init(jsonconfig string) error { | 45 | func (c *ConnWriter) Init(jsonconfig string) error { |
| 46 | err := json.Unmarshal([]byte(jsonconfig), c) | 46 | return json.Unmarshal([]byte(jsonconfig), c) |
| 47 | if err != nil { | ||
| 48 | return err | ||
| 49 | } | ||
| 50 | return nil | ||
| 51 | } | 47 | } |
| 52 | 48 | ||
| 53 | // write message in connection. | 49 | // write message in connection. |
| ... | @@ -77,10 +73,9 @@ func (c *ConnWriter) Flush() { | ... | @@ -77,10 +73,9 @@ func (c *ConnWriter) Flush() { |
| 77 | 73 | ||
| 78 | // destroy connection writer and close tcp listener. | 74 | // destroy connection writer and close tcp listener. |
| 79 | func (c *ConnWriter) Destroy() { | 75 | func (c *ConnWriter) Destroy() { |
| 80 | if c.innerWriter == nil { | 76 | if c.innerWriter != nil { |
| 81 | return | ||
| 82 | } | ||
| 83 | c.innerWriter.Close() | 77 | c.innerWriter.Close() |
| 78 | } | ||
| 84 | } | 79 | } |
| 85 | 80 | ||
| 86 | func (c *ConnWriter) connect() error { | 81 | func (c *ConnWriter) connect() error { | ... | ... |
| ... | @@ -50,9 +50,10 @@ type ConsoleWriter struct { | ... | @@ -50,9 +50,10 @@ type ConsoleWriter struct { |
| 50 | 50 | ||
| 51 | // create ConsoleWriter returning as LoggerInterface. | 51 | // create ConsoleWriter returning as LoggerInterface. |
| 52 | func NewConsole() LoggerInterface { | 52 | func NewConsole() LoggerInterface { |
| 53 | cw := new(ConsoleWriter) | 53 | cw := &ConsoleWriter{ |
| 54 | cw.lg = log.New(os.Stdout, "", log.Ldate|log.Ltime) | 54 | lg: log.New(os.Stdout, "", log.Ldate|log.Ltime), |
| 55 | cw.Level = LevelDebug | 55 | Level: LevelDebug, |
| 56 | } | ||
| 56 | return cw | 57 | return cw |
| 57 | } | 58 | } |
| 58 | 59 | ||
| ... | @@ -62,11 +63,7 @@ func (c *ConsoleWriter) Init(jsonconfig string) error { | ... | @@ -62,11 +63,7 @@ func (c *ConsoleWriter) Init(jsonconfig string) error { |
| 62 | if len(jsonconfig) == 0 { | 63 | if len(jsonconfig) == 0 { |
| 63 | return nil | 64 | return nil |
| 64 | } | 65 | } |
| 65 | err := json.Unmarshal([]byte(jsonconfig), c) | 66 | return json.Unmarshal([]byte(jsonconfig), c) |
| 66 | if err != nil { | ||
| 67 | return err | ||
| 68 | } | ||
| 69 | return nil | ||
| 70 | } | 67 | } |
| 71 | 68 | ||
| 72 | // write message in console. | 69 | // write message in console. |
| ... | @@ -76,9 +73,10 @@ func (c *ConsoleWriter) WriteMsg(msg string, level int) error { | ... | @@ -76,9 +73,10 @@ func (c *ConsoleWriter) WriteMsg(msg string, level int) error { |
| 76 | } | 73 | } |
| 77 | if goos := runtime.GOOS; goos == "windows" { | 74 | if goos := runtime.GOOS; goos == "windows" { |
| 78 | c.lg.Println(msg) | 75 | c.lg.Println(msg) |
| 79 | } else { | 76 | return nil |
| 80 | c.lg.Println(colors[level](msg)) | ||
| 81 | } | 77 | } |
| 78 | c.lg.Println(colors[level](msg)) | ||
| 79 | |||
| 82 | return nil | 80 | return nil |
| 83 | } | 81 | } |
| 84 | 82 | ... | ... |
| ... | @@ -123,11 +123,7 @@ func (w *FileLogWriter) startLogger() error { | ... | @@ -123,11 +123,7 @@ func (w *FileLogWriter) startLogger() error { |
| 123 | return err | 123 | return err |
| 124 | } | 124 | } |
| 125 | w.mw.SetFd(fd) | 125 | w.mw.SetFd(fd) |
| 126 | err = w.initFd() | 126 | return w.initFd() |
| 127 | if err != nil { | ||
| 128 | return err | ||
| 129 | } | ||
| 130 | return nil | ||
| 131 | } | 127 | } |
| 132 | 128 | ||
| 133 | func (w *FileLogWriter) docheck(size int) { | 129 | func (w *FileLogWriter) docheck(size int) { |
| ... | @@ -170,14 +166,13 @@ func (w *FileLogWriter) initFd() error { | ... | @@ -170,14 +166,13 @@ func (w *FileLogWriter) initFd() error { |
| 170 | } | 166 | } |
| 171 | w.maxsize_cursize = int(finfo.Size()) | 167 | w.maxsize_cursize = int(finfo.Size()) |
| 172 | w.daily_opendate = time.Now().Day() | 168 | w.daily_opendate = time.Now().Day() |
| 169 | w.maxlines_curlines = 0 | ||
| 173 | if finfo.Size() > 0 { | 170 | if finfo.Size() > 0 { |
| 174 | count, err := w.lines() | 171 | count, err := w.lines() |
| 175 | if err != nil { | 172 | if err != nil { |
| 176 | return err | 173 | return err |
| 177 | } | 174 | } |
| 178 | w.maxlines_curlines = count | 175 | w.maxlines_curlines = count |
| 179 | } else { | ||
| 180 | w.maxlines_curlines = 0 | ||
| 181 | } | 176 | } |
| 182 | return nil | 177 | return nil |
| 183 | } | 178 | } | ... | ... |
| ... | @@ -292,9 +292,9 @@ func (bl *BeeLogger) Close() { | ... | @@ -292,9 +292,9 @@ func (bl *BeeLogger) Close() { |
| 292 | fmt.Println("ERROR, unable to WriteMsg (while closing logger):", err) | 292 | fmt.Println("ERROR, unable to WriteMsg (while closing logger):", err) |
| 293 | } | 293 | } |
| 294 | } | 294 | } |
| 295 | } else { | 295 | continue |
| 296 | break | ||
| 297 | } | 296 | } |
| 297 | break | ||
| 298 | } | 298 | } |
| 299 | for _, l := range bl.outputs { | 299 | for _, l := range bl.outputs { |
| 300 | l.Flush() | 300 | l.Flush() | ... | ... |
| ... | @@ -25,7 +25,8 @@ import ( | ... | @@ -25,7 +25,8 @@ import ( |
| 25 | ) | 25 | ) |
| 26 | 26 | ||
| 27 | const ( | 27 | const ( |
| 28 | subjectPhrase = "Diagnostic message from server" | 28 | // no usage |
| 29 | // subjectPhrase = "Diagnostic message from server" | ||
| 29 | ) | 30 | ) |
| 30 | 31 | ||
| 31 | // smtpWriter implements LoggerInterface and is used to send emails via given SMTP-server. | 32 | // smtpWriter implements LoggerInterface and is used to send emails via given SMTP-server. |
| ... | @@ -146,9 +147,7 @@ func (s *SmtpWriter) WriteMsg(msg string, level int) error { | ... | @@ -146,9 +147,7 @@ func (s *SmtpWriter) WriteMsg(msg string, level int) error { |
| 146 | mailmsg := []byte("To: " + strings.Join(s.RecipientAddresses, ";") + "\r\nFrom: " + s.FromAddress + "<" + s.FromAddress + | 147 | mailmsg := []byte("To: " + strings.Join(s.RecipientAddresses, ";") + "\r\nFrom: " + s.FromAddress + "<" + s.FromAddress + |
| 147 | ">\r\nSubject: " + s.Subject + "\r\n" + content_type + "\r\n\r\n" + fmt.Sprintf(".%s", time.Now().Format("2006-01-02 15:04:05")) + msg) | 148 | ">\r\nSubject: " + s.Subject + "\r\n" + content_type + "\r\n\r\n" + fmt.Sprintf(".%s", time.Now().Format("2006-01-02 15:04:05")) + msg) |
| 148 | 149 | ||
| 149 | err := s.sendMail(s.Host, auth, s.FromAddress, s.RecipientAddresses, mailmsg) | 150 | return s.sendMail(s.Host, auth, s.FromAddress, s.RecipientAddresses, mailmsg) |
| 150 | |||
| 151 | return err | ||
| 152 | } | 151 | } |
| 153 | 152 | ||
| 154 | // implementing method. empty. | 153 | // implementing method. empty. | ... | ... |
| ... | @@ -323,7 +323,7 @@ func Exception(errcode string, w http.ResponseWriter, r *http.Request, msg strin | ... | @@ -323,7 +323,7 @@ func Exception(errcode string, w http.ResponseWriter, r *http.Request, msg strin |
| 323 | w.WriteHeader(isint) | 323 | w.WriteHeader(isint) |
| 324 | h(w, r) | 324 | h(w, r) |
| 325 | return | 325 | return |
| 326 | } else { | 326 | } |
| 327 | isint, err := strconv.Atoi(errcode) | 327 | isint, err := strconv.Atoi(errcode) |
| 328 | if err != nil { | 328 | if err != nil { |
| 329 | isint = 500 | 329 | isint = 500 |
| ... | @@ -334,6 +334,4 @@ func Exception(errcode string, w http.ResponseWriter, r *http.Request, msg strin | ... | @@ -334,6 +334,4 @@ func Exception(errcode string, w http.ResponseWriter, r *http.Request, msg strin |
| 334 | w.Header().Set("Content-Type", "text/plain; charset=utf-8") | 334 | w.Header().Set("Content-Type", "text/plain; charset=utf-8") |
| 335 | w.WriteHeader(isint) | 335 | w.WriteHeader(isint) |
| 336 | fmt.Fprintln(w, msg) | 336 | fmt.Fprintln(w, msg) |
| 337 | return | ||
| 338 | } | ||
| 339 | } | 337 | } | ... | ... |
| ... | @@ -34,7 +34,6 @@ type Translation struct { | ... | @@ -34,7 +34,6 @@ type Translation struct { |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | func NewLocale(filepath string, defaultlocal string) *Translation { | 36 | func NewLocale(filepath string, defaultlocal string) *Translation { |
| 37 | i18n := make(map[string]map[string]string) | ||
| 38 | file, err := os.Open(filepath) | 37 | file, err := os.Open(filepath) |
| 39 | if err != nil { | 38 | if err != nil { |
| 40 | panic("open " + filepath + " err :" + err.Error()) | 39 | panic("open " + filepath + " err :" + err.Error()) |
| ... | @@ -43,8 +42,9 @@ func NewLocale(filepath string, defaultlocal string) *Translation { | ... | @@ -43,8 +42,9 @@ func NewLocale(filepath string, defaultlocal string) *Translation { |
| 43 | if err != nil { | 42 | if err != nil { |
| 44 | panic("read " + filepath + " err :" + err.Error()) | 43 | panic("read " + filepath + " err :" + err.Error()) |
| 45 | } | 44 | } |
| 46 | err = json.Unmarshal(data, &i18n) | 45 | |
| 47 | if err != nil { | 46 | i18n := make(map[string]map[string]string) |
| 47 | if err = json.Unmarshal(data, &i18n); err != nil { | ||
| 48 | panic("json.Unmarshal " + filepath + " err :" + err.Error()) | 48 | panic("json.Unmarshal " + filepath + " err :" + err.Error()) |
| 49 | } | 49 | } |
| 50 | return &Translation{ | 50 | return &Translation{ | ... | ... |
-
Please register or sign in to post a comment