application

package
v0.0.0-...-0113a05 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2015 License: MIT Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultContentType = "image/png"
View Source
const DefaultFormat = "png"
View Source
const DefaultPort = 3001
View Source
const DefaultShardDepth = 0
View Source
const DefaultShardWidth = 0
View Source
const Version = "0.1"

Current version of the picfit

Variables

Functions

func NotFound

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

func NotFoundHandler

func NotFoundHandler() http.Handler

func Run

func Run(path string) error

Types

type Application

type Application struct {
	Prefix        string
	SecretKey     string
	Format        string
	KVStore       gokvstores.KVStore
	SourceStorage gostorages.Storage
	DestStorage   gostorages.Storage
	Router        *mux.Router
	Shard         Shard
	Raven         *raven.Client
	Logger        *logrus.Logger
}

func NewApplication

func NewApplication() *Application

func (*Application) ImageFileFromRequest

func (a *Application) ImageFileFromRequest(req *Request, async bool, load bool) (*image.ImageFile, error)

func (*Application) IsValidSign

func (a *Application) IsValidSign(qs map[string]string) bool

func (*Application) ShardFilename

func (a *Application) ShardFilename(filename string) string

func (*Application) Store

func (a *Application) Store(i *image.ImageFile) error

func (*Application) WithPrefix

func (a *Application) WithPrefix(str string) string

type Handler

type Handler func(muxer.Response, *Request)
var GetHandler Handler = func(res muxer.Response, req *Request) {
	file, err := App.ImageFileFromRequest(req, false, false)

	util.PanicIf(err)

	content, err := json.Marshal(map[string]string{
		"filename": file.Filename(),
		"path":     file.Path(),
		"url":      file.URL(),
	})

	util.PanicIf(err)

	res.ContentType("application/json")
	res.ResponseWriter.Write(content)
}
var ImageHandler Handler = func(res muxer.Response, req *Request) {
	file, err := App.ImageFileFromRequest(req, true, true)

	util.PanicIf(err)

	content, err := file.ToBytes()

	util.PanicIf(err)

	res.SetHeaders(file.Headers, true)
	res.ResponseWriter.Write(content)
}
var RedirectHandler Handler = func(res muxer.Response, req *Request) {
	file, err := App.ImageFileFromRequest(req, false, false)

	util.PanicIf(err)

	res.PermanentRedirect(file.URL())
}

func (Handler) ServeHTTP

func (h Handler) ServeHTTP(w http.ResponseWriter, req *http.Request)

type Initializer

type Initializer func(jq *jsonq.JsonQuery) error
var BasicInitializer Initializer = func(jq *jsonq.JsonQuery) error {
	format, _ := jq.String("format")

	if format != "" {
		App.Format = format
	} else {
		App.Format = DefaultFormat
	}

	App.SecretKey, _ = jq.String("secret_key")

	return nil
}
var KVStoreInitializer Initializer = func(jq *jsonq.JsonQuery) error {
	_, err := jq.Object("kvstore")

	if err != nil {
		App.KVStore = &dummy.DummyKVStore{}

		return nil
	}

	key, err := jq.String("kvstore", "type")

	if err != nil {
		return err
	}

	parameter, ok := KVStores[key]

	if !ok {
		return fmt.Errorf("KVStore %s does not exist", key)
	}

	config, err := jq.Object("kvstore")

	if err != nil {
		return err
	}

	params := util.MapInterfaceToMapString(config)
	store, err := parameter(params)

	if err != nil {
		return err
	}

	App.Prefix = params["prefix"]
	App.KVStore = store

	return nil
}
var SentryInitializer Initializer = func(jq *jsonq.JsonQuery) error {
	dsn, err := jq.String("sentry", "dsn")

	if err != nil {
		return nil
	}

	results, err := jq.Object("sentry", "tags")

	var tags map[string]string

	if err != nil {
		tags = map[string]string{}
	} else {
		tags = util.MapInterfaceToMapString(results)
	}

	client, err := raven.NewClient(dsn, tags)

	if err != nil {
		return err
	}

	App.Raven = client

	return nil
}
var ShardInitializer Initializer = func(jq *jsonq.JsonQuery) error {
	width, err := jq.Int("shard", "width")

	if err != nil {
		width = DefaultShardWidth
	}

	depth, err := jq.Int("shard", "depth")

	if err != nil {
		depth = DefaultShardDepth
	}

	App.Shard = Shard{Width: width, Depth: depth}

	return nil
}
var StorageInitializer Initializer = func(jq *jsonq.JsonQuery) error {
	_, err := jq.Object("storage")

	if err != nil {
		App.SourceStorage = &dummy.DummyStorage{}
		App.DestStorage = &dummy.DummyStorage{}

		return nil
	}

	sourceStorage, err := getStorageFromConfig("src", jq)

	if err != nil {
		return err
	}

	App.SourceStorage = sourceStorage

	destStorage, err := getStorageFromConfig("dst", jq)

	if err != nil {
		App.DestStorage = sourceStorage
	} else {
		App.DestStorage = destStorage
	}

	return nil
}

