1
0
mirror of https://github.com/taigrr/pastebin synced 2026-04-08 14:41:24 -07:00
Files
pastebin/client/client.go
Tai Groot 25f66bb589 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)
2026-03-09 05:44:08 +00:00

59 lines
1.5 KiB
Go

// Package client provides a programmatic client for the pastebin service.
package client
import (
"crypto/tls"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
const (
formContentType = "application/x-www-form-urlencoded"
formFieldBlob = "blob"
)
// Client is a pastebin API client.
type Client struct {
url string
insecure bool
}
// 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 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 {
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: c.insecure}, //nolint:gosec // user-requested skip
}
httpClient := &http.Client{Transport: transport}
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 {
return fmt.Errorf("posting paste to %s: %w", c.url, 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)
}
fmt.Print(resp.Request.URL.String())
return nil
}