response

package
v0.0.0-...-89e9927 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 10, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package response provides utilities for handling HTTP responses

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Attachment

func Attachment(w http.ResponseWriter, r *http.Request, filePath, filename string) error

Attachment sends a file as attachment with proper headers

func BadRequest

func BadRequest(w http.ResponseWriter)

BadRequest sends a 400 Bad Request response

func Data

func Data(w http.ResponseWriter, code int, contentType string, data []byte) error

Data sends raw data response

func Download

func Download(w http.ResponseWriter, r *http.Request, filePath, filename string)

Download sends a file as attachment

func Error

func Error(w http.ResponseWriter, code int, message string) error

Error sends an error response

func File

func File(w http.ResponseWriter, r *http.Request, filePath string)

File sends a file response

func GuessContentType

func GuessContentType(filename string) string

GuessContentType guesses content type from file extension

func HTML

func HTML(w http.ResponseWriter, code int, html string) error

HTML sends an HTML response

func IsValidStatusCode

func IsValidStatusCode(code int) bool

IsValidStatusCode checks if a status code is valid

func JSON

func JSON(w http.ResponseWriter, code int, obj interface{}) error

JSON sends a JSON response

func JSONP

func JSONP(w http.ResponseWriter, code int, callback string, obj interface{}) error

JSONP sends a JSONP response

func JSONPretty

func JSONPretty(w http.ResponseWriter, code int, obj interface{}) error

JSONPretty sends a pretty-formatted JSON response

func NoContent

func NoContent(w http.ResponseWriter) error

NoContent sends a 204 No Content response

func Redirect

func Redirect(w http.ResponseWriter, r *http.Request, code int, url string) error

Redirect sends a redirect response

func RenderTemplate

func RenderTemplate(w http.ResponseWriter, code int, name string, data interface{}) error

RenderTemplate renders a template using the default renderer

func SetCORSHeaders

func SetCORSHeaders(w http.ResponseWriter, origin string)

SetCORSHeaders sets CORS headers

func SetCacheHeaders

func SetCacheHeaders(w http.ResponseWriter, maxAge int)

SetCacheHeaders sets cache control headers

func SetContentLength

func SetContentLength(w http.ResponseWriter, length int64)

SetContentLength sets the Content-Length header

func SetDefaultTemplateDir

func SetDefaultTemplateDir(dir string) error

SetDefaultTemplateDir sets the default template directory

func SetDownloadHeaders

func SetDownloadHeaders(w http.ResponseWriter, filename string)

SetDownloadHeaders sets headers for file downloads

func SetInlineHeaders

func SetInlineHeaders(w http.ResponseWriter, filename string)

SetInlineHeaders sets headers for inline file display

func SetNoCacheHeaders

func SetNoCacheHeaders(w http.ResponseWriter)

SetNoCacheHeaders sets no-cache headers

func SetSecurityHeaders

func SetSecurityHeaders(w http.ResponseWriter)

SetSecurityHeaders sets common security headers

func StatusText

func StatusText(code int) string

StatusText returns the status text for a given status code

func Stream

func Stream(w http.ResponseWriter, code int, contentType string, reader io.Reader) error

Stream sends a streaming response

func String

func String(w http.ResponseWriter, code int, format string, values ...interface{}) error

String sends a plain text response

func Success

func Success(w http.ResponseWriter, code int, data interface{}) error

Success sends a success response

func Template

func Template(w http.ResponseWriter, code int, renderer *TemplateRenderer, name string, data interface{}) error

Template renders an HTML template

func XML

func XML(w http.ResponseWriter, code int, obj interface{}) error

XML sends an XML response

func YAML

func YAML(w http.ResponseWriter, code int, obj interface{}) error

YAML sends a YAML response

Types

type CORSMiddleware

type CORSMiddleware struct {
	AllowedOrigins []string
	AllowedMethods []string
	AllowedHeaders []string
	MaxAge         int
}

CORSMiddleware handles Cross-Origin Resource Sharing

func NewCORSMiddleware

func NewCORSMiddleware() *CORSMiddleware

NewCORSMiddleware creates a new CORS middleware

func (*CORSMiddleware) SetAllowedHeaders

func (cm *CORSMiddleware) SetAllowedHeaders(headers ...string)

SetAllowedHeaders sets allowed headers

func (*CORSMiddleware) SetAllowedMethods

func (cm *CORSMiddleware) SetAllowedMethods(methods ...string)

SetAllowedMethods sets allowed methods

func (*CORSMiddleware) SetAllowedOrigins

func (cm *CORSMiddleware) SetAllowedOrigins(origins ...string)

SetAllowedOrigins sets allowed origins

func (*CORSMiddleware) Wrap

Wrap adds CORS headers to the response

type CompressionMiddleware

type CompressionMiddleware struct {
	// contains filtered or unexported fields
}

