application

package
v0.0.0-...-c800278 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2015 License: MIT Imports: 34 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 DefaultQuality = 95
View Source
const DefaultShardDepth = 0
View Source
const DefaultShardWidth = 0
View Source
const SIG_PARAM_NAME = "sig"
View Source
const Version = "0.2"

Current version of the picfit

Variables

View Source
var Extractors = map[string]extractors.Extractor{
	"op":   extractors.Operation,
	"url":  extractors.URL,
	"path": extractors.Path,
}
View Source
var UploadHandler = func(res muxer.Response, req *http.Request, app *Application) {
	if !app.EnableUpload {
		res.Forbidden()
		return
	}

	if app.SourceStorage == nil {
		res.Abort(500, "Your application doesn't have a source storage")
		return
	}

	var err error

	multipartForm := new(MultipartForm)
	errs := binding.Bind(req, multipartForm)
	if errs.Handle(res) {
		return
	}

	file, err := multipartForm.Upload(app.SourceStorage)

	if err != nil {
		panic(err)
	}

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

	if err != nil {
		panic(err)
	}

	res.ContentType("application/json")
	res.ResponseWriter.Write(content)
}

Functions

func NotFoundHandler

func NotFoundHandler() http.Handler

func Run

func Run(path string) error

Types

type Application

type Application struct {
	EnableUpload  bool
	Prefix        string
	SecretKey     string
	KVStore       gokvstores.KVStore
	SourceStorage gostorages.Storage
	DestStorage   gostorages.Storage
	Shard         Shard
	Raven         *raven.Client
	Logger        *logrus.Logger
	Engine        engines.Engine
	Jq            *jsonq.JsonQuery
}

func NewApplication

func NewApplication() *Application

func NewFromConfig

func NewFromConfig(content string) (*Application, error)

func NewFromConfigPath

func NewFromConfigPath(path string) (*Application, error)

func NewFromJsonQuery

func NewFromJsonQuery(jq *jsonq.JsonQuery) (*Application, error)

func (*Application) ImageFileFromRequest

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

func (*Application) InitRouter

func (a *Application) InitRouter() *negroni.Negroni

func (*Application) Port

func (a *Application) Port() int

func (*Application) ServeHTTP

func (app *Application) ServeHTTP(h Handler) http.Handler

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, *Application)
var GetHandler Handler = func(res muxer.Response, req *Request, app *Application) {
	file, err := app.ImageFileFromRequest(req, false, false)

	if err != nil {
		panic(err)
	}

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

	if err != nil {
		panic(err)
	}

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

	if err != nil {
		panic(err)
	}

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

	if err != nil {
		panic(err)
	}

	res.PermanentRedirect(file.URL())
}

type Initializer

type Initializer func(jq *jsonq.JsonQuery, app *Application) error
var BasicInitializer Initializer = func(jq *jsonq.JsonQuery, app *Application) error {
	var format string
	var quality int

	f, _ := jq.String("options", "format")

	if f != "" {
		format = f
	}

	q, err := jq.Int("options", "quality")

	if err != nil {
		quality = q
	}

	if quality == 0 {
		quality = DefaultQuality
	}

	app.SecretKey, _ = jq.String("secret_key")
	app.Engine = &engines.GoImageEngine{
		DefaultFormat:  DefaultFormat,
		Format:         format,
		DefaultQuality: quality,
	}

	enableUpload, err := jq.Bool("options", "enable_upload")

	if err == nil {
		app.EnableUpload = enableUpload
	}

	return nil
}
var KVStoreInitializer Initializer = func(jq *jsonq.JsonQuery, app *Application) 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, app *Application) 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, app *Application) 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, app *Application) 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 MultipartForm

type MultipartForm struct {
	Data *multipart.FileHeader `json:"data"`
}

func (*MultipartForm) FieldMap

func (f *MultipartForm) FieldMap() binding.FieldMap

func (*MultipartForm) Upload

func (f *MultipartForm) Upload(storage gostorages.Storage) (*image.ImageFile, error)

type Request

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

func NewRequest

func NewRequest(req *http.Request, con gokvstores.KVStoreConnection) (*Request, error)

func (*Request) IsAuthorized

func (r *Request) IsAuthorized(key string) bool

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