Refacted, new look and raw download feature

This commit is contained in:
James Mills 2017-07-09 01:57:21 -07:00
parent 47ef82966c
commit 3e081f0aa0
No known key found for this signature in database
GPG Key ID: AC4C014F1440EBD6
8 changed files with 202 additions and 50 deletions

View File

@ -14,4 +14,6 @@ WORKDIR /go/src/pastebin
COPY . /go/src/pastebin COPY . /go/src/pastebin
RUN go get -v -d RUN go get -v -d
RUN go get github.com/GeertJohan/go.rice/rice
RUN rice embed-go
RUN go install -v RUN go install -v

102
server.go
View File

@ -3,10 +3,13 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"html/template"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
"strings"
"time"
// Logging // Logging
"github.com/unrolled/logger" "github.com/unrolled/logger"
@ -21,7 +24,6 @@ import (
"github.com/patrickmn/go-cache" "github.com/patrickmn/go-cache"
"github.com/renstrom/shortuuid" "github.com/renstrom/shortuuid"
"github.com/timewasted/go-accept-headers" "github.com/timewasted/go-accept-headers"
"go.iondynamics.net/templice"
) )
// AcceptedTypes ... // AcceptedTypes ...
@ -63,7 +65,7 @@ type Server struct {
bind string bind string
config Config config Config
store *cache.Cache store *cache.Cache
templates *templice.Template templates *Templates
router *httprouter.Router router *httprouter.Router
// Logger // Logger
@ -74,8 +76,13 @@ type Server struct {
stats *stats.Stats stats *stats.Stats
} }
func (s *Server) render(w http.ResponseWriter, tmpl string, data interface{}) { func (s *Server) render(name string, w http.ResponseWriter, ctx interface{}) {
err := s.templates.ExecuteTemplate(w, tmpl+".html", data) buf, err := s.templates.Exec(name, ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
_, err = buf.WriteTo(w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
} }
@ -97,7 +104,7 @@ func (s *Server) IndexHandler() httprouter.Handle {
switch accepts { switch accepts {
case "text/html": case "text/html":
s.render(w, "index", nil) s.render("index", w, nil)
case "text/plain": case "text/plain":
default: default:
} }
@ -109,23 +116,15 @@ func (s *Server) PasteHandler() httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
s.counters.Inc("n_paste") s.counters.Inc("n_paste")
var blob string blob := r.FormValue("blob")
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "Internal Error", http.StatusInternalServerError)
return
}
blob = string(body)
err = r.ParseForm()
if err != nil {
http.Error(w, "Internal Error", http.StatusInternalServerError)
return
}
if blob == "" { if blob == "" {
blob = r.Form.Get("blob") body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "Internal Error", http.StatusInternalServerError)
return
}
blob = string(body)
} }
if blob == "" { if blob == "" {
@ -148,6 +147,33 @@ func (s *Server) PasteHandler() httprouter.Handle {
} }
} }
// DownloadHandler ...
func (s *Server) DownloadHandler() httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
s.counters.Inc("n_download")
uuid := p.ByName("uuid")
if uuid == "" {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
blob, ok := s.store.Get(uuid)
if !ok {
http.Error(w, "Not Found", http.StatusNotFound)
return
}
content := strings.NewReader(blob.(string))
w.Header().Set("Content-Disposition", "attachment; filename="+uuid)
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Length", string(content.Size()))
http.ServeContent(w, r, uuid, time.Now(), content)
}
}
// ViewHandler ... // ViewHandler ...
func (s *Server) ViewHandler() httprouter.Handle { func (s *Server) ViewHandler() httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
@ -176,7 +202,16 @@ func (s *Server) ViewHandler() httprouter.Handle {
switch accepts { switch accepts {
case "text/html": case "text/html":
s.render(w, "view", struct{ Blob string }{Blob: blob.(string)}) s.render(
"view", w,
struct {
Blob string
UUID string
}{
Blob: blob.(string),
UUID: uuid,
},
)
case "text/plain": case "text/plain":
w.Write([]byte(blob.(string))) w.Write([]byte(blob.(string)))
default: default:
@ -213,8 +248,14 @@ func (s *Server) initRoutes() {
s.router.Handler("GET", "/debug/metrics", exp.ExpHandler(s.counters.r)) s.router.Handler("GET", "/debug/metrics", exp.ExpHandler(s.counters.r))
s.router.GET("/debug/stats", s.StatsHandler()) s.router.GET("/debug/stats", s.StatsHandler())
s.router.ServeFiles(
"/css/*filepath",
rice.MustFindBox("static/css").HTTPBox(),
)
s.router.GET("/", s.IndexHandler()) s.router.GET("/", s.IndexHandler())
s.router.POST("/", s.PasteHandler()) s.router.POST("/", s.PasteHandler())
s.router.GET("/download/:uuid", s.DownloadHandler())
s.router.GET("/view/:uuid", s.ViewHandler()) s.router.GET("/view/:uuid", s.ViewHandler())
} }
@ -225,7 +266,7 @@ func NewServer(bind string, config Config) *Server {
config: config, config: config,
router: httprouter.New(), router: httprouter.New(),
store: cache.New(cfg.expiry, cfg.expiry*2), store: cache.New(cfg.expiry, cfg.expiry*2),
templates: templice.New(rice.MustFindBox("templates")), templates: NewTemplates("base"),
// Logger // Logger
logger: logger.New(logger.Options{ logger: logger.New(logger.Options{
@ -239,10 +280,19 @@ func NewServer(bind string, config Config) *Server {
stats: stats.New(), stats: stats.New(),
} }
err := server.templates.Load() // Templates
if err != nil { box := rice.MustFindBox("templates")
log.Panicf("error loading templates: %s", err)
} indexTemplate := template.New("view")
template.Must(indexTemplate.Parse(box.MustString("index.html")))
template.Must(indexTemplate.Parse(box.MustString("base.html")))
viewTemplate := template.New("view")
template.Must(viewTemplate.Parse(box.MustString("view.html")))
template.Must(viewTemplate.Parse(box.MustString("base.html")))
server.templates.Add("index", indexTemplate)
server.templates.Add("view", viewTemplate)
server.initRoutes() server.initRoutes()

1
static/css/spectre-icons.min.css vendored Normal file

File diff suppressed because one or more lines are too long

1
static/css/spectre.min.css vendored Normal file

File diff suppressed because one or more lines are too long

53
templates.go Normal file
View File

@ -0,0 +1,53 @@
package main
import (
"bytes"
"fmt"
"html/template"
"io"
"log"
"sync"
)
type TemplateMap map[string]*template.Template
type Templates struct {
sync.Mutex
base string
templates TemplateMap
}
func NewTemplates(base string) *Templates {
return &Templates{
base: base,
templates: make(TemplateMap),
}
}
func (t *Templates) Add(name string, template *template.Template) {
t.Lock()
defer t.Unlock()
t.templates[name] = template
}
func (t *Templates) Exec(name string, ctx interface{}) (io.WriterTo, error) {
t.Lock()
defer t.Unlock()
template, ok := t.templates[name]
if !ok {
log.Printf("template %s not found", name)
return nil, fmt.Errorf("no such template: %s", name)
}
buf := bytes.NewBuffer([]byte{})
err := template.ExecuteTemplate(buf, t.base, ctx)
if err != nil {
log.Printf("error parsing template %s: %s", name, err)
return nil, err
}
return buf, nil
}

22
templates/base.html Normal file
View File

@ -0,0 +1,22 @@
{{define "base"}}
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="/css/spectre-icons.min.css">
<link rel="stylesheet" href="/css/spectre.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Pastebin</title>
</head>
<body>
<section class="container grid-960 mt-10">
<header class="navbar">
<section class="navbar-section">
<a href="/" class="navbar-brand mr-10">Paste</a>
</section>
<section class="navbar-section"></section>
</header>
{{template "content" .}}
</section>
</body>
</html>
{{end}}

View File

@ -1,14 +1,17 @@
<html> {{define "content"}}
<head> <section class="container">
<title>pastebin</title> <div class="columns">
</head> <div class="column">
<form action="" method="POST">
<body> <div class="form-group">
<p>Enter your text here:</p> <label class="form-label" for="input-blob"></label>
<form method="POST" action=""> <textarea class="form-input" id="input-blob" name="blob" placeholder="Enter content here..." rows="24"></textarea>
<textarea name="blob" cols=80 rows=24></textarea> </div>
<input type="submit"> <div class="form-group">
</form> <button class="btn btn-sm btn-primary" type="submit">Paste</button>
</body> </div>
</form>
</html> </div>
</div>
</section>
{{end}}

View File

@ -1,10 +1,30 @@
<html> {{define "content"}}
<head> <pre class="code">
<title>pastebin</title> <code>{{.Blob}}</code>
</head> </pre>
<section class="container">
<body> <div class="columns">
<pre>{{.Blob}}</pre> <div class="column col-3">
</body> <div class="dropdown">
<div class="btn-group">
</html> <a class="btn btn-sm btn-primary" href="/download/{{.UUID}}">
<i class="icon icon-download"></i>
Download
</a>
<a class="btn btn-sm btn-primary dropdown-toggle" tabindex="0">
<i class="icon icon-caret"></i>
</a>
<ul class="menu">
<li class="menu-item">
<a href="#dropdowns">
<i class="icon icon-delete"></i>
Delete
</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</section>
{{end}}