-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
150 lines (123 loc) · 2.92 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/leezer3379/voice-sender/dataobj"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"path"
"strings"
"syscall"
"github.com/toolkits/pkg/file"
"github.com/toolkits/pkg/logger"
"github.com/toolkits/pkg/runner"
"github.com/leezer3379/voice-sender/config"
"github.com/leezer3379/voice-sender/cron"
"github.com/leezer3379/voice-sender/redisc"
)
var (
vers *bool
help *bool
conf *string
test *string
)
func init() {
vers = flag.Bool("v", false, "display the version.")
help = flag.Bool("h", false, "print this help.")
conf = flag.String("f", "", "specify configuration file.")
test = flag.String("t", "", "test configuration.")
flag.Parse()
if *vers {
fmt.Println("version:", config.Version)
os.Exit(0)
}
if *help {
flag.Usage()
os.Exit(0)
}
runner.Init()
fmt.Println("runner.cwd:", runner.Cwd)
fmt.Println("runner.hostname:", runner.Hostname)
}
func main() {
aconf()
pconf()
if *test != "" {
config.Test(strings.Split(*test, ","))
os.Exit(0)
}
config.InitLogger()
redisc.InitRedis()
go cron.SendVoice()
//ending()
startHttp()
}
func startHttp() {
http.HandleFunc("/voice", sendVoice) //设置访问的路由
err := http.ListenAndServe("127.0.0.1:2008", nil) //设置监听的端口
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func sendVoice(w http.ResponseWriter, r *http.Request) {
fmt.Println("method:", r.Method) //获取请求的方法
if r.Method == "GET" {
fmt.Println("OK")
} else {
s, _ := ioutil.ReadAll(r.Body) //把 body 内容读入字符串 s
fmt.Println("body: ", string(s))
var v3message dataobj.V3Message
err := json.Unmarshal(s, &v3message)
if err != nil {
logger.Errorf("unmarshal message failed, err: %v, redis reply: %v", err)
}
fmt.Println("Tos: ", v3message.Tos)
fmt.Println("Subject: ", v3message.Subject)
fmt.Println("Content: ", v3message.Content)
if count := len(v3message.Tos); count > 0 {
for _, mobile := range v3message.Tos {
go cron.V3SendVoice(mobile, v3message.Content)
}
}
}
}
func ending() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
select {
case <-c:
fmt.Printf("stop signal caught, stopping... pid=%d\n", os.Getpid())
}
logger.Close()
redisc.CloseRedis()
fmt.Println("sender stopped successfully")
}
// auto detect configuration file
func aconf() {
if *conf != "" && file.IsExist(*conf) {
return
}
*conf = path.Join(runner.Cwd, "etc", "voice-sender.local.yml")
if file.IsExist(*conf) {
return
}
*conf = path.Join(runner.Cwd, "etc", "voice-sender.yml")
if file.IsExist(*conf) {
return
}
fmt.Println("no configuration file for sender")
os.Exit(1)
}
// parse configuration file
func pconf() {
if err := config.ParseConfig(*conf); err != nil {
fmt.Println("cannot parse configuration file:", err)
os.Exit(1)
} else {
fmt.Println("parse configuration file:", *conf)
}
}