Documentation ¶
Index ¶
- Variables
- func BasicAuth(user, pass string) string
- func DetectContentType(b []byte, filename string) (mimeType string, ext string)
- func DurationToHuman(d time.Duration) (str string)
- func ExpandHome(path string) string
- func FileExists(filename string) bool
- func InIntList(haystack []int, needle int) bool
- func InStringList(haystack []string, needle string) bool
- func InStringListAll(haystack []string, needles []string) bool
- func ParseFutureTime(s string, now time.Time) (time.Time, error)
- func ParsePriority(priority string) (int, error)
- func ParseSize(s string) (int64, error)
- func PriorityString(priority int) (string, error)
- func RandomString(length int) string
- func ReadPassword(in io.Reader) ([]byte, error)
- func ShortTopicURL(s string) string
- func SplitNoEmpty(s string, sep string) []string
- func ValidRandomString(s string, length int) bool
- type CachingEmbedFS
- type ContentTypeWriter
- type FixedLimiter
- type LimitWriter
- type Limiter
- type PeakedReadCloser
- type RateLimiter
Constants ¶
This section is empty.
Variables ¶
var ErrLimitReached = errors.New("limit reached")
ErrLimitReached is the error returned by the Limiter and LimitWriter when the predefined limit has been reached
Functions ¶
func DetectContentType ¶ added in v1.12.0
DetectContentType probes the byte array b and returns mime type and file extension. The filename is only used to override certain special cases.
func DurationToHuman ¶ added in v1.3.0
DurationToHuman converts a duration to a human-readable format
func ExpandHome ¶ added in v1.8.0
ExpandHome replaces "~" with the user's home directory
func FileExists ¶
FileExists checks if a file exists, and returns true if it does
func InStringList ¶ added in v1.5.2
InStringList returns true if needle is contained in haystack
func InStringListAll ¶ added in v1.8.0
InStringListAll returns true if all needles are contained in haystack
func ParseFutureTime ¶ added in v1.6.0
ParseFutureTime parses a date/time string to a time.Time. It supports unix timestamps, durations and natural language dates
func ParsePriority ¶ added in v1.8.0
ParsePriority parses a priority string into its equivalent integer value
func ParseSize ¶ added in v1.12.0
ParseSize parses a size string like 2K or 2M into bytes. If no unit is found, e.g. 123, bytes is assumed.
func PriorityString ¶ added in v1.9.0
PriorityString converts a priority number to a string
func RandomString ¶
RandomString returns a random string with a given length
func ReadPassword ¶ added in v1.14.0
ReadPassword will read a password from STDIN. If the terminal supports it, it will not print the input characters to the screen. If not, it'll just read using normal readline semantics (useful for testing).
func ShortTopicURL ¶ added in v1.9.0
ShortTopicURL shortens the topic URL to be human-friendly, removing the http:// or https://
func SplitNoEmpty ¶ added in v1.8.0
SplitNoEmpty splits a string using strings.Split, but filters out empty strings
func ValidRandomString ¶ added in v1.16.0
ValidRandomString returns true if the given string matches the format created by RandomString
Types ¶
type CachingEmbedFS ¶ added in v1.5.0
CachingEmbedFS is a wrapper around embed.FS that allows setting a ModTime, so that the default static file server can send 304s back. It can be used like this:
var ( //go:embed docs docsStaticFs embed.FS docsStaticCached = &util.CachingEmbedFS{ModTime: time.Now(), FS: docsStaticFs} ) http.FileServer(http.FS(docsStaticCached)).ServeHTTP(w, r)
type ContentTypeWriter ¶ added in v1.12.0
type ContentTypeWriter struct {
// contains filtered or unexported fields
}
ContentTypeWriter is an implementation of http.ResponseWriter that will detect the content type and set the Content-Type and (optionally) Content-Disposition headers accordingly.
It will always set a Content-Type based on http.DetectContentType, but will never send the "text/html" content type.
func NewContentTypeWriter ¶ added in v1.12.0
func NewContentTypeWriter(w http.ResponseWriter, filename string) *ContentTypeWriter
NewContentTypeWriter creates a new ContentTypeWriter
type FixedLimiter ¶ added in v1.12.0
type FixedLimiter struct {
// contains filtered or unexported fields
}
FixedLimiter is a helper that allows adding values up to a well-defined limit. Once the limit is reached ErrLimitReached will be returned. FixedLimiter may be used by multiple goroutines.
func NewFixedLimiter ¶ added in v1.12.0
func NewFixedLimiter(limit int64) *FixedLimiter
NewFixedLimiter creates a new Limiter
func (*FixedLimiter) Allow ¶ added in v1.12.0
func (l *FixedLimiter) Allow(n int64) error
Allow adds n to the limiters internal value, but only if the limit has not been reached. If the limit was exceeded after adding n, ErrLimitReached is returned.
type LimitWriter ¶ added in v1.12.0
type LimitWriter struct {
// contains filtered or unexported fields
}
LimitWriter implements an io.Writer that will pass through all Write calls to the underlying writer w until any of the limiter's limit is reached, at which point a Write will return ErrLimitReached. Each limiter's value is increased with every write.
func NewLimitWriter ¶ added in v1.12.0
func NewLimitWriter(w io.Writer, limiters ...Limiter) *LimitWriter
NewLimitWriter creates a new LimitWriter
type Limiter ¶ added in v1.1.3
type Limiter interface { // Allow adds n to the limiters internal value, or returns ErrLimitReached if the limit has been reached Allow(n int64) error }
Limiter is an interface that implements a rate limiting mechanism, e.g. based on time or a fixed value
type PeakedReadCloser ¶ added in v1.12.0
type PeakedReadCloser struct { PeakedBytes []byte LimitReached bool // contains filtered or unexported fields }
PeakedReadCloser is a ReadCloser that allows peaking into a stream and buffering it in memory. It can be instantiated using the Peak function. After a stream has been peaked, it can still be fully read by reading the PeakedReadCloser. It first drained from the memory buffer, and then from the remaining underlying reader.
func Peak ¶ added in v1.12.0
func Peak(underlying io.ReadCloser, limit int) (*PeakedReadCloser, error)
Peak reads the underlying ReadCloser into memory up until the limit and returns a PeakedReadCloser
func (*PeakedReadCloser) Close ¶ added in v1.12.0
func (r *PeakedReadCloser) Close() error
Close closes the underlying stream
type RateLimiter ¶ added in v1.12.0
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter is a Limiter that wraps a rate.Limiter, allowing a floating time-based limit.
func NewBytesLimiter ¶ added in v1.12.0
func NewBytesLimiter(bytes int, interval time.Duration) *RateLimiter
NewBytesLimiter creates a RateLimiter that is meant to be used for a bytes-per-interval limit, e.g. 250 MB per day. And example of the underlying idea can be found here: https://go.dev/play/p/0ljgzIZQ6dJ
func NewRateLimiter ¶ added in v1.12.0
func NewRateLimiter(r rate.Limit, b int) *RateLimiter
NewRateLimiter creates a new RateLimiter
func (*RateLimiter) Allow ¶ added in v1.12.0
func (l *RateLimiter) Allow(n int64) error
Allow adds n to the limiters internal value, but only if the limit has not been reached. If the limit was exceeded after adding n, ErrLimitReached is returned.