slimgfast: github.com/ericflo/slimgfast Index | Files | Directories

package slimgfast

import "github.com/ericflo/slimgfast"

Package slimfast is a library that allows you to create a service which will resize, serve, and cache images.

For a full guide, visit https://www.github.com/ericflo/slimgfast

Index

Package Files

app.go fetcher.go request.go sizes.go source.go transformer.go transformer_resize.go worker.go

Constants

const DEFAULT_CACHE_SIZE_MB = int64(128)
const DEFAULT_IMAGE_SOURCE_NAME = "slimgfast_image_source"
const RESIZED_IMAGE_SOURCE_NAME string = "slimgfast_resized_image_source"

type App Uses

type App struct {
    MaxWidth  int
    MaxHeight int
    // contains filtered or unexported fields
}

App ties together the fetcher, the transformers, handles the lifecycle of an image request, and does the actual HTTP serving.

func NewApp Uses

func NewApp(
    fetcher Fetcher,
    transformers []Transformer,
    counterFilename string,
    numWorkers int,
    cacheMegabytes int64,
    maxWidth int,
    maxHeight int,
) (*App, error)

NewApp returns an App that is initialized and ready to be started.

func (*App) Close Uses

func (app *App) Close()

Close signals to the worker group and size counter goroutines to exit.

func (*App) ServeHTTP Uses

func (app *App) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP is responsible for actually kicking off the image transformations and serving the image back to the user who requested it.

func (*App) Start Uses

func (app *App) Start()

Start starts the application worker group and size counter goroutines.

type Fetcher Uses

type Fetcher interface {
    Fetch(urlPath string, dest groupcache.Sink) error
}

Fetcher is the interface that is used to fetch images from some source, which could be the filesystem, a remote URL, or S3 -- but it could be from anywhere.

type ImageRequest Uses

type ImageRequest struct {
    Url    string
    Width  int
    Height int
    Fit    string
}

ImageRequest captures information about which file the user wants to have transformed and served, and what transformations the user would like to make with it.

func ImageRequestFromCacheKey Uses

func ImageRequestFromCacheKey(cacheKey string) (*ImageRequest, error)

ImageRequestFromCacheKey parses a cache key generated by CacheKey and constructs an ImageRequest out of it.

func ImageRequestFromURLString Uses

func ImageRequestFromURLString(rawUrl string) (*ImageRequest, error)

ImageRequestFromURLString parses a URL string and constructs an ImageRequest out of it.

func (*ImageRequest) CacheKey Uses

func (req *ImageRequest) CacheKey() (string, error)

CacheKey generates a cache key that encodes all of the information about this ImageRequest.

func (*ImageRequest) Size Uses

func (req *ImageRequest) Size() (*Size, error)

Size is a convenience function for getting a *Size out of the parsed width and height ImageRequest members.

type ImageSource Uses

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

ImageSource is an abstraction over a fetcher which caches intelligently, and serves as the primary internal interface to fetchers.

func NewImageSource Uses

func NewImageSource(fetcher Fetcher) *ImageSource

NewImageSource initializes and returns an *ImageSource with sane default values.

func NewImageSourceCustomCache Uses

func NewImageSourceCustomCache(fetcher Fetcher, cacheName string, cacheMegabytes int64) *ImageSource

NewImageSourceCustomCache initializes and returns an *ImageSource with a custom groupcache name and a custom cache size.

func (*ImageSource) GetImageData Uses

func (src *ImageSource) GetImageData(req *ImageRequest) ([]byte, error)

GetImageData gets the image data the request asked for, either from cache or from the associated Fetcher.

type Job Uses

type Job struct {
    ImageSource  ImageSource
    ImageRequest ImageRequest
    Result       chan []byte
    Error        chan error
}

Job is a single image resize job that can be sent over a channel to a worker.

type Size Uses

type Size struct {
    Width  uint
    Height uint
}

Size is a Width and a Height

func SizeFromKey Uses

func SizeFromKey(key string) (Size, error)

SizeFromKey takes a string of the form WIDTHxHEIGHT and parses it into a Size struct.

func (Size) Key Uses

func (size Size) Key() string

Key takes the current Size object's Width and Height and generates a string of the form WIDTHxHEIGHT

type SizeCounter Uses

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

SizeCounter keeps track of how many times a certain size was requested, and persists this information to disk on a periodic basis.

func NewSizeCounter Uses

func NewSizeCounter(filename string) (*SizeCounter, error)

NewSizeCounter initializes a *SizeCounter struct, loads in, and parses the persisted sizes.

func (*SizeCounter) Close Uses

func (counter *SizeCounter) Close()

Close stops the SizeCounter from doing any more persistence.

func (*SizeCounter) CountSize Uses

func (counter *SizeCounter) CountSize(size *Size)

CountSize notes the size of one request.

func (*SizeCounter) GetAllSizes Uses

func (counter *SizeCounter) GetAllSizes() ([]Size, error)

GetAllSizes gets a list of all the sizes that we've seen.

func (*SizeCounter) GetTopSizesByCount Uses

func (counter *SizeCounter) GetTopSizesByCount(count uint) ([]Size, error)

GetTopSizesByCount gets a list of all the sizes who have been requested at least `count` times.

func (*SizeCounter) Start Uses

func (counter *SizeCounter) Start(every time.Duration)

Start starts the counter persisting its aggregated size stats to disk periodically.

type SizeFile Uses

type SizeFile struct {
    Counts map[string]uint
}

SizeFile is a struct that is used to serialize the aggregated Size counts to a JSON file.

type Transformer Uses

type Transformer interface {
    Transform(req *ImageRequest, image image.Image) (image.Image, error)
}

Transformer is the interface that any image transformation operations must adhere to. By implementing one Transform method which takes the ImageRequest and an image object and returns a new image object, we can achieve any effect we want.

type TransformerResize Uses

type TransformerResize struct{}

TransformerResize is the primary Transformer that will resize images to the proper size.

func (*TransformerResize) Transform Uses

func (t *TransformerResize) Transform(req *ImageRequest, image image.Image) (image.Image, error)

Transform resizes the image as requested.

type WorkerGroup Uses

type WorkerGroup struct {
    Transformers []Transformer
    NumWorkers   int
    // contains filtered or unexported fields
}

WorkerGroup is a pool of workers, a channel, and a list of Transformers that this pool supports.

func (*WorkerGroup) Close Uses

func (wg *WorkerGroup) Close()

Close tells the workers to quit.

func (*WorkerGroup) Resize Uses

func (wg *WorkerGroup) Resize(imageSource *ImageSource, imageRequest *ImageRequest) ([]byte, error)

Resize enqueues one request to be run on a worker, and waits for it to respond.

func (*WorkerGroup) Start Uses

func (wg *WorkerGroup) Start()

Start spawns workers for the worker pool and starts them up.

Directories

PathSynopsis
fetchers
slimgfastd

Package slimgfast imports 19 packages (graph) and is imported by 1 packages. Updated 2016-07-28. Refresh now. Tools for package owners.