mirror of
https://github.com/taigrr/pastebin
synced 2026-04-14 09:31:08 -07:00
feat: add graceful shutdown, health endpoint, and improved client API
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -17,23 +18,38 @@ const (
|
||||
|
||||
// Client is a pastebin API client.
|
||||
type Client struct {
|
||||
url string
|
||||
insecure bool
|
||||
serviceURL string
|
||||
insecure bool
|
||||
output io.Writer
|
||||
}
|
||||
|
||||
// NewClient creates a new pastebin Client.
|
||||
// When insecure is true, TLS certificate verification is skipped.
|
||||
// Output is written to stdout by default; use WithOutput to change.
|
||||
func NewClient(serviceURL string, insecure bool) *Client {
|
||||
return &Client{url: serviceURL, insecure: insecure}
|
||||
return &Client{serviceURL: serviceURL, insecure: insecure, output: os.Stdout}
|
||||
}
|
||||
|
||||
// WithOutput sets the writer where paste URLs are printed.
|
||||
// Returns the Client for chaining.
|
||||
func (client *Client) WithOutput(writer io.Writer) *Client {
|
||||
client.output = writer
|
||||
return client
|
||||
}
|
||||
|
||||
// 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 {
|
||||
// It prints the resulting paste URL to the configured output writer.
|
||||
func (client *Client) Paste(body io.Reader) error {
|
||||
transport := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: c.insecure}, //nolint:gosec // user-requested skip
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: client.insecure}, //nolint:gosec // user-requested skip
|
||||
}
|
||||
httpClient := &http.Client{
|
||||
Transport: transport,
|
||||
// Don't follow redirects; capture the URL from the response.
|
||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
},
|
||||
}
|
||||
httpClient := &http.Client{Transport: transport}
|
||||
|
||||
var builder strings.Builder
|
||||
if _, err := io.Copy(&builder, body); err != nil {
|
||||
@@ -43,16 +59,30 @@ func (c *Client) Paste(body io.Reader) error {
|
||||
formValues := url.Values{}
|
||||
formValues.Set(formFieldBlob, builder.String())
|
||||
|
||||
resp, err := httpClient.PostForm(c.url, formValues)
|
||||
resp, err := httpClient.PostForm(client.serviceURL, formValues)
|
||||
if err != nil {
|
||||
return fmt.Errorf("posting paste to %s: %w", c.url, err)
|
||||
return fmt.Errorf("posting paste to %s: %w", client.serviceURL, 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)
|
||||
switch resp.StatusCode {
|
||||
case http.StatusOK:
|
||||
// Plain text response contains the paste URL in the body.
|
||||
responseBody, readErr := io.ReadAll(resp.Body)
|
||||
if readErr != nil {
|
||||
return fmt.Errorf("reading response body: %w", readErr)
|
||||
}
|
||||
fmt.Fprint(client.output, string(responseBody))
|
||||
return nil
|
||||
case http.StatusFound, http.StatusMovedPermanently:
|
||||
// HTML response redirects to the paste URL.
|
||||
location := resp.Header.Get("Location")
|
||||
if location == "" {
|
||||
return fmt.Errorf("redirect response missing Location header")
|
||||
}
|
||||
fmt.Fprint(client.output, location)
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("unexpected response from %s: %d", client.serviceURL, resp.StatusCode)
|
||||
}
|
||||
|
||||
fmt.Print(resp.Request.URL.String())
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user