/
smsadapter.go
68 lines (57 loc) · 1.58 KB
/
smsadapter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"flag"
"path/filepath"
"github.com/ant0ine/go-json-rest/rest"
"log"
"net/http"
"strconv"
"github.com/Unknwon/goconfig"
"github.com/xjplke/smsadapter/sms"
"fmt"
)
type Message struct {
Body string
}
func main() {
path := flag.String("config", "./smsadapter.conf", "设置配置文件的路径")
flag.Parse()
*path = filepath.FromSlash(*path)
c, err := goconfig.LoadConfigFile(*path)
if err != nil {
log.Fatal(err)
}
ip, iperr := c.GetValue("local","listen")
if iperr != nil {
log.Printf("%v use default:0.0.0.0",iperr)
ip = "0.0.0.0"
}
port,porterr := c.Int("local","port")
if porterr != nil {
log.Panicf("%v,use default:8500",porterr)
port = 8500
}
smssender := sms.NewSmsHttp()
smssender.Init(c)
fmt.Println("listen on "+ip+":"+strconv.Itoa(port))
handler := rest.ResourceHandler{}
errx := handler.SetRoutes(
&rest.Route{"GET", "/message", func(w rest.ResponseWriter, req *rest.Request) {
w.WriteJson(&Message{
Body: "Hello World!",
})
}},
&rest.Route{"GET","/sms/send",func(w rest.ResponseWriter,req *rest.Request){
w.Header().Set("Content-Type","Application/json; charset=utf-8")
phone := req.FormValue("phone")
password := req.FormValue("password")
rsp := smssender.Send(phone,password)
w.WriteJson(rsp)
}},
)
if errx != nil {
log.Fatal(errx)
}
log.Fatal(http.ListenAndServe(ip+":"+strconv.Itoa(port), &handler))
fmt.Println("exit1")
}