Beispiel #1
0
// 处理所有来自微信的消息,已经验证过 URL 和 Method 了
func processMessage(w http.ResponseWriter, r *http.Request) {
	q := r.URL.Query()
	timestamp := q.Get("timestamp")
	nonce := q.Get("nonce")
	encryptType := q.Get("encrypt_type")
	msgSignature := q.Get("msg_signature")

	// 读取报文
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		log.Error(err)
		http.Error(w, "read body error", http.StatusNotAcceptable)
		return
	}
	log.Debugf("from weixin: %s", body)

	msg, err := parseBody(encryptType, timestamp, nonce, msgSignature, body)
	if err != nil {
		log.Error(err)
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	// 处理消息
	reply := HandleMessage(msg)

	// 如果返回为 nil,表示不需要回复,结束
	if reply == nil {
		return
	}

	// 如果返回不为 nil,表示需要回复
	ret, err := packReply(reply, encryptType, timestamp, nonce)
	if err != nil {
		log.Error(err)
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	log.Debugf("to weixin: %s", ret)
	w.Header().Set("Content-Type", "text/xml; charset=utf-8")
	w.Write(ret)
}
Beispiel #2
0
func execColorizedExamples() {
	log.SetLevel(log.Lall)
	log.Info("default config")

	log.Colorized(true)
	log.Info("colorized config")

	log.Colorized(false)
	log.Error("close colorized config")
}
Beispiel #3
0
func main() {
	log.Debugf("this is a test message, %d", 1111)

	format := fmt.Sprintf("%s %s %s %s:%d %s", "2006-01-02 15:04:05.000000", log.TagToken,
		log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken)
	log.ChangeFormat(format)
	log.Tinfof("6ba7b814-9dad-11d1-80b4-00c04fd430c8", "this is a test message, %d", 1111)

	format = fmt.Sprintf(`{"date": "%s", "time": "%s", "level": "%s", "file": "%s", "line": %d, "log": "%s"}`,
		"2006-01-02", "15:04:05.999", log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken)
	log.ChangeFormat(format)
	log.Infof("this is a test message, %d", 1111)

	format = fmt.Sprintf(`<log><date>%s</date><time>%s</time><level>%s</level><file>%s</file><line>%d</line><msg>%s</msg><log>`,
		"2006-01-02", "15:04:05.000", log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken)
	log.ChangeFormat(format)
	log.Tinfof("6ba7b814-9dad-11d1-80b4-00c04fd430c8", "this is a test message, %d", 1111)

	log.Error("level = debug")
	log.Infof("this is a test message, %d", 1111)
	log.Errorf("this is another test message, %d", 22222)
	// Fatalf("%d %s", log.FatalLevel, log.FatalLevel)

	format = fmt.Sprintf("%s %s %s %s:%d %s", "2006-1-2", "3:4:05.9",
		log.LevelToken, log.PathToken, log.LineToken, log.MessageToken)
	log.ChangeFormat(format)
	log.Infof("this is a test message, %d", 1111)

	format = fmt.Sprintf("%s %s %s %s:%d %s", "2006-01-02", "15:04:05.999999",
		log.LevelToken, log.PackageToken, log.LineToken, log.MessageToken)
	log.ChangeFormat(format)
	log.Infof("this is a test message, %d", 1111)

	format = fmt.Sprintf("%s %s %s:%d %s", "2006-01-02 15:04:05.000000",
		log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken)
	log.ChangeFormat(format)
	log.Infof("this is a test message, %d", 1111)

	format = fmt.Sprintf(`{"date": "%s", "time": "%s", "level": "%s", "file": "%s", "line": %d, "log": "%s"}`,
		"2006-01-02", "15:04:05.999", log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken)
	log.ChangeFormat(format)
	log.Infof("this is a test message, %d", 1111)

	format = fmt.Sprintf(`<log><date>%s</date><time>%s</time><level>%s</level><file>%s</file><line>%d</line><msg>%s</msg><log>`,
		"2006-01-02", "15:04:05.000", log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken)
	log.ChangeFormat(format)
	log.Infof("this is a test message, %d", 1111)
}
Beispiel #4
0
func refresh(url string, ns ...int) (new *accessToken) {
	n := 0
	if len(ns) > 0 {
		n = ns[0]
	}

	var err error
	defer func() {
		if err != nil {
			log.Error(err)
			time.Sleep(3 * time.Minute)
			if n < 9 {
				n++
				new = refresh(url, n)
			}
		}
	}()

	resp, err := http.Get(url)
	if err != nil {
		return
	}

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}
	resp.Body.Close()

	new = &accessToken{}
	err = json.Unmarshal(body, new)
	if err != nil {
		return
	}

	return new
}