Fix pb utility (#16)
* Update dependencies and go compile target version * Adds deletion support to the server * Add form around delete button * Add support for returning the url in curl * remove -config option for now * Upload as form value instead of file * remove dependencies from go mod * update to README re: new curl usage
This commit is contained in:
		@@ -37,7 +37,7 @@ Or use the Web UI: http://localhost:8000/
 | 
				
			|||||||
Or curl:
 | 
					Or curl:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```#bash
 | 
					```#bash
 | 
				
			||||||
$ echo "hello World" | curl -q -L -d @- -o - http://localhost:8000/
 | 
					$ echo "Hello World" | curl -q -L --form blob='<-' -o - http://localhost:8000/
 | 
				
			||||||
...
 | 
					...
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,6 +7,8 @@ import (
 | 
				
			|||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
 | 
						"net/url"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
@@ -31,13 +33,22 @@ func (c *Client) Paste(body io.Reader) error {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	client := &http.Client{Transport: tr}
 | 
						client := &http.Client{Transport: tr}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	res, err := client.Post(c.url, contentType, body)
 | 
						buf := new(strings.Builder)
 | 
				
			||||||
 | 
						_, err := io.Copy(buf, body)
 | 
				
			||||||
 | 
						// check errors
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Printf("error reading in file: %s", err)
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						v := url.Values{}
 | 
				
			||||||
 | 
						v.Set("blob", buf.String())
 | 
				
			||||||
 | 
						res, err := client.PostForm(c.url, v)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Printf("error pasting to %s: %s", c.url, err)
 | 
							log.Printf("error pasting to %s: %s", c.url, err)
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if res.StatusCode != 200 {
 | 
						if res.StatusCode != 200 && res.StatusCode != 301 {
 | 
				
			||||||
		log.Printf("unexpected response from %s: %d", c.url, res.StatusCode)
 | 
							log.Printf("unexpected response from %s: %d", c.url, res.StatusCode)
 | 
				
			||||||
		return errors.New("unexpected response")
 | 
							return errors.New("unexpected response")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,7 +6,6 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	"github.com/prologic/pastebin/client"
 | 
						"github.com/prologic/pastebin/client"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/mitchellh/go-homedir"
 | 
					 | 
				
			||||||
	"github.com/namsral/flag"
 | 
						"github.com/namsral/flag"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -16,22 +15,12 @@ const (
 | 
				
			|||||||
	defaultURL        = "http://localhost:8000"
 | 
						defaultURL        = "http://localhost:8000"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func getDefaultConfig() string {
 | 
					 | 
				
			||||||
	path, err := homedir.Expand(defaultUserConfig)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return defaultConfig
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return path
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
	var (
 | 
						var (
 | 
				
			||||||
		config   string
 | 
					 | 
				
			||||||
		url      string
 | 
							url      string
 | 
				
			||||||
		insecure bool
 | 
							insecure bool
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	flag.StringVar(&config, "config", getDefaultConfig(), "path to config")
 | 
					 | 
				
			||||||
	flag.StringVar(&url, "url", defaultURL, "pastebin service url")
 | 
						flag.StringVar(&url, "url", defaultURL, "pastebin service url")
 | 
				
			||||||
	flag.BoolVar(&insecure, "insecure", false, "insecure (skip ssl verify)")
 | 
						flag.BoolVar(&insecure, "insecure", false, "insecure (skip ssl verify)")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										1
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								go.mod
									
									
									
									
									
								
							@@ -5,7 +5,6 @@ go 1.16
 | 
				
			|||||||
require (
 | 
					require (
 | 
				
			||||||
	github.com/GeertJohan/go.rice v1.0.2
 | 
						github.com/GeertJohan/go.rice v1.0.2
 | 
				
			||||||
	github.com/julienschmidt/httprouter v1.3.0
 | 
						github.com/julienschmidt/httprouter v1.3.0
 | 
				
			||||||
	github.com/mitchellh/go-homedir v1.1.0
 | 
					 | 
				
			||||||
	github.com/namsral/flag v1.7.4-pre
 | 
						github.com/namsral/flag v1.7.4-pre
 | 
				
			||||||
	github.com/patrickmn/go-cache v2.1.0+incompatible
 | 
						github.com/patrickmn/go-cache v2.1.0+incompatible
 | 
				
			||||||
	github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475
 | 
						github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										2
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.sum
									
									
									
									
									
								
							@@ -10,8 +10,6 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
 | 
				
			|||||||
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
 | 
					github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
 | 
				
			||||||
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
 | 
					github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
 | 
				
			||||||
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
 | 
					github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
 | 
				
			||||||
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
 | 
					 | 
				
			||||||
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
 | 
					 | 
				
			||||||
github.com/namsral/flag v1.7.4-pre h1:b2ScHhoCUkbsq0d2C15Mv+VU8bl8hAXV8arnWiOHNZs=
 | 
					github.com/namsral/flag v1.7.4-pre h1:b2ScHhoCUkbsq0d2C15Mv+VU8bl8hAXV8arnWiOHNZs=
 | 
				
			||||||
github.com/namsral/flag v1.7.4-pre/go.mod h1:OXldTctbM6SWH1K899kPZcf65KxJiD7MsceFUpB5yDo=
 | 
					github.com/namsral/flag v1.7.4-pre/go.mod h1:OXldTctbM6SWH1K899kPZcf65KxJiD7MsceFUpB5yDo=
 | 
				
			||||||
github.com/nkovacs/streamquote v1.0.0/go.mod h1:BN+NaZ2CmdKqUuTUXUEm9j95B2TRbpOWpxbJYzzgUsc=
 | 
					github.com/nkovacs/streamquote v1.0.0/go.mod h1:BN+NaZ2CmdKqUuTUXUEm9j95B2TRbpOWpxbJYzzgUsc=
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										54
									
								
								server.go
									
									
									
									
									
								
							
							
						
						
									
										54
									
								
								server.go
									
									
									
									
									
								
							@@ -115,6 +115,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")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							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
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		blob := r.FormValue("blob")
 | 
							blob := r.FormValue("blob")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if len(blob) == 0 {
 | 
							if len(blob) == 0 {
 | 
				
			||||||
@@ -129,7 +138,14 @@ func (s *Server) PasteHandler() httprouter.Handle {
 | 
				
			|||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			http.Error(w, "Internal Error", http.StatusInternalServerError)
 | 
								http.Error(w, "Internal Error", http.StatusInternalServerError)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		http.Redirect(w, r, r.URL.ResolveReference(u).String(), http.StatusFound)
 | 
							switch accepts {
 | 
				
			||||||
 | 
							case "text/html":
 | 
				
			||||||
 | 
								http.Redirect(w, r, r.URL.ResolveReference(u).String(), http.StatusFound)
 | 
				
			||||||
 | 
							case "text/plain":
 | 
				
			||||||
 | 
								fallthrough
 | 
				
			||||||
 | 
							default:
 | 
				
			||||||
 | 
								w.Write([]byte(r.Host + r.URL.ResolveReference(u).String()))
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -160,6 +176,35 @@ func (s *Server) DownloadHandler() httprouter.Handle {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// DeleteHandler
 | 
				
			||||||
 | 
					func (s *Server) DeleteHandler() httprouter.Handle {
 | 
				
			||||||
 | 
						return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
 | 
				
			||||||
 | 
							s.counters.Inc("n_delete")
 | 
				
			||||||
 | 
							_, 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")
 | 
				
			||||||
 | 
							if uuid == "" {
 | 
				
			||||||
 | 
								http.Error(w, "Bad Request", http.StatusBadRequest)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							_, ok := s.store.Get(uuid)
 | 
				
			||||||
 | 
							if !ok {
 | 
				
			||||||
 | 
								http.Error(w, "Not Found", http.StatusNotFound)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							s.store.Delete(uuid)
 | 
				
			||||||
 | 
							http.Error(w, "Deleted", http.StatusOK)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 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) {
 | 
				
			||||||
@@ -202,7 +247,7 @@ func (s *Server) ViewHandler() httprouter.Handle {
 | 
				
			|||||||
				},
 | 
									},
 | 
				
			||||||
			)
 | 
								)
 | 
				
			||||||
		case "text/plain":
 | 
							case "text/plain":
 | 
				
			||||||
			w.Write([]byte(blob))
 | 
								fallthrough
 | 
				
			||||||
		default:
 | 
							default:
 | 
				
			||||||
			w.Write([]byte(blob))
 | 
								w.Write([]byte(blob))
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
@@ -246,6 +291,11 @@ func (s *Server) initRoutes() {
 | 
				
			|||||||
	s.router.POST("/", s.PasteHandler())
 | 
						s.router.POST("/", s.PasteHandler())
 | 
				
			||||||
	s.router.GET("/download/:uuid", s.DownloadHandler())
 | 
						s.router.GET("/download/:uuid", s.DownloadHandler())
 | 
				
			||||||
	s.router.GET("/p/:uuid", s.ViewHandler())
 | 
						s.router.GET("/p/:uuid", s.ViewHandler())
 | 
				
			||||||
 | 
						// Enable DELETE from curl/wget/cli
 | 
				
			||||||
 | 
						s.router.DELETE("/p/:uuid", s.DeleteHandler())
 | 
				
			||||||
 | 
						// Add alternate path since form actions don't support method=DELETE
 | 
				
			||||||
 | 
						// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE
 | 
				
			||||||
 | 
						s.router.POST("/p/:uuid/delete", s.DeleteHandler())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// NewServer ...
 | 
					// NewServer ...
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -16,10 +16,14 @@
 | 
				
			|||||||
          </a>
 | 
					          </a>
 | 
				
			||||||
          <ul class="menu">
 | 
					          <ul class="menu">
 | 
				
			||||||
            <li class="menu-item">
 | 
					            <li class="menu-item">
 | 
				
			||||||
              <a href="#dropdowns">
 | 
						      <form action="/p/{{.UUID}}/delete" method="POST">
 | 
				
			||||||
                <i class="icon icon-delete"></i>
 | 
					                <button type="submit">
 | 
				
			||||||
                Delete
 | 
					                  <span href="#dropdowns">
 | 
				
			||||||
              </a>
 | 
					                    <i class="icon icon-delete"></i>
 | 
				
			||||||
 | 
					                    Delete
 | 
				
			||||||
 | 
					                  </span>
 | 
				
			||||||
 | 
					                </button>
 | 
				
			||||||
 | 
					              </form>
 | 
				
			||||||
            </li>
 | 
					            </li>
 | 
				
			||||||
          </ul>
 | 
					          </ul>
 | 
				
			||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user