Using the go language to implement a micro-public platform _golang

Source: Internet
Author: User
Tags sha1

This is not all code Oh, just a demo can verify and accept the post passed the message and can return the message, the middle of the reply to the logic of the need to wait for your comrades to write their own AH

Copy Code code as follows:

/*
* @go language to achieve public platform
*/
Package Main
Import (
"CRYPTO/SHA1"
"Encoding/xml"
"FMT"
"IO"
"Io/ioutil"
"Log"
"Net/http"
"Sort"
"Strings"
"Time"
)
Type Request struct {
Tousername string
Fromusername string
Createtime time. Duration
Msgtype string
Content string
MsgId int
}
Type Response struct {
Tousername string ' xml: ' Xml>tousername '
Fromusername string ' xml: ' Xml>fromusername '
Createtime string ' xml: ' Xml>createtime '
Msgtype string ' xml: ' Xml>msgtype '
Content string ' xml: ' Xml>content '
MsgId int ' xml: ' Xml>msgid '
}
Func str2sha1 (data string) string {
T: = SHA1. New ()
Io. WriteString (t, data)
Return FMT. Sprintf ("%x", T.sum (nil))
}
Func Action (w http. Responsewriter, R *http. Request) {
Postedmsg, err: = Ioutil. ReadAll (R.body)
If Err!= nil {
Log. Fatal (ERR)
}
R.body.close ()
V: = request{}
Xml. Unmarshal (postedmsg, &v)
if V.msgtype = = "Text" {
V: = Request{v.tousername, V.fromusername, V.createtime, V.msgtype, V.content, V.msgid}
Output, err: = XML. Marshalindent (V, "", "")
If Err!= nil {
Fmt. Printf ("error:%v\n", err)
}
Fmt. fprintf (w, string (output))
else if V.msgtype = = "Event" {
Content: = ' "Welcome attention
My micro-letter "'
V: = Request{v.tousername, V.fromusername, V.createtime, V.msgtype, Content, V.msgid}
Output, err: = XML. Marshalindent (V, "", "")
If Err!= nil {
Fmt. Printf ("error:%v\n", err)
}
Fmt. fprintf (w, string (output))
}
}
Func checksignature (w http. Responsewriter, R *http. Request) {
R.parseform ()
var token string = "Your token"
var signature string = strings. Join (r.form["signature"], "")
var timestamp string = strings. Join (r.form["timestamp"], "")
var nonce string = strings. Join (r.form["nonce"], "")
var echostr string = strings. Join (r.form["Echostr"], "")
Tmps: = []string{token, timestamp, nonce}
Sort. Strings (Tmps)
TMPSTR: = Tmps[0] + tmps[1] + tmps[2]
TMP: = STR2SHA1 (TMPSTR)
if tmp = = Signature {
Fmt. fprintf (W, ECHOSTR)
}
}
Func Main () {
http. Handlefunc ("/check", Checksignature)
http. Handlefunc ("/", action)
http. Listenandserve (": 8080", nil)
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.