server

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2016 License: MIT Imports: 43 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrImageNotFound   = errors.New("image not found")
	ErrInvalidBucketID = errors.New("invalid bucket id - must be: [a-z0-9_:-] max-length: 40")

	BucketIDInvalidator = regexp.MustCompile(`(i?)[^a-z0-9\/_\-:\.]`)
)
View Source
var (
	ErrNoConfigFile = errors.New("no configuration file specified")

	DefaultConfig = Config{}
)
View Source
var (
	DefaultFetcherThroughput     = 100
	DefaultFetcherReqNumAttempts = 2
	// DefaultFetcherReqTimeout = 60 * time.Second
	DefaultUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10; rv:33.0) Gecko/20100101 Firefox/33.0"
)
View Source
var (
	MimeTypes = map[string]string{
		"png":  "image/png",
		"jpeg": "image/jpeg",
		"jpg":  "image/jpeg",
		"bmp":  "image/bmp",
		"bm":   "image/bmp",
		"gif":  "image/gif",
		"ico":  "image/x-icon",
	}

	ErrInvalidURL = errors.New("invalid url")
)
View Source
var (
	EmptyImageKey = sha1Hash("")

	ErrInvalidImageKey = errors.New("invalid image key")
)
View Source
var (
	ErrDBGetKey = errors.New("DB: Unable to get the key")
)

Functions

func AirbrakeRecoverer

func AirbrakeRecoverer(apiKey string) func(http.Handler) http.Handler

Airbrake recoverer middleware to capture and report any panics to airbrake.io.

func BucketAddItems

func BucketAddItems(w http.ResponseWriter, r *http.Request)

func BucketDeleteItem

func BucketDeleteItem(w http.ResponseWriter, r *http.Request)

func BucketFetchItem

func BucketFetchItem(w http.ResponseWriter, r *http.Request)

func BucketGetIndex

func BucketGetIndex(w http.ResponseWriter, r *http.Request)

func BucketGetItem

func BucketGetItem(w http.ResponseWriter, r *http.Request)

func BucketImageUpload

func BucketImageUpload(w http.ResponseWriter, r *http.Request)

Image upload to an s3 bucket, respond with a direct url to the uploaded image. Avoid using respond.ApiError() here to prevent any of the responses from being cached.

func GetImageInfo

func GetImageInfo(w http.ResponseWriter, r *http.Request)

TODO: this can be optimized significantly.......... Ping / DecodeConfig ... do we have to use image magick.......?

Types

type Bucket

type Bucket struct {
	ID string
}

func NewBucket

func NewBucket(id string) (*Bucket, error)

func (*Bucket) AddImage

func (b *Bucket) AddImage(ctx context.Context, i *Image) (err error)

func (*Bucket) AddImages

func (b *Bucket) AddImages(ctx context.Context, images []*Image) (err error)

TODO: .. how do handle errors here... ? each image would have it's own error .. should we put an Err on each image object...? or return an errList ..

func (*Bucket) AddImagesFromUrls

func (b *Bucket) AddImagesFromUrls(ctx context.Context, urls []string) ([]*Image, error)

func (*Bucket) DbDelImage

func (b *Bucket) DbDelImage(ctx context.Context, key string) (err error)

TODO: should delete on *

func (*Bucket) DbFindImage

func (b *Bucket) DbFindImage(ctx context.Context, key string, optSizing ...*imgry.Sizing) (*Image, error)

Loads the image from our table+data store with optional sizing

func (*Bucket) DbIndexKey

func (b *Bucket) DbIndexKey(imageKey string, optSizing ...*imgry.Sizing) string

func (*Bucket) DbSaveImage

func (b *Bucket) DbSaveImage(ctx context.Context, im *Image, sizing *imgry.Sizing) (err error)

Persists the image blob in our data store

func (*Bucket) GetImageSize

func (b *Bucket) GetImageSize(ctx context.Context, key string, sizing *imgry.Sizing) (*Image, error)

func (*Bucket) ValidID

func (b *Bucket) ValidID() (bool, error)

type Config

type Config struct {
	Bind        string `toml:"bind"`
	MaxProcs    int    `toml:"max_procs"`
	LogLevel    string `toml:"log_level"`
	CacheMaxAge int    `toml:"cache_max_age"`
	TmpDir      string `toml:"tmp_dir"`
	Profiler    bool   `toml:"profiler"`

	// [cluster]
	Cluster struct {
		LocalNode string   `toml:"local_node"`
		Nodes     []string `toml:"nodes"`
	} `toml:"cluster"`

	// [limits]
	Limits struct {
		// Throttler settings
		MaxRequests       int    `toml:"max_requests"`
		BacklogSize       int    `toml:"backlog_size"`
		BacklogTimeoutStr string `toml:"backlog_timeout"`
		BacklogTimeout    time.Duration

		// Global request timeout
		RequestTimeoutStr string `toml:"request_timeout"`
		RequestTimeout    time.Duration

		// Imgry limits
		MaxFetchers    int `toml:"max_fetchers"`
		MaxImageSizers int `toml:"max_image_sizers"`
	} `toml:"limits"`

	// [db]
	DB struct {
		RedisUri string `toml:"redis_uri"`
	} `toml:"db"`

	// [airbrake]
	Airbrake struct {
		ApiKey string `toml:"api_key"`
	} `toml:"airbrake"`

	// [chainstore]
	Chainstore struct {
		Path          string `toml:"path"`
		MemCacheSize  int64  `toml:"mem_cache_size"`
		DiskCacheSize int64  `toml:"disk_cache_size"`
		S3Bucket      string `toml:"s3_bucket"`
		S3AccessKey   string `toml:"s3_access_key"`
		S3SecretKey   string `toml:"s3_secret_key"`
	} `toml:"chainstore"`

	// [statsd]
	StatsD struct {
		Enabled     bool   `toml:"enabled"`
		Address     string `toml:"address"`
		ServiceName string `toml:"service_name"`
	}
}

