Documentation ¶
Index ¶
- Constants
- Variables
- func NotFoundHandler() http.Handler
- func Run(path string) error
- type Application
- func (a *Application) ImageCleanup(filepath string)
- func (a *Application) ImageFileFromRequest(req *Request, async bool, load bool) (*image.ImageFile, error)
- func (a *Application) InitRouter() *negroni.Negroni
- func (a *Application) Port() int
- func (app *Application) ServeHTTP(h Handler) http.Handler
- func (a *Application) ShardFilename(filename string) string
- func (a *Application) Store(filepath string, i *image.ImageFile) error
- func (a *Application) WithPrefix(str string) string
- type Extractor
- type Handler
- type Initializer
- type KVStoreParameter
- type MultipartForm
- type Request
- type Response
- func (r *Response) Abort(status int, body string)
- func (r *Response) BadRequest()
- func (r *Response) ContentType(val string) string
- func (r *Response) Forbidden()
- func (r *Response) NotAllowed()
- func (r *Response) NotFound(message string)
- func (r *Response) NotModified()
- func (r *Response) Ok(message string)
- func (r *Response) PermanentRedirect(url string)
- func (r *Response) Redirect(status int, url_ string)
- func (r *Response) SetHeader(hdr string, val string, unique bool)
- func (r *Response) SetHeaders(headers map[string]string, unique bool)
- func (r *Response) Unauthorized()
- func (r *Response) WriteString(content string)
- type Shard
- type StorageParameter
Constants ¶
const DefaultContentType = "image/png"
const DefaultFormat = "png"
const DefaultPort = 3001
const DefaultQuality = 95
const DefaultShardDepth = 0
const DefaultShardWidth = 0
const SIG_PARAM_NAME = "sig"
const Version = "0.3"
Current version of the picfit
Variables ¶
var DeleteHandler = func(res Response, req *http.Request, app *Application) { if app.SourceStorage == nil { res.Abort(500, "Your application doesn't have a source storage") return } params := mux.Vars(req) filename := params["path"] app.Logger.Infof("Deleting source storage file: %s", filename) _ = app.SourceStorage.Delete(filename) app.ImageCleanup(filename) content, err := json.Marshal(map[string]string{ "filename": filename, }) if err != nil { panic(err) } res.ContentType("application/json") res.ResponseWriter.Write(content) }
var Initializers = []Initializer{ KVStoreInitializer, StorageInitializer, ShardInitializer, BasicInitializer, SentryInitializer, }
var KVStores = map[string]KVStoreParameter{ "redis": RedisKVStoreParameter, "cache": CacheKVStoreParameter, }
var Storages = map[string]StorageParameter{ "http+s3": HTTPS3StorageParameter, "s3": S3StorageParameter, "http+fs": HTTPFileSystemStorageParameter, "fs": FileSystemStorageParameter, }
var UploadHandler = func(res 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 ¶
Types ¶
type Application ¶
type Application struct { EnableUpload bool EnableDelete 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) ImageCleanup ¶
func (a *Application) ImageCleanup(filepath string)
func (*Application) ImageFileFromRequest ¶
func (*Application) InitRouter ¶
func (a *Application) InitRouter() *negroni.Negroni
func (*Application) Port ¶
func (a *Application) Port() int
func (*Application) ShardFilename ¶
func (a *Application) ShardFilename(filename string) string
func (*Application) WithPrefix ¶
func (a *Application) WithPrefix(str string) string
type Extractor ¶
var URL Extractor = func(key string, req *Request) (interface{}, error) { value, ok := req.QueryString[key] if !ok { return nil, nil } url, err := url.Parse(value) if err != nil { return nil, fmt.Errorf("URL %s is not valid", value) } mimetype := mime.TypeByExtension(filepath.Ext(value)) _, ok = image.Extensions[mimetype] if !ok { return nil, fmt.Errorf("Mimetype %s is not supported", mimetype) } return url, nil }
type Handler ¶
type Handler func(Response, *Request, *Application)
var GetHandler Handler = func(res 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) }
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 } enableDelete, err := jq.Bool("options", "enable_delete") if err == nil { app.EnableDelete = enableDelete } 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 { Request *http.Request Operation *engines.Operation Connection gokvstores.KVStoreConnection Key string URL *url.URL Filepath string Params map[string]string QueryString map[string]string }
func NewRequest ¶
func NewRequest(req *http.Request, con gokvstores.KVStoreConnection) (*Request, error)
func (*Request) IsAuthorized ¶
type Response ¶
type Response struct {
http.ResponseWriter
}
func NewResponse ¶
func NewResponse(res http.ResponseWriter) Response
func (*Response) Abort ¶
Abort is a helper method that sends an HTTP header and an optional body. It is useful for returning 4xx or 5xx errors. Once it has been called, any return value from the handler will not be written to the response.
func (*Response) ContentType ¶
ContentType sets the Content-Type header for an HTTP response. For example, ctx.ContentType("json") sets the content-type to "application/json" If the supplied value contains a slash (/) it is set as the Content-Type verbatim. The return value is the content type as it was set, or an empty string if none was found.
func (*Response) NotModified ¶
func (r *Response) NotModified()
Notmodified writes a 304 HTTP response
func (*Response) PermanentRedirect ¶
PermanentRedirect is a helper method for 301 redirect
func (*Response) SetHeader ¶
SetHeader sets a response header. If `unique` is true, the current value of that header will be overwritten . If false, it will be appended.
func (*Response) SetHeaders ¶
SetHeaders sets response headers. If `unique` is true, the current value of that header will be overwritten . If false, it will be appended.
func (*Response) Unauthorized ¶
func (r *Response) Unauthorized()
Unauthorized writes a 401 HTTP response
func (*Response) WriteString ¶
WriteString writes string data into the response object.
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 }