type KVStoreParameter

type KVStoreParameter func(params map[string]string) (gokvstores.KVStore, error)
var CacheKVStoreParameter KVStoreParameter = func(params map[string]string) (gokvstores.KVStore, error) {
	value, ok := params["max_entries"]

	var maxEntries int

	if !ok {
		maxEntries = -1
	} else {
		maxEntries, _ = strconv.Atoi(value)
	}

	return gokvstores.NewCacheKVStore(maxEntries), nil
}
var RedisKVStoreParameter KVStoreParameter = func(params map[string]string) (gokvstores.KVStore, error) {
	host := params["host"]

	password := params["password"]

	port, _ := strconv.Atoi(params["port"])

	db, _ := strconv.Atoi(params["db"])

	return gokvstores.NewRedisKVStore(host, port, password, db), nil
}

type Request

type Request struct {
	*muxer.Request
	Operation  *image.Operation
	Connection gokvstores.KVStoreConnection
	Key        string
	URL        *url.URL
	Filepath   string
	Format     string
}

type Shard

type Shard struct {
	Depth int
	Width int
}

type StorageParameter

type StorageParameter func(params map[string]string) (gostorages.Storage, error)
var FileSystemStorageParameter StorageParameter = func(params map[string]string) (gostorages.Storage, error) {
	return gostorages.NewFileSystemStorage(params["location"], params["base_url"]), nil
}
var HTTPFileSystemStorageParameter StorageParameter = func(params map[string]string) (gostorages.Storage, error) {
	storage, err := FileSystemStorageParameter(params)

	if err != nil {
		return nil, err
	}

	if _, ok := params["base_url"]; !ok {
		return nil, fmt.Errorf("You can't use the http wrapper without setting *base_url* in your config file")
	}

	return &http.HTTPStorage{storage}, nil
}
var HTTPS3StorageParameter StorageParameter = func(params map[string]string) (gostorages.Storage, error) {
	storage, err := S3StorageParameter(params)

	if err != nil {
		return nil, err
	}

	if _, ok := params["base_url"]; !ok {
		return nil, fmt.Errorf("You can't use the http wrapper without setting *base_url* in your config file")
	}

	return &http.HTTPStorage{storage}, nil
}
var S3StorageParameter StorageParameter = func(params map[string]string) (gostorages.Storage, error) {

	ACL, ok := gostorages.ACLs[params["acl"]]

	if !ok {
		return nil, fmt.Errorf("The ACL %s does not exist", params["acl"])
	}

	Region, ok := aws.Regions[params["region"]]

	if !ok {
		return nil, fmt.Errorf("The Region %s does not exist", params["region"])
	}

	return gostorages.NewS3Storage(
		params["access_key_id"],
		params["secret_access_key"],
		params["bucket_name"],
		params["location"],
		Region,
		ACL,
		params["base_url"],
	), nil
}

Jump to

Keyboard shortcuts

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