func NewConfig

func NewConfig() *Config

func NewConfigFromFile

func NewConfigFromFile(confFile string, confEnv string) (*Config, error)

func (*Config) Apply

func (cf *Config) Apply() (err error)

func (*Config) GetChainstore

func (cf *Config) GetChainstore() (chainstore.Store, error)

func (*Config) GetDB

func (cf *Config) GetDB() (*DB, error)

func (*Config) SetupStatsD

func (cf *Config) SetupStatsD() error

type DB

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

func NewDB

func NewDB(address string) (*DB, error)

func (*DB) Close

func (db *DB) Close()

func (*DB) Del

func (db *DB) Del(key string) (err error)

func (*DB) Exists

func (db *DB) Exists(key string) (bool, error)

func (*DB) Get

func (db *DB) Get(key string) (val []byte, err error)

func (*DB) HGet

func (db *DB) HGet(key string, dest interface{}) error

func (*DB) HSet

func (db *DB) HSet(key string, src interface{}) error

func (*DB) Ping

func (db *DB) Ping() error

func (*DB) Set

func (db *DB) Set(key string, obj []byte, expireIn ...time.Duration) (err error)

type Fetcher

type Fetcher struct {
	Client    *http.Client
	Transport *http.Transport

	Throughput     int // TODO
	ReqNumAttempts int
	HostKeepAlive  time.Duration
}

func NewFetcher

func NewFetcher() *Fetcher

func (Fetcher) Get

func (f Fetcher) Get(ctx context.Context, url string) (*FetcherResponse, error)

func (Fetcher) GetAll

func (f Fetcher) GetAll(ctx context.Context, urls []string) ([]*FetcherResponse, error)

type FetcherResponse

type FetcherResponse struct {
	URL    *url.URL
	Status int
	Data   []byte
	Err    error
}

type Image

type Image struct {
	Key         string        `json:"key" redis:"key"`
	SrcUrl      string        `json:"src_url" redis:"src"`
	Width       int           `json:"width" redis:"w"`
	Height      int           `json:"height" redis:"h"`
	Format      string        `json:"format" redis:"f"`
	SizingQuery string        `json:"-" redis:"q"` // query from below, for saving
	Sizing      *imgry.Sizing `json:"-" redis:"-"`
	Data        []byte        `json:"-" redis:"-"`
	// contains filtered or unexported fields
}

func NewImageFromKey

func NewImageFromKey(key string) *Image

Hrmm.. how will we generate a Uid if we just have a blob and no srcurl..? perhaps we allow the uid to be like "something.jpg" if they want..? unlikely to be collisions anyways... how to dedupe those..? guess we cant.. only if it was based on blob..

func NewImageFromSrcUrl

func NewImageFromSrcUrl(srcUrl string) *Image

func (*Image) IsValidImage

func (im *Image) IsValidImage() bool

func (*Image) LoadImage

func (im *Image) LoadImage() (err error)

func (*Image) MakeSize

func (im *Image) MakeSize(sizing *imgry.Sizing) (*Image, error)

Create a new blob object from an existing size

func (*Image) MimeType

func (im *Image) MimeType() string

func (*Image) Release

func (im *Image) Release()

func (*Image) SizeIt

func (im *Image) SizeIt(sizing *imgry.Sizing) error

Sizes the current image in place

func (*Image) SrcFileExtension

func (im *Image) SrcFileExtension() string

func (*Image) SrcFilename

func (im *Image) SrcFilename() string

func (*Image) ValidateKey

func (im *Image) ValidateKey() error

type Responder

type Responder struct {
	*render.Render
}

func NewResponder

func NewResponder() *Responder

func (*Responder) ApiError

func (r *Responder) ApiError(w http.ResponseWriter, status int, err error)

func (*Responder) ImageError

func (r *Responder) ImageError(w http.ResponseWriter, status int, err error)

type Server

type Server struct {
	Config      *Config
	DB          *DB
	Chainstore  chainstore.Store
	Fetcher     *Fetcher
	ImageEngine imgry.Engine
}

func New

func New(conf *Config) *Server

func (*Server) Close

func (srv *Server) Close()

Close signals to the server that should deny new requests and finish up requests in progress.

func (*Server) Configure

func (srv *Server) Configure() (err error)

func (*Server) NewRouter

func (srv *Server) NewRouter() http.Handler

func (*Server) Shutdown

func (srv *Server) Shutdown()

Shutdown will release other resources and halt the server.

Jump to

Keyboard shortcuts

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