2017-07-03 06:42:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-07-06 08:33:31 +00:00
|
|
|
"encoding/json"
|
2017-07-03 06:42:34 +00:00
|
|
|
"fmt"
|
2017-07-09 08:57:21 +00:00
|
|
|
"html/template"
|
2017-07-03 08:46:08 +00:00
|
|
|
"io/ioutil"
|
2017-07-03 06:42:34 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2017-07-09 08:57:21 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2017-07-03 06:42:34 +00:00
|
|
|
|
2017-07-06 08:33:31 +00:00
|
|
|
// Logging
|
|
|
|
"github.com/unrolled/logger"
|
|
|
|
|
|
|
|
// Stats/Metrics
|
|
|
|
"github.com/rcrowley/go-metrics"
|
|
|
|
"github.com/rcrowley/go-metrics/exp"
|
|
|
|
"github.com/thoas/stats"
|
|
|
|
|
2017-07-03 06:42:34 +00:00
|
|
|
"github.com/GeertJohan/go.rice"
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
"github.com/patrickmn/go-cache"
|
|
|
|
"github.com/renstrom/shortuuid"
|
2017-07-03 08:46:08 +00:00
|
|
|
"github.com/timewasted/go-accept-headers"
|
2017-07-03 06:42:34 +00:00
|
|
|
)
|
|
|
|
|
2017-07-06 08:33:31 +00:00
|
|
|
// AcceptedTypes ...
|
2017-07-03 08:46:08 +00:00
|
|
|
var AcceptedTypes = []string{
|
|
|
|
"text/html",
|
|
|
|
"text/plain",
|
|
|
|
}
|
|
|
|
|
2017-07-06 08:33:31 +00:00
|
|
|
// Counters ...
|
|
|
|
type Counters struct {
|
|
|
|
r metrics.Registry
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCounters() *Counters {
|
|
|
|
counters := &Counters{
|
|
|
|
r: metrics.NewRegistry(),
|
|
|
|
}
|
|
|
|
return counters
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Counters) Inc(name string) {
|
|
|
|
metrics.GetOrRegisterCounter(name, c.r).Inc(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Counters) Dec(name string) {
|
|
|
|
metrics.GetOrRegisterCounter(name, c.r).Dec(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Counters) IncBy(name string, n int64) {
|
|
|
|
metrics.GetOrRegisterCounter(name, c.r).Inc(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Counters) DecBy(name string, n int64) {
|
|
|
|
metrics.GetOrRegisterCounter(name, c.r).Dec(n)
|
|
|
|
}
|
|
|
|
|
2017-07-03 06:42:34 +00:00
|
|
|
// Server ...
|
|
|
|
type Server struct {
|
|
|
|
bind string
|
|
|
|
config Config
|
|
|
|
store *cache.Cache
|
2017-07-09 08:57:21 +00:00
|
|
|
templates *Templates
|
2017-07-03 06:42:34 +00:00
|
|
|
router *httprouter.Router
|
2017-07-06 08:33:31 +00:00
|
|
|
|
|
|
|
// Logger
|
|
|
|
logger *logger.Logger
|
|
|
|
|
|
|
|
// Stats/Metrics
|
|
|
|
counters *Counters
|
|
|
|
stats *stats.Stats
|
2017-07-03 06:42:34 +00:00
|
|
|
}
|
|
|
|
|
2017-07-09 08:57:21 +00:00
|
|
|
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)
|
2017-07-03 06:42:34 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IndexHandler ...
|
|
|
|
func (s *Server) IndexHandler() httprouter.Handle {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
2017-07-06 08:33:31 +00:00
|
|
|
s.counters.Inc("n_index")
|
|
|
|
|
2017-07-03 08:46:08 +00:00
|
|
|
accepts, err := accept.Negotiate(
|
|
|
|
r.Header.Get("Accept"), AcceptedTypes...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error negotiating: %s", err)
|
|
|
|
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch accepts {
|
|
|
|
case "text/html":
|
2017-07-09 08:57:21 +00:00
|
|
|
s.render("index", w, nil)
|
2017-07-03 08:46:08 +00:00
|
|
|
case "text/plain":
|
|
|
|
default:
|
|
|
|
}
|
2017-07-03 06:42:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PasteHandler ...
|
|
|
|
func (s *Server) PasteHandler() httprouter.Handle {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
2017-07-06 08:33:31 +00:00
|
|
|
s.counters.Inc("n_paste")
|
|
|
|
|
2018-05-17 21:51:39 +00:00
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
log.Printf("body: %v", body)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
|
|
|
return
|
2017-07-03 08:46:08 +00:00
|
|
|
}
|
|
|
|
|
2018-05-17 21:51:39 +00:00
|
|
|
if len(body) == 0 {
|
2017-07-03 08:46:08 +00:00
|
|
|
http.Error(w, "Bad Request", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-03 06:42:34 +00:00
|
|
|
uuid := shortuuid.NewWithNamespace(s.config.fqdn)
|
2018-05-17 21:51:39 +00:00
|
|
|
s.store.Set(uuid, string(body), cache.DefaultExpiration)
|
2017-07-03 06:42:34 +00:00
|
|
|
|
2017-07-09 18:25:50 +00:00
|
|
|
u, err := url.Parse(fmt.Sprintf("./p/%s", uuid))
|
2017-07-03 06:42:34 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, r.URL.ResolveReference(u).String(), http.StatusFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-09 08:57:21 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-03 06:42:34 +00:00
|
|
|
// ViewHandler ...
|
|
|
|
func (s *Server) ViewHandler() httprouter.Handle {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
2017-07-06 08:33:31 +00:00
|
|
|
s.counters.Inc("n_view")
|
|
|
|
|
2017-07-03 08:46:08 +00:00
|
|
|
accepts, err := accept.Negotiate(
|
|
|
|
r.Header.Get("Accept"), AcceptedTypes...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error negotiating: %s", err)
|
|
|
|
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-03 06:42:34 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-07-03 08:46:08 +00:00
|
|
|
switch accepts {
|
|
|
|
case "text/html":
|
2017-07-09 08:57:21 +00:00
|
|
|
s.render(
|
|
|
|
"view", w,
|
|
|
|
struct {
|
|
|
|
Blob string
|
|
|
|
UUID string
|
|
|
|
}{
|
|
|
|
Blob: blob.(string),
|
|
|
|
UUID: uuid,
|
|
|
|
},
|
|
|
|
)
|
2017-07-03 08:46:08 +00:00
|
|
|
case "text/plain":
|
|
|
|
w.Write([]byte(blob.(string)))
|
|
|
|
default:
|
|
|
|
w.Write([]byte(blob.(string)))
|
|
|
|
}
|
2017-07-03 06:42:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 08:33:31 +00:00
|
|
|
// StatsHandler ...
|
|
|
|
func (s *Server) StatsHandler() httprouter.Handle {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
bs, err := json.Marshal(s.stats.Data())
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
w.Write(bs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-03 06:42:34 +00:00
|
|
|
// ListenAndServe ...
|
|
|
|
func (s *Server) ListenAndServe() {
|
2017-07-06 08:33:31 +00:00
|
|
|
log.Fatal(
|
|
|
|
http.ListenAndServe(
|
|
|
|
s.bind,
|
|
|
|
s.logger.Handler(
|
|
|
|
s.stats.Handler(s.router),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
2017-07-03 06:42:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) initRoutes() {
|
2017-07-06 08:33:31 +00:00
|
|
|
s.router.Handler("GET", "/debug/metrics", exp.ExpHandler(s.counters.r))
|
|
|
|
s.router.GET("/debug/stats", s.StatsHandler())
|
|
|
|
|
2017-07-09 08:57:21 +00:00
|
|
|
s.router.ServeFiles(
|
|
|
|
"/css/*filepath",
|
|
|
|
rice.MustFindBox("static/css").HTTPBox(),
|
|
|
|
)
|
|
|
|
|
2017-07-03 06:42:34 +00:00
|
|
|
s.router.GET("/", s.IndexHandler())
|
|
|
|
s.router.POST("/", s.PasteHandler())
|
2017-07-09 08:57:21 +00:00
|
|
|
s.router.GET("/download/:uuid", s.DownloadHandler())
|
2017-07-09 18:25:50 +00:00
|
|
|
s.router.GET("/p/:uuid", s.ViewHandler())
|
2017-07-03 06:42:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer ...
|
|
|
|
func NewServer(bind string, config Config) *Server {
|
|
|
|
server := &Server{
|
|
|
|
bind: bind,
|
|
|
|
config: config,
|
|
|
|
router: httprouter.New(),
|
2017-07-03 06:57:32 +00:00
|
|
|
store: cache.New(cfg.expiry, cfg.expiry*2),
|
2017-07-09 08:57:21 +00:00
|
|
|
templates: NewTemplates("base"),
|
2017-07-06 08:33:31 +00:00
|
|
|
|
|
|
|
// Logger
|
|
|
|
logger: logger.New(logger.Options{
|
|
|
|
Prefix: "pastebin",
|
|
|
|
RemoteAddressHeaders: []string{"X-Forwarded-For"},
|
|
|
|
OutputFlags: log.LstdFlags,
|
|
|
|
}),
|
|
|
|
|
|
|
|
// Stats/Metrics
|
|
|
|
counters: NewCounters(),
|
|
|
|
stats: stats.New(),
|
2017-07-03 06:42:34 +00:00
|
|
|
}
|
|
|
|
|
2017-07-09 08:57:21 +00:00
|
|
|
// Templates
|
|
|
|
box := rice.MustFindBox("templates")
|
|
|
|
|
2017-07-09 17:09:10 +00:00
|
|
|
indexTemplate := template.New("index")
|
2017-07-09 08:57:21 +00:00
|
|
|
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)
|
2017-07-03 06:42:34 +00:00
|
|
|
|
|
|
|
server.initRoutes()
|
|
|
|
|
|
|
|
return server
|
|
|
|
}
|