aboutsummaryrefslogtreecommitdiffgithub
diff options
context:
space:
mode:
-rw-r--r--tool.go12
-rw-r--r--toolbag.go19
-rwxr-xr-xtoolbag/run3
-rw-r--r--toolbag/systemd/toolbag.service6
-rw-r--r--tools/all.go2
-rw-r--r--tools/figlet.go9
-rw-r--r--tools/notfound.go9
-rw-r--r--tools/share/templates/figlet.html2
-rw-r--r--tools/share/templates/notfound.html1
-rw-r--r--tools/share/templates/tools.html46
-rw-r--r--tools/tools.go78
11 files changed, 163 insertions, 24 deletions
diff --git a/tool.go b/tool.go
index 1fd3516..9aec3ec 100644
--- a/tool.go
+++ b/tool.go
@@ -26,7 +26,7 @@ type Tool interface {
Name() string
Desc() string
Path() string
- AddArgs()
+ AddArgs(*ToolBag)
Init() error
http.Handler
}
@@ -44,11 +44,11 @@ func NewToolFuncAt(name, desc, path string, f http.HandlerFunc) *ToolFunc {
return &ToolFunc{name, desc, path, f}
}
-func (ft *ToolFunc) Name() string { return ft.name }
-func (ft *ToolFunc) Desc() string { return ft.desc }
-func (ft *ToolFunc) Path() string { return ft.path }
-func (ft *ToolFunc) AddArgs() {}
-func (ft *ToolFunc) Init() error { return nil }
+func (ft *ToolFunc) Name() string { return ft.name }
+func (ft *ToolFunc) Desc() string { return ft.desc }
+func (ft *ToolFunc) Path() string { return ft.path }
+func (ft *ToolFunc) AddArgs(*ToolBag) {}
+func (ft *ToolFunc) Init() error { return nil }
// just a way to compose names of flags
func Arg(t Tool, argname string) string {
diff --git a/toolbag.go b/toolbag.go
index 75a4e80..f9cf00e 100644
--- a/toolbag.go
+++ b/toolbag.go
@@ -17,23 +17,34 @@
package toolbag
import (
+ "flag"
"net/http"
)
type ToolBag struct {
Tools []Tool
*http.ServeMux
+ *flag.FlagSet
}
func NewToolBag(tools ...Tool) *ToolBag {
- mux := http.NewServeMux()
+ tb := &ToolBag{tools, http.NewServeMux(),
+ // neither the program name nor choice of error handling for
+ // FlagSet.Parse() matters because we're collecting flags, not
+ // parsing them. so, pass some random (but valid) stuff
+ flag.NewFlagSet("toolbag", flag.PanicOnError)}
for _, tool := range tools {
- tool.AddArgs()
- mux.Handle(tool.Path(), tool)
+ tool.AddArgs(tb)
+ tb.ServeMux.Handle(tool.Path(), tool)
}
- return &ToolBag{tools, mux}
+ // for now, just pass the args onto the global flag instance
+ tb.FlagSet.VisitAll(func(f *flag.Flag) {
+ flag.Var(f.Value, f.Name, f.Usage)
+ })
+
+ return tb
}
func (tb *ToolBag) Init() error {
diff --git a/toolbag/run b/toolbag/run
index 5af4f56..7bf5faf 100755
--- a/toolbag/run
+++ b/toolbag/run
@@ -11,6 +11,7 @@ popd >/dev/null
./toolbag -http -tcp :8030 \
-figlet:template share/templates/figlet.html -figlet:tcp 127.0.0.1:4000 \
- -notfound:template share/templates/notfound.html -notfound:dir share/notfound-img
+ -notfound:template share/templates/notfound.html -notfound:dir share/notfound-img \
+ -tools:template share/templates/tools.html -tools:source https://code.austinjadams.com/toolbag/tree/tools/
kill $pid
diff --git a/toolbag/systemd/toolbag.service b/toolbag/systemd/toolbag.service
index 8b851ad..5e0038f 100644
--- a/toolbag/systemd/toolbag.service
+++ b/toolbag/systemd/toolbag.service
@@ -5,9 +5,13 @@ Description=toolbag
Type=simple
User=www-data
WorkingDirectory=/usr/local/share/toolbag
+# hack to remove the old socket, which implementation of toolbag doesn't
+# currently delete
+ExecStartPre=/bin/rm -vf /run/toolbag/sock
ExecStart=/usr/local/bin/toolbag -fcgi -unix /run/toolbag/sock \
-figlet:template templates/figlet.html -figlet:tcp execd:4000 \
- -notfound:template templates/notfound.html -notfound:dir notfound-img
+ -notfound:template templates/notfound.html -notfound:dir notfound-img \
+ -tools:template templates/tools.html -tools:source https://code.austinjadams.com/toolbag/tree/tools/
[Install]
WantedBy=multi-user.target
diff --git a/tools/all.go b/tools/all.go
index 00989ee..d901e56 100644
--- a/tools/all.go
+++ b/tools/all.go
@@ -36,7 +36,7 @@ var (
)
func All() *tb.ToolBag {
- return tb.NewToolBag(IP, Headers, NewFiglet(), NewNotFound())
+ return tb.NewToolBag(IP, Headers, NewFiglet(), NewNotFound(), NewTools())
}
// nasty hack to remove port numbers and the brackets from ipv6 addresses
diff --git a/tools/figlet.go b/tools/figlet.go
index 26e3318..2e69856 100644
--- a/tools/figlet.go
+++ b/tools/figlet.go
@@ -18,7 +18,6 @@ package tools
import (
"errors"
- "flag"
"html/template"
"net/http"
"strings"
@@ -45,10 +44,10 @@ func NewFiglet() *Figlet {
func (f *Figlet) Name() string { return "figlet" }
func (f *Figlet) Desc() string { return "a web frontend to figlet" }
func (f *Figlet) Path() string { return "/" + f.Name() }
-func (f *Figlet) AddArgs() {
- flag.StringVar(&f.args.template, tb.Arg(f, "template"), "", "path to template")
- flag.StringVar(&f.args.unix, tb.Arg(f, "unix"), "", "path to unix socket to execd")
- flag.StringVar(&f.args.tcp, tb.Arg(f, "tcp"), "", "tcp address to execd")
+func (f *Figlet) AddArgs(toolbag *tb.ToolBag) {
+ toolbag.StringVar(&f.args.template, tb.Arg(f, "template"), "", "path to template")
+ toolbag.StringVar(&f.args.unix, tb.Arg(f, "unix"), "", "path to unix socket to execd")
+ toolbag.StringVar(&f.args.tcp, tb.Arg(f, "tcp"), "", "tcp address to execd")
}
func (f *Figlet) makeClient() (*execd.Client, error) {
diff --git a/tools/notfound.go b/tools/notfound.go
index 9e70cbb..e8c504c 100644
--- a/tools/notfound.go
+++ b/tools/notfound.go
@@ -18,7 +18,6 @@ package tools
import (
"errors"
- "flag"
"html/template"
"io/ioutil"
"math/rand"
@@ -48,14 +47,14 @@ func NewNotFound() *NotFound {
}
func (nf *NotFound) Name() string { return "notfound" }
-func (nf *NotFound) Desc() string { return "not found page showing a random math problem" }
+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() {
- flag.StringVar(&nf.args.dir, tb.Arg(nf, "dir"), "", "directory with the math problems")
- flag.StringVar(&nf.args.template, tb.Arg(nf, "template"), "", "path to template")
+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 {
diff --git a/tools/share/templates/figlet.html b/tools/share/templates/figlet.html
index 48a735d..1d3f4b3 100644
--- a/tools/share/templates/figlet.html
+++ b/tools/share/templates/figlet.html
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
- <title>figlet frontend</title>
+ <title>figlet frontend « austin adams</title>
<style type="text/css">
form {
display: block;
diff --git a/tools/share/templates/notfound.html b/tools/share/templates/notfound.html
index 8150d54..ba99e76 100644
--- a/tools/share/templates/notfound.html
+++ b/tools/share/templates/notfound.html
@@ -24,3 +24,4 @@
<img alt="{{ .Alt }}" src="data:image/png;base64,{{ .Base64 }}" />
</body>
</html>
+<!-- vim: set ts=2 sw=2 et : -->
diff --git a/tools/share/templates/tools.html b/tools/share/templates/tools.html
new file mode 100644
index 0000000..278858e
--- /dev/null
+++ b/tools/share/templates/tools.html
@@ -0,0 +1,46 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <meta charset="utf-8">
+ <title>tools « austin adams</title>
+ <style type="text/css">
+ html, body {
+ width: 100%;
+ height: 100%;
+ margin: 0;
+ padding: 0;
+ background-color: white;
+ font-size: 12px;
+ }
+
+ h1 {
+ font-size: 24px;
+ }
+
+ div {
+ display: block;
+ width: 512px;
+ margin: auto;
+ position: relative;
+ top: 128px;
+ }
+
+ li span {
+ width: 85%;
+ float: right;
+ }
+ </style>
+</head>
+<body>
+ <div>
+ <h1>tools</h1>
+ <ul>
+ {{ range .Tools }}
+ <li><a href="{{ .Path }}">{{ .Name }}</a> <span>&mdash; {{ .Desc }}</span></li>
+ {{ end }}
+ </ul>
+ <a href="{{ .Source }}">source</a>
+ </div>
+</body>
+</html>
+<!-- vim: set ts=2 sw=2 et : -->
diff --git a/tools/tools.go b/tools/tools.go
new file mode 100644
index 0000000..c0a355a
--- /dev/null
+++ b/tools/tools.go
@@ -0,0 +1,78 @@
+// 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"
+ "net/http"
+
+ tb "code.austinjadams.com/toolbag"
+)
+
+type Tools struct {
+ tb *tb.ToolBag
+ templ *template.Template
+
+ args struct {
+ source, template string
+ }
+}
+
+func NewTools() *Tools {
+ return &Tools{}
+}
+
+func (t *Tools) Name() string { return "tools" }
+func (t *Tools) Desc() string { return "list tools" }
+
+// be a catch-all
+func (t *Tools) Path() string { return "/" + t.Name() }
+
+func (t *Tools) AddArgs(toolbag *tb.ToolBag) {
+ // XXX kind of a hack, but no regrets here
+ t.tb = toolbag
+ toolbag.StringVar(&t.args.template, tb.Arg(t, "template"), "", "path to template")
+ toolbag.StringVar(&t.args.source, tb.Arg(t, "source"), "", "url of toolbag source")
+}
+
+func (t *Tools) Init() error {
+ if t.args.template == "" {
+ return errors.New("missing template arg")
+ }
+ if t.args.source == "" {
+ return errors.New("missing source arg")
+ }
+
+ templ, err := template.ParseFiles(t.args.template)
+ if err != nil {
+ return err
+ }
+ t.templ = templ
+ return nil
+}
+
+func (t *Tools) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ err := t.templ.Execute(w, struct {
+ Source string
+ Tools []tb.Tool
+ }{t.args.source, t.tb.Tools})
+
+ if err != nil {
+ tb.LogWhine(t, err)
+ }
+}