Darkroom combines the storage backend and the image processor and acts as an Image Proxy
on your image source.
You may implement your own Storage
and Processor
interfaces to gain custom functionality while still keeping other Darkroom Server functionality.
The native implementations focus on speed and resiliency.
go get -u github.com/gojek/darkroom
Darkroom acts as an image proxy and currently support several image processing operations such as:
- Cropping based on given anchor points (top, left, right, bottom)
- Resizing
- Grayscaling
The project has docker images available. They can be tested locally or can be be deployed to production.
Create a file containing the environment variables mentioned in config.yaml.example
and save it as config.env
Note: Bucket credentials are dummy, you need to provide your own credentials.
DEBUG=true
LOG_LEVEL=debug
APP_NAME=darkroom
APP_VERSION=0.0.1
APP_DESCRIPTION=A realtime image processing service
SOURCE_KIND=s3
SOURCE_BUCKET_NAME=bucket-name
SOURCE_BUCKET_REGION=bucket-region
SOURCE_BUCKET_ACCESSKEY=AKIA*************
SOURCE_BUCKET_SECRETKEY=4y/*******************************
SOURCE_PATHPREFIX=/uploads
PORT=3000
CACHE_TIME=31536000
SOURCE_HYSTRIX_COMMANDNAME=S3_ADAPTER
SOURCE_HYSTRIX_TIMEOUT=5000
SOURCE_HYSTRIX_MAXCONCURRENTREQUESTS=100
SOURCE_HYSTRIX_REQUESTVOLUMETHRESHOLD=10
SOURCE_HYSTRIX_SLEEPWINDOW=10
SOURCE_HYSTRIX_ERRORPERCENTTHRESHOLD=25
Build the docker image and run it with the config created.
make docker-image
docker run -p 80:3000 --env-file ./config.env ${USER}/darkroom:latest
You may customise darkroom, for example, you may want to write a storage backend that talks to another service and gets the images. Or might want to create an image processor that uses GPU acceleration to speed up the performance.
type Processor interface {
Crop(img image.Image, width, height int, point CropPoint) image.Image
Decode(data []byte) (image.Image, string, error)
Encode(img image.Image, format string) ([]byte, error)
GrayScale(img image.Image) image.Image
Resize(img image.Image, width, height int) image.Image
Watermark(base []byte, overlay []byte, opacity uint8) ([]byte, error)
Flip(image image.Image, mode string) image.Image
Rotate(image image.Image, angle float64) image.Image
FixOrientation(image image.Image, orientation int) image.Image
}
type Storage interface {
Get(ctx context.Context, path string) IResponse
}
type IResponse interface {
Data() []byte
Error() error
Status() int
}
Any struct
implementing the above interfaces can be used with Darkroom.
Note: The struct implementing the
Storage
interface must return a struct implementing theIResponse
interface.