Commit df5327e6 by usual2970

添加beego作为对外服务

parent 65641bfe
...@@ -2,11 +2,17 @@ package main ...@@ -2,11 +2,17 @@ package main
import ( import (
"github.com/usual2970/util/tel" "github.com/usual2970/util/tel"
"fmt"
//"github.com/usual2970/util/request"
) )
func main() { func main() {
tel := tel.NewTel("18257140570", "15000210629") tel := tel.NewTel("15000000000", "15000000000")
//var resp request.Resp
tel.Dial() resp,err:=tel.Dial()
if err!=nil{
fmt.Println(err)
}
fmt.Println(resp)
} }
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
"github.com/usual2970/util/time" "github.com/usual2970/util/time"
"crypto/sha256" "crypto/sha256"
"encoding/base64" "encoding/base64"
"io/ioutil"
) )
...@@ -19,6 +20,11 @@ type Client struct { ...@@ -19,6 +20,11 @@ type Client struct {
httpClient *http.Client httpClient *http.Client
} }
type Resp struct {
Code,Description,Sessionid string
}
func (clt *Client) AppKey() string { func (clt *Client) AppKey() string {
return clt.appKey return clt.appKey
} }
...@@ -37,9 +43,8 @@ func NewClient(appKey,appSecret string, httpClient *http.Client) *Client { ...@@ -37,9 +43,8 @@ func NewClient(appKey,appSecret string, httpClient *http.Client) *Client {
} }
} }
func (clt *Client) Post(url string,params interface{}) (resp interface{},err error){ func (clt *Client) Post(url string,params interface{}) (resp Resp,err error){
pBytes,err:=ffjson.Marshal(params) pBytes,err:=ffjson.Marshal(params)
fmt.Println(string(pBytes))
if err!=nil{ if err!=nil{
return return
} }
...@@ -50,23 +55,23 @@ func (clt *Client) Post(url string,params interface{}) (resp interface{},err err ...@@ -50,23 +55,23 @@ func (clt *Client) Post(url string,params interface{}) (resp interface{},err err
return return
} }
ctime:=time.NowCm() ctime:=time.NowCmUtc()
nonce:=string(random.RandomCreateBytes(64)) nonce:=string(random.RandomCreateBytes(64))
pwDigest:=clt.PwDigest(ctime,nonce) pwDigest:=clt.PwDigest(ctime,nonce)
headers:=http.Header{
req.Header.Add("Authorization","WSSE realm=\"SDP\", profile=\"UsernameToken\", type=\"AppKey\"") "Authorization":[]string{"WSSE realm=\"SDP\", profile=\"UsernameToken\", type=\"AppKey\""},
req.Header.Add("X-WSSE", "X-WSSE":[]string{fmt.Sprintf(`UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"`,
fmt.Sprintf(`UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"`,
clt.AppKey(), clt.AppKey(),
pwDigest, pwDigest,
nonce, nonce,
ctime, ctime,
)) )},
req.Header.Add("Content-Type","application/json; charset=UTF-8") "Content-Type":[]string{"application/json; charset=UTF-8"},
}
req.Header=headers
response,err:=clt.httpClient.Do(req) response,err:=clt.httpClient.Do(req)
if err!=nil{ if err!=nil{
...@@ -74,7 +79,21 @@ func (clt *Client) Post(url string,params interface{}) (resp interface{},err err ...@@ -74,7 +79,21 @@ func (clt *Client) Post(url string,params interface{}) (resp interface{},err err
return return
} }
defer response.Body.Close() defer response.Body.Close()
fmt.Println(response)
rsBody,err:=ioutil.ReadAll(response.Body)
if err!=nil {
fmt.Println(err)
return
}
err=ffjson.Unmarshal(rsBody,&resp)
if err!=nil{
fmt.Println(err)
return
}
fmt.Println(resp)
return return
} }
......
appname = service
httpport = 8080
runmode = dev
package controllers
import (
"github.com/astaxie/beego"
)
type MainController struct {
beego.Controller
}
func (c *MainController) Get() {
c.Data["Website"] = "beego.me"
c.Data["Email"] = "astaxie@gmail.com"
c.TplName = "index.tpl"
}
package main
import (
_ "github.com/usual2970/util/service/routers"
"github.com/astaxie/beego"
)
func main() {
beego.Run()
}
package routers
import (
"github.com/usual2970/util/service/controllers"
"github.com/astaxie/beego"
)
func init() {
beego.Router("/", &controllers.MainController{})
}
package test
import (
"net/http"
"net/http/httptest"
"testing"
"runtime"
"path/filepath"
_ "github.com/usual2970/util/service/routers"
"github.com/astaxie/beego"
. "github.com/smartystreets/goconvey/convey"
)
func init() {
_, file, _, _ := runtime.Caller(1)
apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".." + string(filepath.Separator))))
beego.TestBeegoInit(apppath)
}
// TestMain is a sample to run an endpoint test
func TestMain(t *testing.T) {
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
beego.BeeApp.Handlers.ServeHTTP(w, r)
beego.Trace("testing", "TestMain", "Code[%d]\n%s", w.Code, w.Body.String())
Convey("Subject: Test Station Endpoint\n", t, func() {
Convey("Status Code Should Be 200", func() {
So(w.Code, ShouldEqual, 200)
})
Convey("The Result Should Not Be Empty", func() {
So(w.Body.Len(), ShouldBeGreaterThan, 0)
})
})
}
...@@ -3,6 +3,7 @@ package tel ...@@ -3,6 +3,7 @@ package tel
import( import(
"github.com/usual2970/util/request" "github.com/usual2970/util/request"
"fmt"
) )
...@@ -35,7 +36,7 @@ func NewTel(from, to string) *Tel { ...@@ -35,7 +36,7 @@ func NewTel(from, to string) *Tel {
} }
} }
func (t *Tel) Dial() (v interface{},err error) { func (t *Tel) Dial() (resp request.Resp,err error) {
tlsClient,err:=request.NewTLSHttpClientSingle(CerPath) tlsClient,err:=request.NewTLSHttpClientSingle(CerPath)
if err!=nil{ if err!=nil{
return return
...@@ -43,7 +44,9 @@ func (t *Tel) Dial() (v interface{},err error) { ...@@ -43,7 +44,9 @@ func (t *Tel) Dial() (v interface{},err error) {
client:=request.NewClient(AppKey,AppSecret,tlsClient) client:=request.NewClient(AppKey,AppSecret,tlsClient)
client.Post(DialUrl,t.Params) if resp,err=client.Post(DialUrl,t.Params);err!=nil{
fmt.Println(err)
}
return return
} }
......
...@@ -7,3 +7,8 @@ import ( ...@@ -7,3 +7,8 @@ import (
func NowCm() string{ func NowCm() string{
return time.Now().Format("2006-01-02T15:04:05Z") return time.Now().Format("2006-01-02T15:04:05Z")
} }
func NowCmUtc() string{
return time.Now().UTC().Format("2006-01-02T15:04:05Z")
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment