Refacted, new look and raw download feature
This commit is contained in:
parent
47ef82966c
commit
3e081f0aa0
@ -14,4 +14,6 @@ WORKDIR /go/src/pastebin
|
||||
COPY . /go/src/pastebin
|
||||
|
||||
RUN go get -v -d
|
||||
RUN go get github.com/GeertJohan/go.rice/rice
|
||||
RUN rice embed-go
|
||||
RUN go install -v
|
||||
|
102
server.go
102
server.go
@ -3,10 +3,13 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
// Logging
|
||||
"github.com/unrolled/logger"
|
||||
@ -21,7 +24,6 @@ import (
|
||||
"github.com/patrickmn/go-cache"
|
||||
"github.com/renstrom/shortuuid"
|
||||
"github.com/timewasted/go-accept-headers"
|
||||
"go.iondynamics.net/templice"
|
||||
)
|
||||
|
||||
// AcceptedTypes ...
|
||||
@ -63,7 +65,7 @@ type Server struct {
|
||||
bind string
|
||||
config Config
|
||||
store *cache.Cache
|
||||
templates *templice.Template
|
||||
templates *Templates
|
||||
router *httprouter.Router
|
||||
|
||||
// Logger
|
||||
@ -74,8 +76,13 @@ type Server struct {
|
||||
stats *stats.Stats
|
||||
}
|
||||
|
||||
func (s *Server) render(w http.ResponseWriter, tmpl string, data interface{}) {
|
||||
err := s.templates.ExecuteTemplate(w, tmpl+".html", data)
|
||||
func (s *Server) render(name string, w http.ResponseWriter, ctx interface{}) {
|
||||
buf, err := s.templates.Exec(name, ctx)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
_, err = buf.WriteTo(w)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
@ -97,7 +104,7 @@ func (s *Server) IndexHandler() httprouter.Handle {
|
||||
|
||||
switch accepts {
|
||||
case "text/html":
|
||||
s.render(w, "index", nil)
|
||||
s.render("index", w, nil)
|
||||
case "text/plain":
|
||||
default:
|
||||
}
|
||||
@ -109,23 +116,15 @@ func (s *Server) PasteHandler() httprouter.Handle {
|
||||
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
s.counters.Inc("n_paste")
|
||||
|
||||
var blob string
|
||||
|
||||
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
|
||||
}
|
||||
blob := r.FormValue("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 == "" {
|
||||
@ -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 ...
|
||||
func (s *Server) ViewHandler() httprouter.Handle {
|
||||
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
@ -176,7 +202,16 @@ func (s *Server) ViewHandler() httprouter.Handle {
|
||||
|
||||
switch accepts {
|
||||
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":
|
||||
w.Write([]byte(blob.(string)))
|
||||
default:
|
||||
@ -213,8 +248,14 @@ func (s *Server) initRoutes() {
|
||||
s.router.Handler("GET", "/debug/metrics", exp.ExpHandler(s.counters.r))
|
||||
s.router.GET("/debug/stats", s.StatsHandler())
|
||||
|
||||
s.router.ServeFiles(
|
||||
"/css/*filepath",
|
||||
rice.MustFindBox("static/css").HTTPBox(),
|
||||
)
|
||||
|
||||
s.router.GET("/", s.IndexHandler())
|
||||
s.router.POST("/", s.PasteHandler())
|
||||
s.router.GET("/download/:uuid", s.DownloadHandler())
|
||||
s.router.GET("/view/:uuid", s.ViewHandler())
|
||||
}
|
||||
|
||||
@ -225,7 +266,7 @@ func NewServer(bind string, config Config) *Server {
|
||||
config: config,
|
||||
router: httprouter.New(),
|
||||
store: cache.New(cfg.expiry, cfg.expiry*2),
|
||||
templates: templice.New(rice.MustFindBox("templates")),
|
||||
templates: NewTemplates("base"),
|
||||
|
||||
// Logger
|
||||
logger: logger.New(logger.Options{
|
||||
@ -239,10 +280,19 @@ func NewServer(bind string, config Config) *Server {
|
||||
stats: stats.New(),
|
||||
}
|
||||
|
||||
err := server.templates.Load()
|
||||
if err != nil {
|
||||
log.Panicf("error loading templates: %s", err)
|
||||
}
|
||||
// Templates
|
||||
box := rice.MustFindBox("templates")
|
||||
|
||||
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()
|
||||
|
||||
|
1
static/css/spectre-icons.min.css
vendored
Normal file
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
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
53
templates.go
Normal 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
22
templates/base.html
Normal 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}}
|
@ -1,14 +1,17 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>pastebin</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<p>Enter your text here:</p>
|
||||
<form method="POST" action="">
|
||||
<textarea name="blob" cols=80 rows=24></textarea>
|
||||
<input type="submit">
|
||||
</form>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
{{define "content"}}
|
||||
<section class="container">
|
||||
<div class="columns">
|
||||
<div class="column">
|
||||
<form action="" method="POST">
|
||||
<div class="form-group">
|
||||
<label class="form-label" for="input-blob"></label>
|
||||
<textarea class="form-input" id="input-blob" name="blob" placeholder="Enter content here..." rows="24"></textarea>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<button class="btn btn-sm btn-primary" type="submit">Paste</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
{{end}}
|
||||
|
@ -1,10 +1,30 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>pastebin</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<pre>{{.Blob}}</pre>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
{{define "content"}}
|
||||
<pre class="code">
|
||||
<code>{{.Blob}}</code>
|
||||
</pre>
|
||||
<section class="container">
|
||||
<div class="columns">
|
||||
<div class="column col-3">
|
||||
<div class="dropdown">
|
||||
<div class="btn-group">
|
||||
<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}}
|
||||
|
Loading…
Reference in New Issue
Block a user