Support content negotation for text/html and text/plain

This commit is contained in:
James Mills 2017-07-03 01:46:08 -07:00
parent 43a32970d3
commit 61b79025e8
No known key found for this signature in database
GPG Key ID: AC4C014F1440EBD6

View File

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
@ -10,9 +11,15 @@ import (
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
"github.com/patrickmn/go-cache" "github.com/patrickmn/go-cache"
"github.com/renstrom/shortuuid" "github.com/renstrom/shortuuid"
"github.com/timewasted/go-accept-headers"
"go.iondynamics.net/templice" "go.iondynamics.net/templice"
) )
var AcceptedTypes = []string{
"text/html",
"text/plain",
}
// Server ... // Server ...
type Server struct { type Server struct {
bind string bind string
@ -32,21 +39,56 @@ func (s *Server) render(w http.ResponseWriter, tmpl string, data interface{}) {
// IndexHandler ... // IndexHandler ...
func (s *Server) IndexHandler() httprouter.Handle { func (s *Server) IndexHandler() httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
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":
s.render(w, "index", nil) s.render(w, "index", nil)
case "text/plain":
default:
}
} }
} }
// PasteHandler ... // PasteHandler ...
func (s *Server) PasteHandler() httprouter.Handle { 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) {
err := r.ParseForm() 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 { if err != nil {
http.Error(w, "Internal Error", http.StatusInternalServerError) http.Error(w, "Internal Error", http.StatusInternalServerError)
return return
} }
if blob == "" {
blob = r.Form.Get("blob")
}
if blob == "" {
blob = r.URL.Query().Get("blob")
}
if blob == "" {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
uuid := shortuuid.NewWithNamespace(s.config.fqdn) uuid := shortuuid.NewWithNamespace(s.config.fqdn)
blob := r.Form.Get("blob")
s.store.Set(uuid, blob, cache.DefaultExpiration) s.store.Set(uuid, blob, cache.DefaultExpiration)
u, err := url.Parse(fmt.Sprintf("./%s", uuid)) u, err := url.Parse(fmt.Sprintf("./%s", uuid))
@ -60,6 +102,15 @@ func (s *Server) PasteHandler() httprouter.Handle {
// 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) {
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
}
uuid := p.ByName("uuid") uuid := p.ByName("uuid")
if uuid == "" { if uuid == "" {
http.Error(w, "Bad Request", http.StatusBadRequest) http.Error(w, "Bad Request", http.StatusBadRequest)
@ -72,7 +123,14 @@ func (s *Server) ViewHandler() httprouter.Handle {
return return
} }
switch accepts {
case "text/html":
s.render(w, "view", struct{ Blob string }{Blob: blob.(string)}) s.render(w, "view", struct{ Blob string }{Blob: blob.(string)})
case "text/plain":
w.Write([]byte(blob.(string)))
default:
w.Write([]byte(blob.(string)))
}
} }
} }