/
main.go
115 lines (94 loc) · 2.32 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
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"log"
"net/http"
"os"
"time"
)
// Binding from JSON
type SMS struct {
Mobile string `form:"mobile" json:"mobile" binding:"required"`
Message string `form:"message" json:"message" binding:"required"`
}
var (
modem *GSMModem
msgs chan SMS
bindaddress string
)
func init() {
// setup config
err := InitialiseConfig(cfgfile)
if err != nil {
fmt.Printf("error reading config: %s\r\n", err)
}
bindaddress = fmt.Sprintf("%s:%d", config.BindAddress, config.BindPort)
modem = NewModem(config.CommPort, config.Baud, "Modem")
msgs = make(chan SMS, config.BufferSize)
log.SetFlags(log.LstdFlags)
}
func main() {
err := modem.Connect()
if err != nil {
log.Printf("ConnectModem: error connecting to %s, %s\r\n", modem.DeviceId, err)
log.Printf("commport: %s\r\n", config.CommPort)
log.Printf("baud: %d\r\n", config.Baud)
os.Exit(1)
}
err = modem.InitialiseModem()
if err != nil {
log.Printf("InitModem: error initialising %s, %s\r\n", modem.DeviceId, err)
}
// Creates a router without any middleware by default
//gin.SetMode(gin.ReleaseMode)
r := gin.New()
// Global middlewares
r.Use(gin.Logger())
r.Use(gin.Recovery())
r.Use(SetJellyBeans())
api := r.Group("/api")
{
api.GET("/sms", index)
api.POST("/sms", sendSMS)
}
go worker()
// Listen and server on 0.0.0.0:8951
r.Run(bindaddress)
}
func index(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": 200, "message": "hello"})
}
func SetJellyBeans() gin.HandlerFunc {
// Do some initialization logic here
// Foo()
return func(c *gin.Context) {
c.Writer.Header().Set("X-Powered-By", "Black Jelly Beans")
c.Next()
}
}
func sendSMS(c *gin.Context) {
var json SMS
if c.BindJSON(&json) == nil {
if json.Mobile == "" || json.Message == "" {
c.JSON(http.StatusInternalServerError, gin.H{"status": 500, "message": "invalid message format"})
} else {
msgs <- json
c.JSON(http.StatusOK, gin.H{"status": 200, "message": "message received"})
}
}
}
func worker() {
for {
m := <-msgs
log.Printf("msg received: %s\r\n", m)
time.Sleep(time.Second)
resp, err := modem.SendSMS(m.Mobile, m.Message)
if err != nil {
log.Printf("msg error: %s\r\n", err)
log.Printf("msg modem response: %s\r\n", resp)
} else {
log.Printf("msg success: %s\r\n", m)
}
}
}