ls 4 years ago
parent
commit
febdc76aa6
4 changed files with 22 additions and 14 deletions
  1. 12 12
      router.go
  2. 4 2
      tree.go
  3. 3 0
      wechat/client.go
  4. 3 0
      wechat/request.go

+ 12 - 12
router.go

@@ -160,37 +160,37 @@ func NewRouter() *Router {
 
 // GET is a shortcut for router.Handle("GET", path, handle)
 func (r *Router) GET(path string, handle Handle) {
-	r.Handle("GET", path, handle)
+	r.Handle(http.MethodGet, path, handle)
 }
 
 // HEAD is a shortcut for router.Handle("HEAD", path, handle)
 func (r *Router) HEAD(path string, handle Handle) {
-	r.Handle("HEAD", path, handle)
+	r.Handle(http.MethodHead, path, handle)
 }
 
 // OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle)
 func (r *Router) OPTIONS(path string, handle Handle) {
-	r.Handle("OPTIONS", path, handle)
+	r.Handle(http.MethodOptions, path, handle)
 }
 
 // POST is a shortcut for router.Handle("POST", path, handle)
 func (r *Router) POST(path string, handle Handle) {
-	r.Handle("POST", path, handle)
+	r.Handle(http.MethodPost, path, handle)
 }
 
 // PUT is a shortcut for router.Handle("PUT", path, handle)
 func (r *Router) PUT(path string, handle Handle) {
-	r.Handle("PUT", path, handle)
+	r.Handle(http.MethodPut, path, handle)
 }
 
 // PATCH is a shortcut for router.Handle("PATCH", path, handle)
 func (r *Router) PATCH(path string, handle Handle) {
-	r.Handle("PATCH", path, handle)
+	r.Handle(http.MethodPatch, path, handle)
 }
 
 // DELETE is a shortcut for router.Handle("DELETE", path, handle)
 func (r *Router) DELETE(path string, handle Handle) {
-	r.Handle("DELETE", path, handle)
+	r.Handle(http.MethodDelete, path, handle)
 }
 
 // Handle registers a new request handle with the given path and method.
@@ -283,7 +283,7 @@ func (r *Router) Lookup(method, path string) (Handle, map[string]string, bool) {
 func (r *Router) allowed(path, reqMethod string) (allow string) {
 	if path == "*" { // server-wide
 		for method := range r.trees {
-			if method == "OPTIONS" {
+			if method == http.MethodOptions {
 				continue
 			}
 
@@ -297,7 +297,7 @@ func (r *Router) allowed(path, reqMethod string) (allow string) {
 	} else { // specific path
 		for method := range r.trees {
 			// Skip the requested method - we already tried this one
-			if method == reqMethod || method == "OPTIONS" {
+			if method == reqMethod || method == http.MethodOptions {
 				continue
 			}
 
@@ -332,9 +332,9 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
 			req = setCurrentPath(req, path)
 			handle(w, req)
 			return
-		} else if req.Method != "CONNECT" && path != "/" {
+		} else if req.Method != http.MethodConnect && path != "/" {
 			code := 301 // Permanent redirect, request with GET method
-			if req.Method != "GET" {
+			if req.Method != http.MethodGet {
 				// Temporary redirect, request with same method
 				// As of Go 1.3, Go does not support status code 308.
 				code = 307
@@ -365,7 +365,7 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
 		}
 	}
 
-	if req.Method == "OPTIONS" && r.HandleOPTIONS {
+	if req.Method == http.MethodOptions && r.HandleOPTIONS {
 		// Handle OPTIONS requests
 		if allow := r.allowed(path, req.Method); len(allow) > 0 {
 			w.Header().Set("Allow", allow)

+ 4 - 2
tree.go

@@ -17,6 +17,8 @@ func min(a, b int) int {
 	return b
 }
 
+const maxParamCount uint8 = ^uint8(0)
+
 func countParams(path string) uint8 {
 	var n uint
 	for i := 0; i < len(path); i++ {
@@ -25,8 +27,8 @@ func countParams(path string) uint8 {
 		}
 		n++
 	}
-	if n >= 255 {
-		return 255
+	if n >= uint(maxParamCount) {
+		return maxParamCount
 	}
 	return uint8(n)
 }

+ 3 - 0
wechat/client.go

@@ -26,8 +26,11 @@ func NewClient(appID, appSecret, token, encodingAESKey string) *Client {
 // getToken get token
 func (wc *Client) getToken() (token string, err error) {
 	now := time.Now().Unix()
+	fmt.Println("now", now, wc.LastTokenTime)
 	if wc.LastTokenTime > 0 {
+		fmt.Println("wc.LastTokenTime", wc.LastTokenTime, TokenExpires)
 		if now-wc.LastTokenTime < TokenExpires {
+			fmt.Println("token = wc.AccessToken")
 			token = wc.AccessToken
 			return
 		}

+ 3 - 0
wechat/request.go

@@ -5,11 +5,13 @@ import (
 	"encoding/json"
 	"encoding/xml"
 	"errors"
+	"fmt"
 
 	"git.chuangxin1.com/cx/myth"
 )
 
 func checkJSONError(msg myth.HTTPMessage) (jq *myth.JSONQuery, err error) {
+	fmt.Println(msg)
 	if msg.StatusCode == 200 {
 		jq, err = msg.JSONQuery()
 		if err != nil {
@@ -44,6 +46,7 @@ func getJSON(uri string) (jq *myth.JSONQuery, err error) {
 	headers["Accept-Encoding"] = "gzip, deflate, br"
 	headers["Accept"] = "application/json"
 	if msg, err = myth.Get(uri, "", "", headers); err != nil {
+		fmt.Println("myth.Get", uri, string(msg.Body))
 		return
 	}
 	jq, err = checkJSONError(msg)