aboutsummaryrefslogtreecommitdiffgithub
path: root/tools/notfound.go
blob: e8c504cc5962f9ba665b515ff518aa4d172626f2 (plain)
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
// toolbag - dynamic tools for my website
// Copyright (C) 2015 Austin Adams
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package tools

import (
	"errors"
	"html/template"
	"io/ioutil"
	"math/rand"
	"net/http"
	"os"
	"path"
	"strconv"

	tb "code.austinjadams.com/toolbag"
)

type notFoundImg struct {
	Alt, Base64 string
}

type NotFound struct {
	images []*notFoundImg
	templ  *template.Template

	args struct {
		dir, template string
	}
}

func NewNotFound() *NotFound {
	return &NotFound{}
}

func (nf *NotFound) Name() string { return "notfound" }
func (nf *NotFound) Desc() string { return "show a random math problem" }

// be a catch-all
func (nf *NotFound) Path() string { return "/" }

func (nf *NotFound) AddArgs(toolbag *tb.ToolBag) {
	toolbag.StringVar(&nf.args.dir, tb.Arg(nf, "dir"), "", "directory with the math problems")
	toolbag.StringVar(&nf.args.template, tb.Arg(nf, "template"), "", "path to template")
}

func (nf *NotFound) Init() error {
	if nf.args.dir == "" {
		return errors.New("missing dir arg")
	}
	if nf.args.template == "" {
		return errors.New("missing template arg")
	}

	templ, err := template.ParseFiles(nf.args.template)
	if err != nil {
		return err
	}
	nf.templ = templ

	f, err := os.Open(nf.args.dir)
	if err != nil {
		return err
	}
	names, err := f.Readdirnames(0)
	if err != nil {
		return err
	}

	for _, name := range names {
		imgpath := path.Join(nf.args.dir, name)
		contents, err := ioutil.ReadFile(imgpath)
		if err != nil {
			return err
		}
		pieces := splitLines(string(contents))

		if len(pieces) != 2 {
			return errors.New("malformed file " + imgpath + ". got " + strconv.Itoa(len(pieces)) +
				" lines, expected 2 (first with alttext, second with base64)")
		}

		nf.images = append(nf.images, &notFoundImg{pieces[0], pieces[1]})
	}
	return nil
}

func (nf *NotFound) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusNotFound)
	// deterministic, but who cares?
	i := rand.Intn(len(nf.images))

	err := nf.templ.Execute(w, nf.images[i])
	// template probably already started writing the request, so just log the error
	if err != nil {
		tb.LogWhine(nf, err)
	}
}