From 1331f3b5642f521236fcb1ec21ee43d5b76c0b91 Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Sun, 14 Apr 2019 22:30:40 +0200 Subject: Move commands under cmd/ --- bdf-preview/main.go | 93 ---------------- bdf-sample/main.go | 42 -------- cmd/bdf-preview/main.go | 93 ++++++++++++++++ cmd/bdf-sample/main.go | 42 ++++++++ cmd/label-tool/main.go | 199 ++++++++++++++++++++++++++++++++++ cmd/ql-info/main.go | 41 +++++++ cmd/ql-print/main.go | 88 +++++++++++++++ cmd/sklad/base.tmpl | 68 ++++++++++++ cmd/sklad/container.tmpl | 99 +++++++++++++++++ cmd/sklad/db.go | 225 +++++++++++++++++++++++++++++++++++++++ cmd/sklad/label.tmpl | 13 +++ cmd/sklad/login.tmpl | 17 +++ cmd/sklad/main.go | 271 +++++++++++++++++++++++++++++++++++++++++++++++ cmd/sklad/search.tmpl | 38 +++++++ cmd/sklad/series.tmpl | 43 ++++++++ cmd/sklad/session.go | 66 ++++++++++++ label-tool/main.go | 199 ---------------------------------- ql-info/main.go | 41 ------- ql-print/main.go | 88 --------------- sklad/base.tmpl | 68 ------------ sklad/container.tmpl | 99 ----------------- sklad/db.go | 225 --------------------------------------- sklad/label.tmpl | 13 --- sklad/login.tmpl | 17 --- sklad/main.go | 271 ----------------------------------------------- sklad/search.tmpl | 38 ------- sklad/series.tmpl | 43 -------- sklad/session.go | 66 ------------ 28 files changed, 1303 insertions(+), 1303 deletions(-) delete mode 100644 bdf-preview/main.go delete mode 100644 bdf-sample/main.go create mode 100644 cmd/bdf-preview/main.go create mode 100644 cmd/bdf-sample/main.go create mode 100644 cmd/label-tool/main.go create mode 100644 cmd/ql-info/main.go create mode 100644 cmd/ql-print/main.go create mode 100644 cmd/sklad/base.tmpl create mode 100644 cmd/sklad/container.tmpl create mode 100644 cmd/sklad/db.go create mode 100644 cmd/sklad/label.tmpl create mode 100644 cmd/sklad/login.tmpl create mode 100644 cmd/sklad/main.go create mode 100644 cmd/sklad/search.tmpl create mode 100644 cmd/sklad/series.tmpl create mode 100644 cmd/sklad/session.go delete mode 100644 label-tool/main.go delete mode 100644 ql-info/main.go delete mode 100644 ql-print/main.go delete mode 100644 sklad/base.tmpl delete mode 100644 sklad/container.tmpl delete mode 100644 sklad/db.go delete mode 100644 sklad/label.tmpl delete mode 100644 sklad/login.tmpl delete mode 100644 sklad/main.go delete mode 100644 sklad/search.tmpl delete mode 100644 sklad/series.tmpl delete mode 100644 sklad/session.go diff --git a/bdf-preview/main.go b/bdf-preview/main.go deleted file mode 100644 index b1f2045..0000000 --- a/bdf-preview/main.go +++ /dev/null @@ -1,93 +0,0 @@ -package main - -import ( - "html/template" - "image" - "image/draw" - "image/png" - "log" - "net/http" - "os" - - "janouch.name/sklad/bdf" -) - -type fontItem struct { - Font *bdf.Font - Preview image.Image -} - -var fonts = map[string]fontItem{} - -var tmpl = template.Must(template.New("list").Parse(` - - - - - - - -{{range $k, $v := . }} - - - - -{{end}} -
NamePreview
{{$k}}
- -`)) - -func handle(w http.ResponseWriter, r *http.Request) { - if err := r.ParseForm(); err != nil { - http.Error(w, err.Error(), 500) - return - } - - name := r.FormValue("name") - if name == "" { - w.Header().Set("Content-Type", "text/html") - tmpl.Execute(w, fonts) - return - } - - item, ok := fonts[name] - if !ok { - http.Error(w, "No such font.", 400) - return - } - - w.Header().Set("Content-Type", "image/png") - if err := png.Encode(w, item.Preview); err != nil { - http.Error(w, err.Error(), 500) - return - } -} - -func main() { - for _, filename := range os.Args[1:] { - fi, err := os.Open(filename) - if err != nil { - log.Fatalln(err) - } - font, err := bdf.NewFromBDF(fi) - if err != nil { - log.Fatalf("%s: %s\n", filename, err) - } - if err := fi.Close(); err != nil { - log.Fatalln(err) - } - - r, _ := font.BoundString(font.Name) - super := r.Inset(-3) - - img := image.NewRGBA(super) - draw.Draw(img, super, image.White, image.ZP, draw.Src) - font.DrawString(img, image.ZP, font.Name) - - fonts[filename] = fontItem{Font: font, Preview: img} - } - - log.Println("Starting server") - http.HandleFunc("/", handle) - log.Fatal(http.ListenAndServe(":8080", nil)) -} diff --git a/bdf-sample/main.go b/bdf-sample/main.go deleted file mode 100644 index 1850118..0000000 --- a/bdf-sample/main.go +++ /dev/null @@ -1,42 +0,0 @@ -package main - -import ( - "image" - "image/draw" - "image/png" - "log" - "os" - - "janouch.name/sklad/bdf" -) - -func main() { - fi, err := os.Open(os.Args[1]) - if err != nil { - log.Fatalln(err) - } - defer fi.Close() - font, err := bdf.NewFromBDF(fi) - if err != nil { - log.Fatalln(err) - } - - r, _ := font.BoundString(font.Name) - super := r.Inset(-20) - - img := image.NewRGBA(super) - draw.Draw(img, super, image.White, image.ZP, draw.Src) - font.DrawString(img, image.ZP, font.Name) - - fo, err := os.Create("out.png") - if err != nil { - log.Fatalln(err) - } - if err := png.Encode(fo, img); err != nil { - fo.Close() - log.Fatal(err) - } - if err := fo.Close(); err != nil { - log.Fatal(err) - } -} diff --git a/cmd/bdf-preview/main.go b/cmd/bdf-preview/main.go new file mode 100644 index 0000000..b1f2045 --- /dev/null +++ b/cmd/bdf-preview/main.go @@ -0,0 +1,93 @@ +package main + +import ( + "html/template" + "image" + "image/draw" + "image/png" + "log" + "net/http" + "os" + + "janouch.name/sklad/bdf" +) + +type fontItem struct { + Font *bdf.Font + Preview image.Image +} + +var fonts = map[string]fontItem{} + +var tmpl = template.Must(template.New("list").Parse(` + + + + + + + +{{range $k, $v := . }} + + + + +{{end}} +
NamePreview
{{$k}}
+ +`)) + +func handle(w http.ResponseWriter, r *http.Request) { + if err := r.ParseForm(); err != nil { + http.Error(w, err.Error(), 500) + return + } + + name := r.FormValue("name") + if name == "" { + w.Header().Set("Content-Type", "text/html") + tmpl.Execute(w, fonts) + return + } + + item, ok := fonts[name] + if !ok { + http.Error(w, "No such font.", 400) + return + } + + w.Header().Set("Content-Type", "image/png") + if err := png.Encode(w, item.Preview); err != nil { + http.Error(w, err.Error(), 500) + return + } +} + +func main() { + for _, filename := range os.Args[1:] { + fi, err := os.Open(filename) + if err != nil { + log.Fatalln(err) + } + font, err := bdf.NewFromBDF(fi) + if err != nil { + log.Fatalf("%s: %s\n", filename, err) + } + if err := fi.Close(); err != nil { + log.Fatalln(err) + } + + r, _ := font.BoundString(font.Name) + super := r.Inset(-3) + + img := image.NewRGBA(super) + draw.Draw(img, super, image.White, image.ZP, draw.Src) + font.DrawString(img, image.ZP, font.Name) + + fonts[filename] = fontItem{Font: font, Preview: img} + } + + log.Println("Starting server") + http.HandleFunc("/", handle) + log.Fatal(http.ListenAndServe(":8080", nil)) +} diff --git a/cmd/bdf-sample/main.go b/cmd/bdf-sample/main.go new file mode 100644 index 0000000..1850118 --- /dev/null +++ b/cmd/bdf-sample/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "image" + "image/draw" + "image/png" + "log" + "os" + + "janouch.name/sklad/bdf" +) + +func main() { + fi, err := os.Open(os.Args[1]) + if err != nil { + log.Fatalln(err) + } + defer fi.Close() + font, err := bdf.NewFromBDF(fi) + if err != nil { + log.Fatalln(err) + } + + r, _ := font.BoundString(font.Name) + super := r.Inset(-20) + + img := image.NewRGBA(super) + draw.Draw(img, super, image.White, image.ZP, draw.Src) + font.DrawString(img, image.ZP, font.Name) + + fo, err := os.Create("out.png") + if err != nil { + log.Fatalln(err) + } + if err := png.Encode(fo, img); err != nil { + fo.Close() + log.Fatal(err) + } + if err := fo.Close(); err != nil { + log.Fatal(err) + } +} diff --git a/cmd/label-tool/main.go b/cmd/label-tool/main.go new file mode 100644 index 0000000..39a840c --- /dev/null +++ b/cmd/label-tool/main.go @@ -0,0 +1,199 @@ +package main + +import ( + "errors" + "html/template" + "image" + "image/png" + "log" + "net/http" + "os" + "strconv" + + "janouch.name/sklad/bdf" + "janouch.name/sklad/imgutil" + "janouch.name/sklad/label" + "janouch.name/sklad/ql" +) + +var font *bdf.Font + +var tmpl = template.Must(template.New("form").Parse(` + + +

PT-CBP label printing tool

+ + + +
+ + +
+ {{ if .Printer }} + +

Printer: {{ .Printer.Manufacturer }} {{ .Printer.Model }} +

Tape: + {{ if .Printer.LastStatus }} + {{ .Printer.LastStatus.MediaWidthMM }} mm × + {{ .Printer.LastStatus.MediaLengthMM }} mm + + {{ if .MediaInfo }} + (offset: {{ .MediaInfo.SideMarginPins }} pt, + print area: {{ .MediaInfo.PrintAreaPins }} pt) + {{ else }} + (unknown media) + {{ end }} + + {{ if .Printer.LastStatus.Errors }} + {{ range .Printer.LastStatus.Errors }} +

Error: {{ . }} + {{ end }} + {{ end }} + + {{ end }} + {{ if .InitErr }} + {{ .InitErr }} + {{ end }} + + {{ else }} +

Error: {{ .PrinterErr }} + {{ end }} +

+
+

Font: {{ .Font.Name }} +

+
+

+ + + +

+ +

+
+ +`)) + +func getPrinter() (*ql.Printer, error) { + printer, err := ql.Open() + if err != nil { + return nil, err + } + if printer == nil { + return nil, errors.New("no suitable printer found") + } + return printer, nil +} + +func getStatus(printer *ql.Printer) error { + if err := printer.Initialize(); err != nil { + return err + } + if err := printer.UpdateStatus(); err != nil { + return err + } + return nil +} + +func handle(w http.ResponseWriter, r *http.Request) { + if err := r.ParseForm(); err != nil { + http.Error(w, err.Error(), 500) + return + } + + var ( + initErr error + mediaInfo *ql.MediaInfo + ) + printer, printerErr := getPrinter() + if printerErr == nil { + defer printer.Close() + printer.StatusNotify = func(status *ql.Status) { + log.Printf("\x1b[1mreceived status\x1b[m\n%s", status) + } + + if initErr = getStatus(printer); initErr == nil { + mediaInfo = ql.GetMediaInfo( + printer.LastStatus.MediaWidthMM(), + printer.LastStatus.MediaLengthMM(), + ) + } + } + + var params = struct { + Printer *ql.Printer + PrinterErr error + InitErr error + MediaInfo *ql.MediaInfo + Font *bdf.Font + Text string + Scale int + }{ + Printer: printer, + PrinterErr: printerErr, + InitErr: initErr, + MediaInfo: mediaInfo, + Font: font, + Text: r.FormValue("text"), + } + + var err error + params.Scale, err = strconv.Atoi(r.FormValue("scale")) + if err != nil { + params.Scale = 3 + } + + var img image.Image + if mediaInfo != nil { + img = &imgutil.LeftRotate{Image: label.GenLabelForHeight( + font, params.Text, mediaInfo.PrintAreaPins, params.Scale)} + if r.FormValue("print") != "" { + if err := printer.Print(img); err != nil { + log.Println("print error:", err) + } + } + } + + if _, ok := r.Form["img"]; !ok { + w.Header().Set("Content-Type", "text/html") + tmpl.Execute(w, ¶ms) + return + } + + if mediaInfo == nil { + http.Error(w, "unknown media", 500) + return + } + + w.Header().Set("Content-Type", "image/png") + if err := png.Encode(w, img); err != nil { + http.Error(w, err.Error(), 500) + return + } +} + +func main() { + if len(os.Args) != 3 { + log.Fatalf("usage: %s ADDRESS BDF-FILE\n", os.Args[0]) + } + + address, bdfPath := os.Args[1], os.Args[2] + + var err error + fi, err := os.Open(bdfPath) + if err != nil { + log.Fatalln(err) + } + + font, err = bdf.NewFromBDF(fi) + if err := fi.Close(); err != nil { + log.Fatalln(err) + } + if err != nil { + log.Fatalln(err) + } + + log.Println("starting server") + http.HandleFunc("/", handle) + log.Fatalln(http.ListenAndServe(address, nil)) +} diff --git a/cmd/ql-info/main.go b/cmd/ql-info/main.go new file mode 100644 index 0000000..8d53d63 --- /dev/null +++ b/cmd/ql-info/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + "log" + + "janouch.name/sklad/ql" +) + +func main() { + printer, err := ql.Open() + if err != nil { + log.Fatalln(err) + } + if printer == nil { + log.Fatalln("no suitable printer found") + } + + defer printer.Close() + + fmt.Printf("\x1b[1m%s %s\x1b[m\n", printer.Manufacturer, printer.Model) + if err := printer.Initialize(); err != nil { + log.Fatalln(err) + } + if err := printer.UpdateStatus(); err != nil { + log.Fatalln(err) + } + + status := printer.LastStatus + fmt.Print(status) + + fmt.Println("\x1b[1mMedia information\x1b[m") + if mi := ql.GetMediaInfo( + status.MediaWidthMM(), status.MediaLengthMM()); mi != nil { + fmt.Println("side margin pins:", mi.SideMarginPins) + fmt.Println("print area pins:", mi.PrintAreaPins) + fmt.Println("print area length:", mi.PrintAreaLength) + } else { + fmt.Println("unknown media") + } +} diff --git a/cmd/ql-print/main.go b/cmd/ql-print/main.go new file mode 100644 index 0000000..d0c986d --- /dev/null +++ b/cmd/ql-print/main.go @@ -0,0 +1,88 @@ +package main + +import ( + "image" + _ "image/gif" + _ "image/jpeg" + _ "image/png" + + "flag" + "fmt" + "log" + "os" + + "janouch.name/sklad/imgutil" + "janouch.name/sklad/ql" +) + +var scale = flag.Int("scale", 1, "integer upscaling") +var rotate = flag.Bool("rotate", false, "print sideways") + +func main() { + flag.Usage = func() { + fmt.Fprintf(os.Stderr, "Usage: %s IMAGE\n", os.Args[0]) + flag.PrintDefaults() + } + + flag.Parse() + if flag.NArg() != 1 { + flag.Usage() + os.Exit(1) + } + + // Open the picture. + f, err := os.Open(flag.Arg(0)) + if err != nil { + log.Fatalln(err) + } + defer f.Close() + + // Load and eventually transform the picture. + img, _, err := image.Decode(f) + if err != nil { + log.Fatalln(err) + } + if *scale > 1 { + img = &imgutil.Scale{Image: img, Scale: *scale} + } + if *rotate { + img = &imgutil.LeftRotate{Image: img} + } + + // Open and initialize the printer. + p, err := ql.Open() + if err != nil { + log.Fatalln(err) + } + if p == nil { + log.Fatalln("no suitable printer found") + } + if err := p.Initialize(); err != nil { + log.Fatalln(err) + } + if err := p.UpdateStatus(); err != nil { + log.Fatalln(err) + } + + // Check the picture against the media in the printer. + mi := ql.GetMediaInfo( + p.LastStatus.MediaWidthMM(), + p.LastStatus.MediaLengthMM(), + ) + if mi == nil { + log.Fatalln("unknown media") + } + + bounds := img.Bounds() + dx, dy := bounds.Dx(), bounds.Dy() + if dx > mi.PrintAreaPins { + log.Fatalln("the image is too wide,", dx, ">", mi.PrintAreaPins, "pt") + } + if dy > mi.PrintAreaLength && mi.PrintAreaLength != 0 { + log.Fatalln("the image is too high,", dy, ">", mi.PrintAreaLength, "pt") + } + + if err := p.Print(img); err != nil { + log.Fatalln(err) + } +} diff --git a/cmd/sklad/base.tmpl b/cmd/sklad/base.tmpl new file mode 100644 index 0000000..d92a818 --- /dev/null +++ b/cmd/sklad/base.tmpl @@ -0,0 +1,68 @@ + + + + {{ template "Title" . }} - sklad + + + + + + +
+

sklad

+ +{{ block "HeaderControls" . }} + Obaly + Řady + +
+ +
+ +
+ +
+{{ end }} + +
+ +{{ template "Content" . }} + + diff --git a/cmd/sklad/container.tmpl b/cmd/sklad/container.tmpl new file mode 100644 index 0000000..4bacae8 --- /dev/null +++ b/cmd/sklad/container.tmpl @@ -0,0 +1,99 @@ +{{ define "Title" }}{{/* +*/}}{{ if .Container }}{{ .Container.Id }}{{ else }}Obaly{{ end }}{{ end }} +{{ define "Content" }} + +{{ if .Container }} + +
+
+

{{ .Container.Id }}

+
+ +
+
+ +
+
+ +
+ +
+
+ + +
+
+ + +
+ +
+
+
+ +

Podobaly

+ +{{ else }} +
+
+

Nový obal

+
+
+ +
+
+ + +
+
+ + +
+ +
+
+
+ +

Obaly nejvyšší úrovně

+{{ end }} + +{{ range .Children }} +
+
+

{{ .Id }}

+
+ +
+
+ +
+
+{{ if .Description }} +

{{ .Description }} +{{ end }} +{{ if .Children }} +

+{{ range .Children }} +{{ .Id }} +{{ end }} +{{ end }} +

+{{ else }} +

Obal je prázdný. +{{ end }} + +{{ end }} diff --git a/cmd/sklad/db.go b/cmd/sklad/db.go new file mode 100644 index 0000000..def18a5 --- /dev/null +++ b/cmd/sklad/db.go @@ -0,0 +1,225 @@ +package main + +import ( + "encoding/json" + "errors" + "fmt" + "os" + "strings" + "time" + + "janouch.name/sklad/bdf" +) + +type Series struct { + Prefix string // PK: prefix + Description string // what kind of containers this is for +} + +type ContainerId string + +type Container struct { + Series string // PK: what series does this belong to + Number uint // PK: order within the series + Parent ContainerId // the container we're in, if any, otherwise "" + Description string // description and/or contents of this container +} + +func (c *Container) Id() ContainerId { + return ContainerId(fmt.Sprintf("%s%s%d", db.Prefix, c.Series, c.Number)) +} + +func (c *Container) Children() []*Container { + // TODO: Sort this by Id, or maybe even return a map[string]*Container, + // text/template would sort that automatically. + return indexChildren[c.Id()] +} + +func (c *Container) Path() (result []ContainerId) { + for c != nil && c.Parent != "" { + c = indexContainer[c.Parent] + result = append(result, c.Id()) + } + return +} + +type Database struct { + Password string // password for web users + Prefix string // prefix for all container IDs + Series []*Series // all known series + Containers []*Container // all known containers + + BDFPath string // path to bitmap font file + BDFScale int // integer scaling for the bitmap font +} + +var ( + dbPath string + db Database + dbLast Database + dbLog *os.File + + indexSeries = map[string]*Series{} + indexContainer = map[ContainerId]*Container{} + indexChildren = map[ContainerId][]*Container{} + + labelFont *bdf.Font +) + +// TODO: Some functions to add, remove and change things in the database. +// Indexes must be kept valid, just like any invariants. + +func dbSearchSeries(query string) (result []*Series) { + query = strings.ToLower(query) + added := map[string]bool{} + for _, s := range db.Series { + if query == strings.ToLower(s.Prefix) { + result = append(result, s) + added[s.Prefix] = true + } + } + for _, s := range db.Series { + if strings.Contains( + strings.ToLower(s.Description), query) && !added[s.Prefix] { + result = append(result, s) + } + } + return +} + +func dbSearchContainers(query string) (result []*Container) { + query = strings.ToLower(query) + added := map[ContainerId]bool{} + for id, c := range indexContainer { + if query == strings.ToLower(string(id)) { + result = append(result, c) + added[id] = true + } + } + for id, c := range indexContainer { + if strings.Contains( + strings.ToLower(c.Description), query) && !added[id] { + result = append(result, c) + } + } + return +} + +func dbCommit() error { + // Write a timestamp. + e := json.NewEncoder(dbLog) + e.SetIndent("", " ") + if err := e.Encode(time.Now().Format(time.RFC3339)); err != nil { + return err + } + + // Back up the current database contents. + if err := e.Encode(&dbLast); err != nil { + return err + } + if err := dbLog.Sync(); err != nil { + return err + } + + // Atomically replace the current database file. + tempPath := dbPath + ".new" + temp, err := os.OpenFile(tempPath, os.O_WRONLY|os.O_CREATE, 0644) + if err != nil { + return err + } + defer temp.Close() + + e = json.NewEncoder(temp) + e.SetIndent("", " ") + if err := e.Encode(&db); err != nil { + return err + } + + if err := os.Rename(tempPath, dbPath); err != nil { + return err + } + + dbLast = db + return nil +} + +// loadDatabase loads the database from a simple JSON file. We do not use +// any SQL stuff or even external KV storage because there is no real need +// for our trivial use case, with our general amount of data. +func loadDatabase() error { + dbFile, err := os.Open(dbPath) + if err != nil { + return err + } + if err := json.NewDecoder(dbFile).Decode(&db); err != nil { + return err + } + + // Further validate the database. + if db.Prefix == "" { + return errors.New("misconfigured prefix") + } + + // Construct indexes for primary keys, validate against duplicates. + for _, pv := range db.Series { + if _, ok := indexSeries[pv.Prefix]; ok { + return fmt.Errorf("duplicate series: %s", pv.Prefix) + } + indexSeries[pv.Prefix] = pv + } + for _, pv := range db.Containers { + id := pv.Id() + if _, ok := indexContainer[id]; ok { + return fmt.Errorf("duplicate container: %s", id) + } + indexContainer[id] = pv + } + + // Construct an index that goes from parent containers to their children. + for _, pv := range db.Containers { + if pv.Parent != "" { + if _, ok := indexContainer[pv.Parent]; !ok { + return fmt.Errorf("container %s has a nonexistent parent %s", + pv.Id(), pv.Parent) + } + } + indexChildren[pv.Parent] = append(indexChildren[pv.Parent], pv) + } + + // Validate that no container is a parent of itself on any level. + // This could probably be optimized but it would stop being obvious. + for _, pv := range db.Containers { + parents := map[ContainerId]bool{pv.Id(): true} + for pv.Parent != "" { + if parents[pv.Parent] { + return fmt.Errorf("%s contains itself", pv.Parent) + } + parents[pv.Parent] = true + pv = indexContainer[pv.Parent] + } + } + + // Prepare label printing. + if db.BDFScale <= 0 { + db.BDFScale = 1 + } + + if f, err := os.Open(db.BDFPath); err != nil { + return fmt.Errorf("cannot load label font: %s", err) + } else { + defer f.Close() + if labelFont, err = bdf.NewFromBDF(f); err != nil { + return fmt.Errorf("cannot load label font: %s", err) + } + } + + // Open database log file for appending. + if dbLog, err = os.OpenFile(dbPath+".log", + os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err != nil { + return err + } + + // Remember the current state of the database. + dbLast = db + return nil +} diff --git a/cmd/sklad/label.tmpl b/cmd/sklad/label.tmpl new file mode 100644 index 0000000..da17c58 --- /dev/null +++ b/cmd/sklad/label.tmpl @@ -0,0 +1,13 @@ +{{ define "Title" }}Tisk štítku{{ end }} +{{ define "Content" }} +

Tisk štítku pro {{ .Id }}

+ +{{ if .UnknownId }} +

Neznámý obal. +{{ else if .Error }} +

Tisk selhal: {{ .Error }} +{{ else }} +

Tisk proběhl úspěšně. +{{ end }} + +{{ end }} diff --git a/cmd/sklad/login.tmpl b/cmd/sklad/login.tmpl new file mode 100644 index 0000000..c34ab53 --- /dev/null +++ b/cmd/sklad/login.tmpl @@ -0,0 +1,17 @@ +{{ define "Title" }}Přihlášení{{ end }} +{{ define "HeaderControls" }}{{ end }} +{{ define "Content" }} + +

Přihlášení

+ +
+ + +
+ +{{ if .IncorrectPassword }} +

Bylo zadáno nesprávné heslo. +{{ end }} + +{{ end }} diff --git a/cmd/sklad/main.go b/cmd/sklad/main.go new file mode 100644 index 0000000..32dd68b --- /dev/null +++ b/cmd/sklad/main.go @@ -0,0 +1,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)) +} diff --git a/cmd/sklad/search.tmpl b/cmd/sklad/search.tmpl new file mode 100644 index 0000000..cf704cf --- /dev/null +++ b/cmd/sklad/search.tmpl @@ -0,0 +1,38 @@ +{{ define "Title" }}„{{ .Query }}“ — Vyhledávání{{ end }} +{{ define "Content" }} + +

