日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

linux 多目录makefile,royalchen

發布時間:2023/11/27 生活经验 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux 多目录makefile,royalchen 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近接觸了一下微信公眾號開發,剛好很久很久之前申請過一個公眾號,看了下API文檔,用go實現了一個簡單的獲取 access_token的功能。 下面的代碼可以從微信服務器獲取access_tokens,并保存在內存中,然后其他程序可以通過http請求獲取到內存中的 access_token,超時的時候,還會自動重新從微信服務器獲取新的access_tokens。 只需要配置 config.json文件即可,config.json和main.go文件處于同一目錄層級,config.json格式文件配置如下:

{

“appid”:”XXX”,

“secret”:”XXX”,

“token”:”XXX”,

“port”:”7001”

}

其中 appid,secret以及token需要在微信公眾平臺獲取相關信息。

port 則是你使用的監聽接口,即其他人通過這個接口獲取access_tokens。 獲取的方式是通過瀏覽器訪問你的 url地址+/token 后綴。

運行時,輸入 go run main.go config.json 即可

代碼: main.go

package main

import (

“bytes”

“encoding/json”

“io/ioutil”

“log”

“net/http”

“os”

“time”

)

const wx_params_timestamp string = “timestamp”

const wx_params_signature string = “signature”

const wx_params_nonce string = “nonce”

const wx_params_echostr string = “echostr”

const wx_access_token_key string = “access_token”

const wx_access_token_time_key string = “expires_in”

const wx_access_token_error_code string = “errcode”

const wx_access_token_error_msg string = “errmsg”

//

var wx_gzh_token_str string

var wx_gzh_appid string

var wx_gzh_secret string

var global_access_token string

var global_access_token_time_out int64

type SConsConfig struct {

Appid string `json:”appid”`

Secret string `json:”secret”`

Token string `json:”token”`

Port string `json:”port”`

}

func NewJsonStruct() *SConsConfig {

return &SConsConfig{}

}

func (jst *SConsConfig) LoadConfig(fileName string, v interface{}) {

data, err := ioutil.ReadFile(fileName)

if err != nil {

return

}

err = json.Unmarshal(data, v)

if err != nil {

return

}

}

func ParseSconsConfig(fileName string) (v SConsConfig, err error) {

JsonParse := NewJsonStruct()

v = SConsConfig{}

JsonParse.LoadConfig(fileName, &v)

return v, nil

}

type wx_access_token_center_handler struct{}

func (h *wx_access_token_center_handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

log.Println(“somebody require token”)

current_time := time.Now().Unix()

if current_time > global_access_token_time_out {

log.Println(“token timetou ,require from wx………..”)

GetAccessToken(wx_gzh_appid, wx_gzh_secret)

} else {

log.Println(“token is valid,require from local”)

}

w.Write([]byte(global_access_token))

}

func ParseTokens(str string) {

decoder := json.NewDecoder(bytes.NewBufferString(str))

decoder.UseNumber()

var result map[string]interface{}

if err := decoder.Decode(&result); err != nil {

log.Println(“json parse failed,str=”, str, “\nerr=”, err)

os.Exit(1)

}

log.Println(“parse”, str, “json”)

if _, ok := result[wx_access_token_error_code]; ok {

log.Println(“error token,code=”, result[wx_access_token_error_code])

log.Println(“errmsg=”, result[wx_access_token_error_msg])

}

access_token := result[wx_access_token_key]

access_token_value, ok := access_token.(string)

if !ok {

log.Println(“parse access_token failed,access_token=”, access_token)

}

log.Println(“access_token_value:”, access_token_value)

time_num := result[wx_access_token_time_key]

time_num_value, err := time_num.(json.Number).Int64()

if err != nil {

log.Println(“parase time_num failed,time_num=”, time_num)

}

log.Println(“time:”, time_num_value)

current_time := time.Now().Unix()

global_access_token = access_token_value

global_access_token_time_out = current_time + time_num_value

formatTimeStr := time.Unix(global_access_token_time_out, 0).Format(“2006-01-02 15:04:05”)

log.Println(“success parse json,token will be timeout at “, global_access_token_time_out, “,”, formatTimeStr)

}

func GetAccessToken(appid string, secret string) {

params := “/cgi-bin/token?grant_type=client_credential&appid=” + appid + “&secret=” + secret

urls := []string{“api.weixin.qq.com”, “api2.weixin.qq.com”, “sh.api.weixin.qq.com”, “sz.api.weixin.qq.com”, “hk.api.weixin.qq.com”}

success := false

for _, v := range urls {

url := “https://“ + v + params

resp, err := http.Get(url)

if nil != err {

log.Println(“get wx access token from “+url+” error,err=”, err)

} else {

success = true

log.Println(“get wx access token from “ + url + “ success”)

defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)

if nil != err {

log.Println(“get wx access token error,read resp body error,err=”, err)

return

}

tokenstr := string(body)

log.Println(“access_token:”, tokenstr)

ParseTokens(tokenstr)

break

}

}

if !success {

log.Println(“get wx access error from all api”)

os.Exit(1)

}

}

func InitGlobal() {

wx_gzh_appid = “”

wx_gzh_secret = “”

wx_gzh_token_str = “”

global_access_token = “”

global_access_token_time_out = 0

}

func HttpTokenServer(tokenPort string) {

http.Handle(“/token”, &wx_access_token_center_handler{})

port := “0.0.0.0:” + tokenPort

log.Println(“start listen token port:”, tokenPort)

http.ListenAndServe(port, nil)

}

func main() {

log.Println(“start gzh token center”)

log.Println(“param format: config.json”)

arg_num := len(os.Args)

if arg_num < 2 {

log.Println(“please input the port”)

os.Exit(1)

}

InitGlobal()

configFile := os.Args[1]

config, err := ParseSconsConfig(configFile)

if nil != err {

log.Println(“parse config error,err=”, err)

os.Exit(1)

}

wx_gzh_appid = config.Appid

wx_gzh_secret = config.Secret

wx_gzh_token_str = config.Token

HttpTokenServer(config.Port)

}

總結

以上是生活随笔為你收集整理的linux 多目录makefile,royalchen的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。