/
BarcodeQueryTool.go
93 lines (79 loc) · 2.04 KB
/
BarcodeQueryTool.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
package main
import (
"flag"
"github.com/RoanBrand/BarcodeQueryTool/api"
"github.com/kardianos/osext"
"github.com/kardianos/service"
"log"
"net/http"
)
type app struct {
conf *api.AppConfig
execPath string
}
func (p *app) Start(s service.Service) error {
go p.run()
return nil
}
func (p *app) run() {
// Static files for frontend
fs := http.FileServer(http.Dir(p.execPath + "\\static"))
http.Handle("/", fs)
// Connect to DB
dbConn := api.New(p.conf)
defer dbConn.EndGracefully()
// Api calls
http.HandleFunc("/retrieve", dbConn.DashFunc)
http.HandleFunc("/query", dbConn.QueryFunc)
http.HandleFunc("/errorcount", dbConn.ErrorCount)
http.HandleFunc("/getlocaldrivelist", api.GetLocalDrivelist)
// Launch web server
log.Println("Starting HTTP Server")
log.Fatal(http.ListenAndServe(":80", nil))
}
func (p *app) Stop(s service.Service) error {
return nil
}
func main() {
usageHelpMessage := `You must set the config, e.g. BarcodeQueryTool.exe -config="config.json"`
// Get flags
svcFlag := flag.String("service", "", "Control the system service.")
mainconfig := flag.String("config", "", usageHelpMessage)
flag.Parse()
svcConfig := &service.Config{
Name: "Barcode Query Tool",
DisplayName: "BarcodeQueryTool",
Description: "Back-end for BarcodeQueryTool in Siemens WinCC",
Arguments: []string{"-config=" + *mainconfig},
}
newApp := &app{}
s, err := service.New(newApp, svcConfig)
if err != nil {
log.Fatal(err)
}
if *svcFlag != "" {
err = service.Control(s, *svcFlag)
if err != nil {
log.Printf("Valid actions: %q\n", service.ControlAction)
log.Fatal(err)
}
return
}
newApp.execPath, err = osext.ExecutableFolder()
if err != nil {
log.Fatalf("Error finding program executable path: %v", err)
}
// Load config
if *mainconfig == "" {
log.Fatal(usageHelpMessage)
}
newApp.conf = &api.AppConfig{}
err = newApp.conf.LoadFromFile(newApp.execPath + "\\" + *mainconfig)
if err != nil {
log.Fatalf("Error loading %v: %v", *mainconfig, err)
}
err = s.Run()
if err != nil {
log.Fatal(err)
}
}