forked from CloudInn/escpos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
52 lines (42 loc) · 959 Bytes
/
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
package main
import (
"flag"
"log"
"net/http"
"os"
"github.com/cloudinn/escpos"
)
var (
flagListen = flag.String("l", "127.0.22.8:80", "listen")
flagEndpoint = flag.String("endpoint", escpos.DefaultEndpoint, "endpoint")
flagPrinter = flag.String("p", "", "path to printer")
)
func main() {
flag.Parse()
if *flagPrinter == "" {
log.Fatal("must specify path to printer via -p")
}
// open printer
f, err := os.Create(*flagPrinter)
if err != nil {
panic(err)
}
defer f.Close()
// create printer
ep, err := escpos.NewPrinter(f)
if err != nil {
log.Fatal(err)
}
// create server
s, err := escpos.NewServer(ep)
if err != nil {
log.Fatal(err)
}
// set up mux
mux := http.NewServeMux()
mux.Handle(*flagEndpoint, s)
mux.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
http.Error(res, http.StatusText(http.StatusNotFound), http.StatusNotFound)
})
log.Fatal(http.ListenAndServe(*flagListen, mux))
}