Vyhledávání: „{{ .Query }}“

+ +

Řady

+ +{{ range .Series }} +
+
+

{{ .Prefix }}

+

{{ .Description }} +

+
+{{ else }} +

Neodpovídají žádné řady. +{{ end }} + +

Obaly

+ +{{ range .Containers }} +
+
+

{{ .Id }} +{{ range .Path }} + « {{ . }} +{{ end }} +

+
+{{ if .Description }} +

{{ .Description }} +{{ end }} +

+{{ else }} +

Neodpovídají žádné obaly. +{{ end }} + +{{ end }} diff --git a/cmd/sklad/series.tmpl b/cmd/sklad/series.tmpl new file mode 100644 index 0000000..4956e3a --- /dev/null +++ b/cmd/sklad/series.tmpl @@ -0,0 +1,43 @@ +{{ define "Title" }}{{ or .Prefix "Řady" }}{{ end }} +{{ define "Content" }} + +{{ if .Prefix }} +

{{ .Prefix }}

+ +{{ if .Description }} +

{{ .Description }} +{{ end }} +{{ else }} + +

+
+
+

Nová řada

+ + + +
+ +
+ +{{ range $prefix, $desc := .AllSeries }} +
+
+

{{ $prefix }}

+
+ +
+
+ +
+
+
+{{ else }} +

Nejsou žádné řady. +{{ end }} + +{{ end }} + +{{ end }} diff --git a/cmd/sklad/session.go b/cmd/sklad/session.go new file mode 100644 index 0000000..02fe0b0 --- /dev/null +++ b/cmd/sklad/session.go @@ -0,0 +1,66 @@ +package main + +import ( + "context" + "encoding/hex" + "math/rand" + "net/http" + "net/url" +) + +// session storage indexed by a random UUID +var sessions = map[string]*Session{} + +type Session struct { + LoggedIn bool // may access the DB +} + +type sessionContextKey struct{} + +func sessionGenId() string { + u := make([]byte, 16) + if _, err := rand.Read(u); err != nil { + panic("cannot generate random bytes") + } + return hex.EncodeToString(u) +} + +// TODO: We don't want to keep an unlimited amount of cookies in the storage. +// - The essential question is: how do we avoid DoS? +// - Which cookies are worth keeping? +// - Definitely logged-in users, only one person should know the password. +// - Evict by FIFO? LRU? +func sessionGet(w http.ResponseWriter, r *http.Request) (session *Session) { + if c, _ := r.Cookie("sessionid"); c != nil { + session, _ = sessions[c.Value] + } + if session == nil { + id := sessionGenId() + session = &Session{LoggedIn: false} + sessions[id] = session + http.SetCookie(w, &http.Cookie{Name: "sessionid", Value: id}) + } + return +} + +func sessionWrap(inner func(http.ResponseWriter, *http.Request)) func( + http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + // We might also try no-cache with an ETag for the whole database, + // though I don't expect any substantial improvements of anything. + w.Header().Set("Cache-Control", "no-store") + + redirect := "/login" + if r.RequestURI != "/" && r.Method == http.MethodGet { + redirect += "?redirect=" + url.QueryEscape(r.RequestURI) + } + + session := sessionGet(w, r) + if !session.LoggedIn { + http.Redirect(w, r, redirect, http.StatusSeeOther) + return + } + inner(w, r.WithContext( + context.WithValue(r.Context(), sessionContextKey{}, session))) + } +} diff --git a/label-tool/main.go b/label-tool/main.go deleted file mode 100644 index 39a840c..0000000 --- a/label-tool/main.go +++ /dev/null @@ -1,199 +0,0 @@ -package main - -import ( - "errors" - "html/template" - "image" - "image/png" - "log" - "net/http" - "os" - "strconv" - - "janouch.name/sklad/bdf" - "janouch.name/sklad/imgutil" - "janouch.name/sklad/label" - "janouch.name/sklad/ql" -) - -var font *bdf.Font - -var tmpl = template.Must(template.New("form").Parse(` - - -