CompressionMiddleware provides gzip compression for responses

func NewCompressionMiddleware

func NewCompressionMiddleware(level int) *CompressionMiddleware

NewCompressionMiddleware creates a new compression middleware

func (*CompressionMiddleware) ShouldCompress

func (cm *CompressionMiddleware) ShouldCompress(r *http.Request, contentType string) bool

ShouldCompress determines if the response should be compressed

func (*CompressionMiddleware) Wrap

Wrap wraps an http.ResponseWriter with gzip compression

type SecurityMiddleware

type SecurityMiddleware struct {
	CSPPolicy string
	HSTS      bool
}

SecurityMiddleware adds security headers to responses

func NewSecurityMiddleware

func NewSecurityMiddleware() *SecurityMiddleware

NewSecurityMiddleware creates a new security middleware

func (*SecurityMiddleware) SetCSP

func (sm *SecurityMiddleware) SetCSP(policy string)

SetCSP sets the Content Security Policy

func (*SecurityMiddleware) SetHSTS

func (sm *SecurityMiddleware) SetHSTS(enabled bool)

SetHSTS enables or disables HSTS

func (*SecurityMiddleware) Wrap

Wrap adds security headers to the response

type TemplateRenderer

type TemplateRenderer struct {
	TemplateDir string
	Templates   map[string]*template.Template
	FuncMap     template.FuncMap
	Layout      string
}

TemplateRenderer handles HTML template rendering

var DefaultTemplateRenderer *TemplateRenderer

DefaultTemplateRenderer is a default instance

func NewTemplateRenderer

func NewTemplateRenderer(templateDir string) *TemplateRenderer

NewTemplateRenderer creates a new template renderer

func (*TemplateRenderer) AddFunc

func (tr *TemplateRenderer) AddFunc(name string, fn interface{})

AddFunc adds a template function

func (*TemplateRenderer) LoadTemplates

func (tr *TemplateRenderer) LoadTemplates() error

LoadTemplates loads all templates from the template directory

func (*TemplateRenderer) Render

func (tr *TemplateRenderer) Render(w io.Writer, name string, data interface{}) error

Render renders a template with data

func (*TemplateRenderer) RenderHTTP

func (tr *TemplateRenderer) RenderHTTP(w http.ResponseWriter, code int, name string, data interface{}) error

RenderHTTP renders a template as HTTP response

func (*TemplateRenderer) SetLayout

func (tr *TemplateRenderer) SetLayout(layout string)

SetLayout sets the layout template

type Writer

type Writer struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

Writer wraps http.ResponseWriter with additional functionality

func NewWriter

func NewWriter(w http.ResponseWriter) *Writer

NewWriter creates a new Response wrapper

func (*Writer) AddHeader

func (w *Writer) AddHeader(key, value string)

AddHeader adds a response header

func (*Writer) CloseNotify

func (w *Writer) CloseNotify() <-chan bool

CloseNotify implements http.CloseNotifier interface

func (*Writer) DeleteHeader

func (w *Writer) DeleteHeader(key string)

DeleteHeader deletes a response header

func (*Writer) Flush

func (w *Writer) Flush()

Flush implements http.Flusher interface

func (*Writer) Hijack

func (w *Writer) Hijack() (net.Conn, *bufio.ReadWriter, error)

Hijack implements http.Hijacker interface

func (*Writer) Push

func (w *Writer) Push(target string, opts *http.PushOptions) error

Push implements http.Pusher interface for HTTP/2 server push

func (*Writer) SetCORS

func (w *Writer) SetCORS(origin string)

SetCORS sets CORS headers

func (*Writer) SetCacheControl

func (w *Writer) SetCacheControl(maxAge int)

SetCacheControl sets cache control headers

func (*Writer) SetContentType

func (w *Writer) SetContentType(contentType string)

SetContentType sets the Content-Type header

func (*Writer) SetCookie

func (w *Writer) SetCookie(cookie *http.Cookie)

SetCookie sets a cookie

func (*Writer) SetHeader

func (w *Writer) SetHeader(key, value string)

SetHeader sets a response header

func (*Writer) SetNoCache

func (w *Writer) SetNoCache()

SetNoCache sets no-cache headers

func (*Writer) SetSecurity

func (w *Writer) SetSecurity()

SetSecurity sets security headers

func (*Writer) Size

func (w *Writer) Size() int

Size returns the response size in bytes

func (*Writer) Status

func (w *Writer) Status() int

Status returns the HTTP status code

func (*Writer) Write

func (w *Writer) Write(data []byte) (int, error)

Write captures the response size and sets written flag

func (*Writer) WriteHeader

func (w *Writer) WriteHeader(code int)

WriteHeader captures the status code

func (*Writer) Written

func (w *Writer) Written() bool

Written returns whether the response has been written

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL