-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (110 loc) · 2.86 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
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"net/http"
)
var (
action string
port int
outFile string
file string
)
var printerName = "Receipt Printer"
const docName = "DK & NCK Receipt"
func init() {
flag.StringVar(&action, "action", "listen", "listen, print, or generate?")
flag.IntVar(&port, "port", 35625, "Port to use when serving")
flag.StringVar(&outFile, "outfile", "output.bin", "Output filename for the generated escpos code")
}
type InvoiceItem struct {
Product_id string
Name string
Unit_price string
Quantity string
Total_price string
}
type Invoice struct {
ID string
Date string
Items []InvoiceItem
Subtotal string
}
type PrintRequest struct {
Secret_key string
Invoice Invoice
}
func listen() {
http.HandleFunc("/print-receipt", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "OPTIONS" {
fmt.Println("OPTIONS request received")
w.Header().Add("Connection", "keep-alive")
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Allow-Methods", "POST")
w.Header().Add("Access-Control-Allow-Headers", "content-type")
w.Header().Add("Access-Control-Max-Age", "86400")
fmt.Println("handled preflight")
return
}
w.Header().Set("Access-Control-Allow-Origin", "*")
if r.Method != "POST" {
fmt.Println("not POST or OPTIONS method, abort")
http.Error(w, "method not supported", http.StatusBadRequest)
return
}
var printReq PrintRequest
err := json.NewDecoder(r.Body).Decode(&printReq)
if err != nil {
fmt.Println(err.Error())
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
fmt.Println("request:", printReq)
if printReq.Secret_key != "supersecret" {
fmt.Println("not authorized")
http.Error(w, "not authorized", http.StatusBadRequest)
return
}
fmt.Println("secret key matches")
bytes := GetInvoiceBytes(printReq)
fmt.Printf("sending receipt to printer %s\n", printerName)
PrintBytes(printerName, bytes, docName)
fmt.Println("receipt sent to printer")
fmt.Fprintf(w, "printing receipt")
})
fmt.Printf("Starting server on port %d\n", port)
err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
fmt.Printf("listening for print requests on http://localhost:%d\n/35625", port)
if errors.Is(err, http.ErrServerClosed) {
fmt.Printf("Server closed\n")
} else if err != nil {
fmt.Printf("[Error] Could not start server: %s\n", err)
}
}
func main() {
flag.Parse()
args := flag.Args()
if len(args) == 1 {
printerName = args[0]
}
if len(args) == 2 {
file = args[1]
}
switch action {
case "print":
fmt.Printf(
"Attempting to send file [%s] to printer [%s].\n",
file,
printerName,
)
err := PrintFile(printerName, file, "DKNCK Receipt")
if err != nil {
fmt.Println("[Error] Could not print file", err)
return
}
case "listen":
listen()
}
}