From 8803281285658b38a61448f0bcf4254e860d1df5 Mon Sep 17 00:00:00 2001 From: Daan Sprenkels Date: Fri, 6 Sep 2019 00:07:50 +0200 Subject: [PATCH] Put index in template --- go.mod | 2 -- handlers.go | 14 +++++++++----- {assets => text}/index.txt | 0 3 files changed, 9 insertions(+), 7 deletions(-) rename {assets => text}/index.txt (100%) diff --git a/go.mod b/go.mod index 7532190..08f20a7 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,6 @@ go 1.12 require ( github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect github.com/gorilla/mux v1.7.3 - github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect - github.com/modern-go/reflect2 v1.0.1 // indirect github.com/pkg/errors v0.8.1 github.com/prometheus/client_golang v1.1.0 go.etcd.io/bbolt v1.3.3 diff --git a/handlers.go b/handlers.go index fdd71f0..8af4f38 100644 --- a/handlers.go +++ b/handlers.go @@ -2,7 +2,7 @@ package main //go:generate go get github.com/go-bindata/go-bindata //go:generate go get -u github.com/go-bindata/go-bindata/... -//go:generate go-bindata -pkg $GOPACKAGE assets/ +//go:generate go-bindata -pkg $GOPACKAGE text/ import ( "bytes" @@ -15,6 +15,7 @@ import ( "net/http" "net/url" "strings" + "text/template" "time" "unicode" @@ -56,7 +57,8 @@ var base64Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345 var base64Encoder = base64.NewEncoding(base64Alphabet).WithPadding(base64.NoPadding) // Page contents -var indexContents = MustAsset("assets/index.txt") +var baseTemplate = template.New("empty") +var indexTemplate = template.Must(baseTemplate.Parse(string(MustAsset("text/index.txt")))) func (t PasteType) String() (string, error) { switch t { @@ -81,8 +83,7 @@ func (t PasteState) String() (string, error) { } func indexGetHandler(w http.ResponseWriter, r *http.Request) { - _, err := w.Write(indexContents) - if err != nil { + if err := indexTemplate.Execute(w, nil); err != nil { panic(err) } } @@ -245,7 +246,10 @@ func shortenPostHandler(w http.ResponseWriter, r *http.Request) { saveURL, err := r.URL.Parse(string(storedPaste.Key)) if err != nil { - log.Printf("error: %v\n", errors.Wrap(err, "parsing url")) + err = errors.Wrap(err, "parsing url") + log.Printf("error: %v\n", err) + fmt.Fprintf(w, "internal server error: %v\n", err) + return } var base64OwnerToken = make([]byte, 24) base64Encoder.Encode(base64OwnerToken, storedPaste.OwnerToken[:]) diff --git a/assets/index.txt b/text/index.txt similarity index 100% rename from assets/index.txt rename to text/index.txt