-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
106 lines (90 loc) · 2.29 KB
/
server.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
package main
import (
_ "embed"
"encoding/json"
"fmt"
"image"
"image/png"
"log"
"net/http"
"strconv"
"sync"
)
type Server struct {
Address string
mu sync.Mutex
}
func (s *Server) Print(img image.Image) error {
if !s.mu.TryLock() {
return fmt.Errorf("another printing is in progress; please try again later")
}
defer s.mu.Unlock()
t := Tape{Image: img}
return t.Print(s.Address)
}
func writeJSONMessage(w http.ResponseWriter, code int, msg string) {
body := struct {
Message string `json:"message"`
}{
Message: msg,
}
b, err := json.Marshal(body)
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Length", strconv.Itoa(len(b)))
w.WriteHeader(code)
w.Write(b)
}
func (s *Server) handleNotFound(w http.ResponseWriter, r *http.Request) {
writeJSONMessage(w, http.StatusNotFound, "not found")
}
//go:embed index.html
var indexHTML []byte
func (s *Server) handleIndex(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet && r.Method != http.MethodHead {
writeJSONMessage(w, http.StatusMethodNotAllowed, "method not allowed")
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Content-Length", strconv.Itoa(len(indexHTML)))
w.WriteHeader(http.StatusOK)
w.Write(indexHTML)
}
func (s *Server) handlePrint(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
writeJSONMessage(w, http.StatusMethodNotAllowed, "unexpected http method")
return
}
f, _, err := r.FormFile("tape")
if err != nil {
log.Printf("failed to parse request body: %+v", err)
writeJSONMessage(w, http.StatusBadRequest, "invalid request body")
return
}
defer f.Close()
img, err := png.Decode(f)
if err != nil {
log.Printf("failed to parse png: %+v", err)
writeJSONMessage(w, http.StatusBadRequest, "invalid image")
return
}
err = s.Print(img)
if err != nil {
log.Printf("failed to print: %+v", err)
writeJSONMessage(w, http.StatusInternalServerError, fmt.Sprintf("failed to print: %v", err))
return
}
writeJSONMessage(w, http.StatusOK, "printed successfully")
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/":
s.handleIndex(w, r)
case "/print":
s.handlePrint(w, r)
default:
s.handleNotFound(w, r)
}
}