PT-CBP label printing tool

- - - -
- - -
- {{ if .Printer }} - -

Printer: {{ .Printer.Manufacturer }} {{ .Printer.Model }} -

Tape: - {{ if .Printer.LastStatus }} - {{ .Printer.LastStatus.MediaWidthMM }} mm × - {{ .Printer.LastStatus.MediaLengthMM }} mm - - {{ if .MediaInfo }} - (offset: {{ .MediaInfo.SideMarginPins }} pt, - print area: {{ .MediaInfo.PrintAreaPins }} pt) - {{ else }} - (unknown media) - {{ end }} - - {{ if .Printer.LastStatus.Errors }} - {{ range .Printer.LastStatus.Errors }} -

Error: {{ . }} - {{ end }} - {{ end }} - - {{ end }} - {{ if .InitErr }} - {{ .InitErr }} - {{ end }} - - {{ else }} -

Error: {{ .PrinterErr }} - {{ end }} -

-
-

Font: {{ .Font.Name }} -

-
-

- - - -

- -

-
- -`)) - -func getPrinter() (*ql.Printer, error) { - printer, err := ql.Open() - if err != nil { - return nil, err - } - if printer == nil { - return nil, errors.New("no suitable printer found") - } - return printer, nil -} - -func getStatus(printer *ql.Printer) error { - if err := printer.Initialize(); err != nil { - return err - } - if err := printer.UpdateStatus(); err != nil { - return err - } - return nil -} - -func handle(w http.ResponseWriter, r *http.Request) { - if err := r.ParseForm(); err != nil { - http.Error(w, err.Error(), 500) - return - } - - var ( - initErr error - mediaInfo *ql.MediaInfo - ) - printer, printerErr := getPrinter() - if printerErr == nil { - defer printer.Close() - printer.StatusNotify = func(status *ql.Status) { - log.Printf("\x1b[1mreceived status\x1b[m\n%s", status) - } - - if initErr = getStatus(printer); initErr == nil { - mediaInfo = ql.GetMediaInfo( - printer.LastStatus.MediaWidthMM(), - printer.LastStatus.MediaLengthMM(), - ) - } - } - - var params = struct { - Printer *ql.Printer - PrinterErr error - InitErr error - MediaInfo *ql.MediaInfo - Font *bdf.Font - Text string - Scale int - }{ - Printer: printer, - PrinterErr: printerErr, - InitErr: initErr, - MediaInfo: mediaInfo, - Font: font, - Text: r.FormValue("text"), - } - - var err error - params.Scale, err = strconv.Atoi(r.FormValue("scale")) - if err != nil { - params.Scale = 3 - } - - var img image.Image - if mediaInfo != nil { - img = &imgutil.LeftRotate{Image: label.GenLabelForHeight( - font, params.Text, mediaInfo.PrintAreaPins, params.Scale)} - if r.FormValue("print") != "" { - if err := printer.Print(img); err != nil { - log.Println("print error:", err) - } - } - } - - if _, ok := r.Form["img"]; !ok { - w.Header().Set("Content-Type", "text/html") - tmpl.Execute(w, ¶ms) - return - } - - if mediaInfo == nil { - http.Error(w, "unknown media", 500) - return - } - - w.Header().Set("Content-Type", "image/png") - if err := png.Encode(w, img); err != nil { - http.Error(w, err.Error(), 500) - return - } -} - -func main() { - if len(os.Args) != 3 { - log.Fatalf("usage: %s ADDRESS BDF-FILE\n", os.Args[0]) - } - - address, bdfPath := os.Args[1], os.Args[2] - - var err error - fi, err := os.Open(bdfPath) - if err != nil { - log.Fatalln(err) - } - - font, err = bdf.NewFromBDF(fi) - if err := fi.Close(); err != nil { - log.Fatalln(err) - } - if err != nil { - log.Fatalln(err) - } - - log.Println("starting server") - http.HandleFunc("/", handle) - log.Fatalln(http.ListenAndServe(address, nil)) -} diff --git a/ql-info/main.go b/ql-info/main.go deleted file mode 100644 index 8d53d63..0000000 --- a/ql-info/main.go +++ /dev/null @@ -1,41 +0,0 @@ -package main - -import ( - "fmt" - "log" - - "janouch.name/sklad/ql" -) - -func main() { - printer, err := ql.Open() - if err != nil { - log.Fatalln(err) - } - if printer == nil { - log.Fatalln("no suitable printer found") - } - - defer printer.Close() - - fmt.Printf("\x1b[1m%s %s\x1b[m\n", printer.Manufacturer, printer.Model) - if err := printer.Initialize(); err != nil { - log.Fatalln(err) - } - if err := printer.UpdateStatus(); err != nil { - log.Fatalln(err) - } - - status := printer.LastStatus - fmt.Print(status) - - fmt.Println("\x1b[1mMedia information\x1b[m") - if mi := ql.GetMediaInfo( - status.MediaWidthMM(), status.MediaLengthMM()); mi != nil { - fmt.Println("side margin pins:", mi.SideMarginPins) - fmt.Println("print area pins:", mi.PrintAreaPins) - fmt.Println("print area length:", mi.PrintAreaLength) - } else { - fmt.Println("unknown media") - } -} diff --git a/ql-print/main.go b/ql-print/main.go deleted file mode 100644 index d0c986d..0000000 --- a/ql-print/main.go +++ /dev/null @@ -1,88 +0,0 @@ -package main - -import ( - "image" - _ "image/gif" - _ "image/jpeg" - _ "image/png" - - "flag" - "fmt" - "log" - "os" - - "janouch.name/sklad/imgutil" - "janouch.name/sklad/ql" -) - -var scale = flag.Int("scale", 1, "integer upscaling") -var rotate = flag.Bool("rotate", false, "print sideways") - -func main() { - flag.Usage = func() { - fmt.Fprintf(os.Stderr, "Usage: %s IMAGE\n", os.Args[0]) - flag.PrintDefaults() - } - - flag.Parse() - if flag.NArg() != 1 { - flag.Usage() - os.Exit(1) - } - - // Open the picture. - f, err := os.Open(flag.Arg(0)) - if err != nil { - log.Fatalln(err) - } - defer f.Close() - - // Load and eventually transform the picture. - img, _, err := image.Decode(f) - if err != nil { - log.Fatalln(err) - } - if *scale > 1 { - img = &imgutil.Scale{Image: img, Scale: *scale} - } - if *rotate { - img = &imgutil.LeftRotate{Image: img} - } - - // Open and initialize the printer. - p, err := ql.Open() - if err != nil { - log.Fatalln(err) - } - if p == nil { - log.Fatalln("no suitable printer found") - } - if err := p.Initialize(); err != nil { - log.Fatalln(err) - } - if err := p.UpdateStatus(); err != nil { - log.Fatalln(err) - } - - // Check the picture against the media in the printer. - mi := ql.GetMediaInfo( - p.LastStatus.MediaWidthMM(), - p.LastStatus.MediaLengthMM(), - ) - if mi == nil { - log.Fatalln("unknown media") - } - - bounds := img.Bounds() - dx, dy := bounds.Dx(), bounds.Dy() - if dx > mi.PrintAreaPins { - log.Fatalln("the image is too wide,", dx, ">", mi.PrintAreaPins, "pt") - } - if dy > mi.PrintAreaLength && mi.PrintAreaLength != 0 { - log.Fatalln("the image is too high,", dy, ">", mi.PrintAreaLength, "pt") - } - - if err := p.Print(img); err != nil { - log.Fatalln(err) - } -} diff --git a/sklad/base.tmpl b/sklad/base.tmpl deleted file mode 100644 index d92a818..0000000 --- a/sklad/base.tmpl +++ /dev/null @@ -1,68 +0,0 @@ - - - - {{ template "Title" . }} - sklad - - - - - - -
-

sklad

- -{{ block "HeaderControls" . }} - Obaly - Řady - -
- -
- -
- -
-{{ end }} - -
- -{{ template "Content" . }} - - diff --git a/sklad/container.tmpl b/sklad/container.tmpl deleted file mode 100644 index 4bacae8..0000000 --- a/sklad/container.tmpl +++ /dev/null @@ -1,99 +0,0 @@ -{{ define "Title" }}{{/* -*/}}{{ if .Container }}{{ .Container.Id }}{{ else }}Obaly{{ end }}{{ end }} -{{ define "Content" }} - -{{ if .Container }} - -
-
-

{{ .Container.Id }}

-
- -
-
- -
-
- -
- -
-
- - -
-
- - -
- -
-
-
- -

Podobaly

- -{{ else }} -
-
-

Nový obal

-
-
- -
-
- - -
-
- - -
- -
-
-
- -

Obaly nejvyšší úrovně

-{{ end }} - -{{ range .Children }} -
-
-

{{ .Id }}

-
- -
-
- -
-
-{{ if .Description }} -

{{ .Description }} -{{ end }} -{{ if .Children }} -

-{{ range .Children }} -{{ .Id }} -{{ end }} -{{ end }} -

-{{ else }} -

Obal je prázdný. -{{ end }} - -{{ end }} diff --git a/sklad/db.go b/sklad/db.go deleted file mode 100644 index def18a5..0000000 --- a/sklad/db.go +++ /dev/null @@ -1,225 +0,0 @@ -package main - -import ( - "encoding/json" - "errors" - "fmt" - "os" - "strings" - "time" - - "janouch.name/sklad/bdf" -) - -type Series struct { - Prefix string // PK: prefix - Description string // what kind of containers this is for -} - -type ContainerId string - -type Container struct { - Series string // PK: what series does this belong to - Number uint // PK: order within the series - Parent ContainerId // the container we're in, if any, otherwise "" - Description string // description and/or contents of this container -} - -func (c *Container) Id() ContainerId { - return ContainerId(fmt.Sprintf("%s%s%d", db.Prefix, c.Series, c.Number)) -} - -func (c *Container) Children() []*Container { - // TODO: Sort this by Id, or maybe even return a map[string]*Container, - // text/template would sort that automatically. - return indexChildren[c.Id()] -} - -func (c *Container) Path() (result []ContainerId) { - for c != nil && c.Parent != "" { - c = indexContainer[c.Parent] - result = append(result, c.Id()) - } - return -} - -type Database struct { - Password string // password for web users - Prefix string // prefix for all container IDs - Series []*Series // all known series - Containers []*Container // all known containers - - BDFPath string // path to bitmap font file - BDFScale int // integer scaling for the bitmap font -} - -var ( - dbPath string - db Database - dbLast Database - dbLog *os.File - - indexSeries = map[string]*Series{} - indexContainer = map[ContainerId]*Container{} - indexChildren = map[ContainerId][]*Container{} - - labelFont *bdf.Font -) - -// TODO: Some functions to add, remove and change things in the database. -// Indexes must be kept valid, just like any invariants. - -func dbSearchSeries(query string) (result []*Series) { - query = strings.ToLower(query) - added := map[string]bool{} - for _, s := range db.Series { - if query == strings.ToLower(s.Prefix) { - result = append(result, s) - added[s.Prefix] = true - } - } - for _, s := range db.Series { - if strings.Contains( - strings.ToLower(s.Description), query) && !added[s.Prefix] { - result = append(result, s) - } - } - return -} - -func dbSearchContainers(query string) (result []*Container) { - query = strings.ToLower(query) - added := map[ContainerId]bool{} - for id, c := range indexContainer { - if query == strings.ToLower(string(id)) { - result = append(result, c) - added[id] = true - } - } - for id, c := range indexContainer { - if strings.Contains( - strings.ToLower(c.Description), query) && !added[id] { - result = append(result, c) - } - } - return -} - -func dbCommit() error { - // Write a timestamp. - e := json.NewEncoder(dbLog) - e.SetIndent("", " ") - if err := e.Encode(time.Now().Format(time.RFC3339)); err != nil { - return err - } - - // Back up the current database contents. - if err := e.Encode(&dbLast); err != nil { - return err - } - if err := dbLog.Sync(); err != nil { - return err - } - - // Atomically replace the current database file. - tempPath := dbPath + ".new" - temp, err := os.OpenFile(tempPath, os.O_WRONLY|os.O_CREATE, 0644) - if err != nil { - return err - } - defer temp.Close() - - e = json.NewEncoder(temp) - e.SetIndent("", " ") - if err := e.Encode(&db); err != nil { - return err - } - - if err := os.Rename(tempPath, dbPath); err != nil { - return err - } - - dbLast = db - return nil -} - -// loadDatabase loads the database from a simple JSON file. We do not use -// any SQL stuff or even external KV storage because there is no real need -// for our trivial use case, with our general amount of data. -func loadDatabase() error { - dbFile, err := os.Open(dbPath) - if err != nil { - return err - } - if err := json.NewDecoder(dbFile).Decode(&db); err != nil { - return err - } - - // Further validate the database. - if db.Prefix == "" { - return errors.New("misconfigured prefix") - } - - // Construct indexes for primary keys, validate against duplicates. - for _, pv := range db.Series { - if _, ok := indexSeries[pv.Prefix]; ok { - return fmt.Errorf("duplicate series: %s", pv.Prefix) - } - indexSeries[pv.Prefix] = pv - } - for _, pv := range db.Containers { - id := pv.Id() - if _, ok := indexContainer[id]; ok { - return fmt.Errorf("duplicate container: %s", id) - } - indexContainer[id] = pv - } - - // Construct an index that goes from parent containers to their children. - for _, pv := range db.Containers { - if pv.Parent != "" { - if _, ok := indexContainer[pv.Parent]; !ok { - return fmt.Errorf("container %s has a nonexistent parent %s", - pv.Id(), pv.Parent) - } - } - indexChildren[pv.Parent] = append(indexChildren[pv.Parent], pv) - } - - // Validate that no container is a parent of itself on any level. - // This could probably be optimized but it would stop being obvious. - for _, pv := range db.Containers { - parents := map[ContainerId]bool{pv.Id(): true} - for pv.Parent != "" { - if parents[pv.Parent] { - return fmt.Errorf("%s contains itself", pv.Parent) - } - parents[pv.Parent] = true - pv = indexContainer[pv.Parent] - } - } - - // Prepare label printing. - if db.BDFScale <= 0 { - db.BDFScale = 1 - } - - if f, err := os.Open(db.BDFPath); err != nil { - return fmt.Errorf("cannot load label font: %s", err) - } else { - defer f.Close() - if labelFont, err = bdf.NewFromBDF(f); err != nil { - return fmt.Errorf("cannot load label font: %s", err) - } - } - - // Open database log file for appending. - if dbLog, err = os.OpenFile(dbPath+".log", - os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err != nil { - return err - } - - // Remember the current state of the database. - dbLast = db - return nil -} diff --git a/sklad/label.tmpl b/sklad/label.tmpl deleted file mode 100644 index da17c58..0000000 --- a/sklad/label.tmpl +++ /dev/null @@ -1,13 +0,0 @@ -{{ define "Title" }}Tisk štítku{{ end }} -{{ define "Content" }} -

Tisk štítku pro {{ .Id }}

- -{{ if .UnknownId }} -

Neznámý obal. -{{ else if .Error }} -

Tisk selhal: {{ .Error }} -{{ else }} -

Tisk proběhl úspěšně. -{{ end }} - -{{ end }} diff --git a/sklad/login.tmpl b/sklad/login.tmpl deleted file mode 100644 index c34ab53..0000000 --- a/sklad/login.tmpl +++ /dev/null @@ -1,17 +0,0 @@ -{{ define "Title" }}Přihlášení{{ end }} -{{ define "HeaderControls" }}{{ end }} -{{ define "Content" }} - -

Přihlášení

- -
- - -
- -{{ if .IncorrectPassword }} -

Bylo zadáno nesprávné heslo. -{{ end }} - -{{ end }} diff --git a/sklad/main.go b/sklad/main.go deleted file mode 100644 index 32dd68b..0000000 --- a/sklad/main.go +++ /dev/null @@ -1,271 +0,0 @@ -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)) -} diff --git a/sklad/search.tmpl b/sklad/search.tmpl deleted file mode 100644 index cf704cf..0000000 --- a/sklad/search.tmpl +++ /dev/null @@ -1,38 +0,0 @@ -{{ define "Title" }}„{{ .Query }}“ — Vyhledávání{{ end }} -{{ define "Content" }} - -

Vyhledávání: „{{ .Query }}“

- -

Řady

- -{{ range .Series }} -
-
-

{{ .Prefix }}

-

{{ .Description }} -

-
-{{ else }} -

Neodpovídají žádné řady. -{{ end }} - -

Obaly

- -{{ range .Containers }} -
-
-

{{ .Id }} -{{ range .Path }} - « {{ . }} -{{ end }} -

-
-{{ if .Description }} -

{{ .Description }} -{{ end }} -

-{{ else }} -

Neodpovídají žádné obaly. -{{ end }} - -{{ end }} diff --git a/sklad/series.tmpl b/sklad/series.tmpl deleted file mode 100644 index 4956e3a..0000000 --- a/sklad/series.tmpl +++ /dev/null @@ -1,43 +0,0 @@ -{{ define "Title" }}{{ or .Prefix "Řady" }}{{ end }} -{{ define "Content" }} - -{{ if .Prefix }} -

{{ .Prefix }}

- -{{ if .Description }} -

{{ .Description }} -{{ end }} -{{ else }} - -

-
-
-

Nová řada

- - - -
- -
- -{{ range $prefix, $desc := .AllSeries }} -
-
-

{{ $prefix }}

-
- -
-
- -
-
-
-{{ else }} -

Nejsou žádné řady. -{{ end }} - -{{ end }} - -{{ end }} diff --git a/sklad/session.go b/sklad/session.go deleted file mode 100644 index 02fe0b0..0000000 --- a/sklad/session.go +++ /dev/null @@ -1,66 +0,0 @@ -package main - -import ( - "context" - "encoding/hex" - "math/rand" - "net/http" - "net/url" -) - -// session storage indexed by a random UUID -var sessions = map[string]*Session{} - -type Session struct { - LoggedIn bool // may access the DB -} - -type sessionContextKey struct{} - -func sessionGenId() string { - u := make([]byte, 16) - if _, err := rand.Read(u); err != nil { - panic("cannot generate random bytes") - } - return hex.EncodeToString(u) -} - -// TODO: We don't want to keep an unlimited amount of cookies in the storage. -// - The essential question is: how do we avoid DoS? -// - Which cookies are worth keeping? -// - Definitely logged-in users, only one person should know the password. -// - Evict by FIFO? LRU? -func sessionGet(w http.ResponseWriter, r *http.Request) (session *Session) { - if c, _ := r.Cookie("sessionid"); c != nil { - session, _ = sessions[c.Value] - } - if session == nil { - id := sessionGenId() - session = &Session{LoggedIn: false} - sessions[id] = session - http.SetCookie(w, &http.Cookie{Name: "sessionid", Value: id}) - } - return -} - -func sessionWrap(inner func(http.ResponseWriter, *http.Request)) func( - http.ResponseWriter, *http.Request) { - return func(w http.ResponseWriter, r *http.Request) { - // We might also try no-cache with an ETag for the whole database, - // though I don't expect any substantial improvements of anything. - w.Header().Set("Cache-Control", "no-store") - - redirect := "/login" - if r.RequestURI != "/" && r.Method == http.MethodGet { - redirect += "?redirect=" + url.QueryEscape(r.RequestURI) - } - - session := sessionGet(w, r) - if !session.LoggedIn { - http.Redirect(w, r, redirect, http.StatusSeeOther) - return - } - inner(w, r.WithContext( - context.WithValue(r.Context(), sessionContextKey{}, session))) - } -} -- cgit v1.2.3-70-g09d2