-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
196 lines (171 loc) · 3.66 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/kardianos/service"
"github.com/urfave/cli"
"BRC-ED40/scanner"
"log"
"net/http"
"os"
"strings"
)
const (
appname = "BRC"
appver = "0.0.1"
)
var (
host string
port string
svcOp string
certFile string
keyFile string
noTLS bool
hub *Hub
logger service.Logger
)
type BarCode struct {
Code string
}
type program struct{}
func (p *program) Start(s service.Service) error {
// Start should not block. Do the actual work async.
hub = newHub()
go hub.run()
go scanner.ScanForever(processScanFn)
go p.run()
return nil
}
func (p *program) run() {
// Do work here
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
serveWs(hub, w, r)
})
var addr = flag.String("addr", host+":"+port, "http service address")
var err error
if noTLS {
err = http.ListenAndServe(*addr, nil)
} else {
err = http.ListenAndServeTLS(*addr, certFile, keyFile, nil)
}
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func (p *program) Stop(s service.Service) error {
// Stop should not block. Return with a few seconds.
return nil
}
func processScanFn(scanData string) {
if scanData[0:2] == "-j" {
scanData = scanData[2:]
}
fmt.Println("Scanned Data: %s", scanData)
data := BarCode{
Code: scanData,
}
obj, err := json.Marshal(data)
hub.broadcast <- obj
if err != nil {
fmt.Println(err)
}
}
func main() {
app := cli.NewApp()
app.Name = appname
app.Usage = "BarCode Reader with Websocket event support"
app.Version = appver
app.Authors = []cli.Author{
{
Name: "Vishal Kumar Singh",
Email: "vishalkumarsingh1707@gmail.com",
},
}
app.Copyright = "(c) 2017 Vishal Kumar Singh"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "host",
Value: "localhost",
Usage: "Host for websocket",
EnvVar: "BRC_HOST",
Destination: &host,
},
cli.StringFlag{
Name: "port,p",
Value: "8080",
Usage: "the websocket port",
EnvVar: "BRC_PORT",
Destination: &port,
},
cli.StringFlag{
Name: "cert",
Value: "cert.pem",
Usage: "The certificate file to use for TLS",
EnvVar: "BRC_CERT",
Destination: &certFile,
},
cli.StringFlag{
Name: "key",
Value: "key.pem",
Usage: "The key file to use for TLS",
EnvVar: "BRC_KEY",
Destination: &keyFile,
},
cli.BoolFlag{
Name: "notls",
Usage: "Disable TLS for the websoclket server",
EnvVar: "BRC_NOTLS",
Destination: &noTLS,
},
cli.StringFlag{
Name: "service",
Usage: "Service operation",
EnvVar: "BRC_OP",
Destination: &svcOp,
},
}
app.Action = func(c *cli.Context) error {
argHost := fmt.Sprintf("--host=%s", host)
argPort := fmt.Sprintf("--port=%s", port)
argCert := fmt.Sprintf("--cert=%s", certFile)
argKey := fmt.Sprintf("--key=%s", keyFile)
args := []string{argHost, argPort}
if noTLS {
args = append(args, "--notls")
} else {
args = append(args, argCert)
args = append(args, argKey)
}
svcConfig := &service.Config{
Name: "BRC-ED40",
DisplayName: "BRCService",
Description: "Websocket for barcode reader service",
Arguments: args,
}
prg := &program{}
svc, err := service.New(prg, svcConfig)
if err != nil {
log.Fatal(err)
}
if svcOp != "" {
svcOp = strings.ToLower(svcOp)
err := service.Control(svc, svcOp)
if err != nil {
log.Fatal(err)
return err
}
return nil
}
logger, err = svc.Logger(nil)
if err != nil {
log.Fatal(err)
}
err = svc.Run()
if err != nil {
logger.Error(err)
}
return err
}
app.Run(os.Args)
}