1
0
mirror of https://github.com/taigrr/pastebin synced 2026-04-15 12:50:48 -07:00

feat: modernize codebase and complete API surface

- Update module path from prologic/pastebin to taigrr/pastebin
- Replace go.rice with stdlib embed for templates and static assets
- Replace httprouter with stdlib net/http ServeMux (Go 1.22+)
- Replace namsral/flag with cobra + fang for CLI argument parsing
- Fix inverted TLS insecure logic in client (was negating the flag)
- Close HTTP response body in client
- Use base64 URLEncoding instead of StdEncoding for paste IDs
- Add comprehensive handler tests for all endpoints
- Add client package tests
- Add utils tests for RandomString
- Remove templates.go (replaced by embed)
- Remove unused dependencies (go-metrics, stats, logger, httprouter, go.rice)
This commit is contained in:
2026-03-09 05:44:08 +00:00
parent 50e3bf0242
commit 25f66bb589
16 changed files with 708 additions and 496 deletions

View File

@@ -1,59 +1,58 @@
// Package client provides a programmatic client for the pastebin service.
package client
import (
"crypto/tls"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
)
const (
contentType = "application/x-www-form-urlencoded"
formContentType = "application/x-www-form-urlencoded"
formFieldBlob = "blob"
)
// Client ...
// Client is a pastebin API client.
type Client struct {
url string
insecure bool
}
// NewClient ...
func NewClient(url string, insecure bool) *Client {
return &Client{url: url, insecure: insecure}
// NewClient creates a new pastebin Client.
// When insecure is true, TLS certificate verification is skipped.
func NewClient(serviceURL string, insecure bool) *Client {
return &Client{url: serviceURL, insecure: insecure}
}
// Paste ...
// Paste reads from body and submits it as a new paste.
// It prints the resulting paste URL to stdout.
func (c *Client) Paste(body io.Reader) error {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: !c.insecure},
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: c.insecure}, //nolint:gosec // user-requested skip
}
client := &http.Client{Transport: tr}
httpClient := &http.Client{Transport: transport}
buf := new(strings.Builder)
_, err := io.Copy(buf, body)
// check errors
var builder strings.Builder
if _, err := io.Copy(&builder, body); err != nil {
return fmt.Errorf("reading input: %w", err)
}
formValues := url.Values{}
formValues.Set(formFieldBlob, builder.String())
resp, err := httpClient.PostForm(c.url, formValues)
if err != nil {
log.Printf("error reading in file: %s", err)
return err
return fmt.Errorf("posting paste to %s: %w", c.url, err)
}
v := url.Values{}
v.Set("blob", buf.String())
res, err := client.PostForm(c.url, v)
if err != nil {
log.Printf("error pasting to %s: %s", c.url, err)
return err
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusMovedPermanently {
return fmt.Errorf("unexpected response from %s: %d", c.url, resp.StatusCode)
}
if res.StatusCode != 200 && res.StatusCode != 301 {
log.Printf("unexpected response from %s: %d", c.url, res.StatusCode)
return errors.New("unexpected response")
}
fmt.Printf("%s", res.Request.URL.String())
fmt.Print(resp.Request.URL.String())
return nil
}

51
client/client_test.go Normal file
View File

@@ -0,0 +1,51 @@
package client
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPasteSuccess(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method)
err := r.ParseForm()
require.NoError(t, err)
blob := r.FormValue("blob")
assert.Equal(t, "test content", blob)
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
cli := NewClient(server.URL, false)
err := cli.Paste(strings.NewReader("test content"))
assert.NoError(t, err)
}
func TestPasteServerError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer server.Close()
cli := NewClient(server.URL, false)
err := cli.Paste(strings.NewReader("test content"))
assert.Error(t, err)
assert.Contains(t, err.Error(), "unexpected response")
}
func TestPasteInvalidURL(t *testing.T) {
cli := NewClient("http://invalid.invalid.invalid:99999", false)
err := cli.Paste(strings.NewReader("test content"))
assert.Error(t, err)
}
func TestNewClient(t *testing.T) {
cli := NewClient("http://example.com", true)
assert.Equal(t, "http://example.com", cli.url)
assert.True(t, cli.insecure)
}