diff --git a/.travis.yml b/.travis.yml index 075bdd3..a3d0800 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,7 @@ script: - go get github.com/cloudflare/redoctober - go test github.com/cloudflare/redoctober... - go vet github.com/cloudflare/redoctober... + - ./scripts/validate-html-generation - go list -f '{{if len .TestGoFiles}}"go test -coverprofile={{.Dir}}/.coverprofile {{.ImportPath}}"{{end}}' ./... | xargs -i sh -c {} - gover . coverprofile.txt after_success: diff --git a/generate.go b/generate.go new file mode 100644 index 0000000..56b3ae3 --- /dev/null +++ b/generate.go @@ -0,0 +1,36 @@ +// +build ignore + +package main + +import ( + "io" + "os" + "path/filepath" +) + +// Reads static/index.html and saves as a constant in static.go +func main() { + wd, err := os.Getwd() + if err != nil { + panic(err) + } + out, err := os.Create(filepath.Join(wd, "static.go")) + if err != nil { + panic(err) + } + indexPath := filepath.Join(wd, "static", "index.html") + + out.Write([]byte("// This file is autogenerated; DO NOT EDIT DIRECTLY\n// See generate.go for more info\npackage main\n\nconst (\n")) + out.Write([]byte("\tindexHtml = `")) + f, err := os.Open(indexPath) + if err != nil { + panic(err) + } + defer f.Close() + if _, err := io.Copy(out, f); err != nil { + panic(err) + } + + out.Write([]byte("`\n")) + out.Write([]byte(")\n")) +} diff --git a/redoctober.go b/redoctober.go index 02f5ef8..1151006 100644 --- a/redoctober.go +++ b/redoctober.go @@ -198,7 +198,7 @@ func (this *indexHandler) handle(w http.ResponseWriter, r *http.Request) { defer f.Close() body = f } else { - body = bytes.NewReader(indexHtml) + body = bytes.NewReader([]byte(indexHtml)) } header := w.Header() @@ -219,7 +219,21 @@ multi-cert example: redoctober -vaultpath diskrecord.json -addr localhost:8080 -certs cert1.pem,cert2.pem -keys cert1.key,cert2.key ` -func main() { +var ( + addr string + caPath string + certsPath string + hcHost string + hcKey string + hcRoom string + keysPath string + roHost string + staticPath string + useSystemdSocket bool + vaultPath string +) + +func init() { flag.Usage = func() { fmt.Fprint(os.Stderr, "main usage dump\n") fmt.Fprint(os.Stderr, usage) @@ -227,922 +241,41 @@ func main() { os.Exit(2) } - var staticPath = flag.String("static", "", "Path to override built-in index.html") - var vaultPath = flag.String("vaultpath", "diskrecord.json", "Path to the the disk vault") - var addr = flag.String("addr", "localhost:8080", "Server and port separated by :") - var useSystemdSocket = flag.Bool("systemdfds", false, "Use systemd socket activation to listen on a file. Useful for binding privileged sockets.") - var certsPathString = flag.String("certs", "", "Path(s) of TLS certificate in PEM format, comma-separated") - var keysPathString = flag.String("keys", "", "Path(s) of TLS private key in PEM format, comma-separated, must me in the same order as the certs") - var caPath = flag.String("ca", "", "Path of TLS CA for client authentication (optional)") - var hcKey = flag.String("hckey", "", "Hipchat API Key") - var hcRoom = flag.String("hcroom", "", "Hipchat Room Id") - var hcHost = flag.String("hchost", "", "Hipchat Url Base (ex: hipchat.com)") - var roHost = flag.String("rohost", "", "RedOctober Url Base (ex: localhost:8080)") - flag.Parse() + flag.StringVar(&addr, "addr", "localhost:8080", "Server and port separated by :") + flag.StringVar(&caPath, "ca", "", "Path of TLS CA for client authentication (optional)") + flag.StringVar(&certsPath, "certs", "", "Path(s) of TLS certificate in PEM format, comma-separated") + flag.StringVar(&hcHost, "hchost", "", "Hipchat Url Base (ex: hipchat.com)") + flag.StringVar(&hcKey, "hckey", "", "Hipchat API Key") + flag.StringVar(&hcRoom, "hcroom", "", "Hipchat Room Id") + flag.StringVar(&keysPath, "keys", "", "Path(s) of TLS private key in PEM format, comma-separated, must me in the same order as the certs") + flag.StringVar(&roHost, "rohost", "", "RedOctober Url Base (ex: localhost:8080)") + flag.StringVar(&staticPath, "static", "", "Path to override built-in index.html") + flag.BoolVar(&useSystemdSocket, "systemdfds", false, "Use systemd socket activation to listen on a file. Useful for binding privileged sockets.") + flag.StringVar(&vaultPath, "vaultpath", "diskrecord.json", "Path to the the disk vault") - if *vaultPath == "" || *certsPathString == "" || *keysPathString == "" || (*addr == "" && *useSystemdSocket == false) { + flag.Parse() +} + +//go:generate go run generate.go + +func main() { + if vaultPath == "" || certsPath == "" || keysPath == "" || + (addr == "" && useSystemdSocket == false) { fmt.Fprint(os.Stderr, usage) flag.PrintDefaults() os.Exit(2) } - certPaths := strings.Split(*certsPathString, ",") - keyPaths := strings.Split(*keysPathString, ",") + certPaths := strings.Split(certsPath, ",") + keyPaths := strings.Split(keysPath, ",") - if err := core.Init(*vaultPath, *hcKey, *hcRoom, *hcHost, *roHost); err != nil { - log.Fatalf(err.Error()) + if err := core.Init(vaultPath, hcKey, hcRoom, hcHost, roHost); err != nil { + log.Fatal(err) } - s, l, err := NewServer(*staticPath, *addr, *caPath, certPaths, keyPaths, *useSystemdSocket) + s, l, err := NewServer(staticPath, addr, caPath, certPaths, keyPaths, useSystemdSocket) if err != nil { log.Fatalf("Error starting redoctober server: %s\n", err) } s.Serve(l) } - -var indexHtml = []byte(` - -
-