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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
package main
import (
"errors"
"html/template"
"io"
"log"
"math/rand"
"net/http"
"os"
"path/filepath"
"time"
"janouch.name/sklad/imgutil"
"janouch.name/sklad/label"
"janouch.name/sklad/ql"
)
var templates = map[string]*template.Template{}
func executeTemplate(name string, w io.Writer, data interface{}) {
if err := templates[name].Execute(w, data); err != nil {
panic(err)
}
}
func wrap(inner func(http.ResponseWriter, *http.Request)) func(
http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if r.Method == http.MethodGet {
w.Header().Set("Cache-Control", "no-store")
}
inner(w, r)
}
}
func handleLogin(w http.ResponseWriter, r *http.Request) {
redirect := r.FormValue("redirect")
if redirect == "" {
redirect = "/"
}
session := sessionGet(w, r)
if session.LoggedIn {
http.Redirect(w, r, redirect, http.StatusSeeOther)
return
}
params := struct {
IncorrectPassword bool
}{}
switch r.Method {
case http.MethodGet:
// We're just going to render the template.
case http.MethodPost:
if r.FormValue("password") == db.Password {
session.LoggedIn = true
http.Redirect(w, r, redirect, http.StatusSeeOther)
return
}
params.IncorrectPassword = true
default:
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
executeTemplate("login.tmpl", w, ¶ms)
}
func handleLogout(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
session := r.Context().Value(sessionContextKey{}).(*Session)
session.LoggedIn = false
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func handleContainer(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
// TODO
}
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
allSeries := map[string]string{}
for _, s := range indexSeries {
allSeries[s.Prefix] = s.Description
}
var container *Container
children := []*Container{}
if id := ContainerId(r.FormValue("id")); id == "" {
children = indexChildren[""]
} else if c, ok := indexContainer[id]; ok {
children = c.Children()
container = c
}
params := struct {
Container *Container
Children []*Container
AllSeries map[string]string
}{
Container: container,
Children: children,
AllSeries: allSeries,
}
executeTemplate("container.tmpl", w, ¶ms)
}
func handleSeries(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
// TODO
}
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
allSeries := map[string]string{}
for _, s := range indexSeries {
allSeries[s.Prefix] = s.Description
}
prefix := r.FormValue("prefix")
description := ""
if prefix == "" {
} else if series, ok := indexSeries[prefix]; ok {
description = series.Description
}
params := struct {
Prefix string
Description string
AllSeries map[string]string
}{
Prefix: prefix,
Description: description,
AllSeries: allSeries,
}
executeTemplate("series.tmpl", w, ¶ms)
}
func handleSearch(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
query := r.FormValue("q")
params := struct {
Query string
Series []*Series
Containers []*Container
}{
Query: query,
Series: dbSearchSeries(query),
Containers: dbSearchContainers(query),
}
executeTemplate("search.tmpl", w, ¶ms)
}
func printLabel(id string) error {
printer, err := ql.Open()
if err != nil {
return err
}
if printer == nil {
return errors.New("no suitable printer found")
}
defer printer.Close()
/*
printer.StatusNotify = func(status *ql.Status) {
log.Printf("\x1b[1mreceived status\x1b[m\n%+v\n%s",
status[:], status)
}
*/
if err := printer.Initialize(); err != nil {
return err
}
if err := printer.UpdateStatus(); err != nil {
return err
}
mediaInfo := ql.GetMediaInfo(
printer.LastStatus.MediaWidthMM(),
printer.LastStatus.MediaLengthMM(),
)
if mediaInfo == nil {
return errors.New("unknown media")
}
return printer.Print(&imgutil.LeftRotate{Image: label.GenLabelForHeight(
labelFont, id, mediaInfo.PrintAreaPins, db.BDFScale)})
}
func handleLabel(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
params := struct {
Id string
UnknownId bool
Error error
}{
Id: r.FormValue("id"),
}
if c := indexContainer[ContainerId(params.Id)]; c == nil {
params.UnknownId = true
} else {
params.Error = printLabel(params.Id)
}
executeTemplate("label.tmpl", w, ¶ms)
}
func main() {
// Randomize the RNG for session string generation.
rand.Seed(time.Now().UnixNano())
if len(os.Args) != 3 {
log.Fatalf("Usage: %s ADDRESS DATABASE-FILE\n", os.Args[0])
}
var address string
address, dbPath = os.Args[1], os.Args[2]
// Load database.
if err := loadDatabase(); err != nil {
log.Fatalln(err)
}
// Load HTML templates from the current working directory.
m, err := filepath.Glob("*.tmpl")
if err != nil {
log.Fatalln(err)
}
for _, name := range m {
templates[name] = template.Must(template.ParseFiles("base.tmpl", name))
}
http.HandleFunc("/login", wrap(handleLogin))
http.HandleFunc("/logout", sessionWrap(wrap(handleLogout)))
http.HandleFunc("/", sessionWrap(wrap(handleContainer)))
http.HandleFunc("/series", sessionWrap(wrap(handleSeries)))
http.HandleFunc("/search", sessionWrap(wrap(handleSearch)))
http.HandleFunc("/label", sessionWrap(wrap(handleLabel)))
log.Fatalln(http.ListenAndServe(address, nil))
}
|