Home

Awesome

webp-server

Coverage Status Go Report Card Release Github Workflow Status Mentioned in Awesome Go

Simple and minimal image server capable of storing, resizing, converting, and caching images. You can quickly find out how it works by looking at the flowchart below.

<p align="center"> <img src="https://github.com/mehdipourfar/webp-server/raw/master/docs/flowchart.jpg" alt="Flowchart"/> </p>

Contents

Quickstart

Run a docker container of webp-server.

docker run -d -v webp_server_volume:/var/lib/webp-server --name webp-server -e TOKEN='MY_STRONG_TOKEN' -p 127.0.0.1:8080:8080 ms68/webp-server

Upload an image:

curl -H 'Token: MY_STRONG_TOKEN' -X POST -F 'image_file=@/path/to/image.jpg' http://127.0.0.1:8080/upload/

# this api will return an image_id

Open these urls in your browser.

http://127.0.0.1:8080/image/width=500,height=500,fit=contain,quality=100/{image_id}
http://127.0.0.1:8080/image/width=300,height=300,fit=cover,quality=90/{image_id}

For supporting more image sizes and qualities, you should edit the config file which resides in webp_server_volume:

docker volume ls -f name=webp_server_volume --format "{{ .Mountpoint }}"

And then, restart the server:

docker container restart webp-server

FAQ

Installation

There are two methods for running webp-server. Either use docker or build it yourself:

Docker

docker run -d -v webp_server_volume:/var/lib/webp-server --name webp-server -e TOKEN='MY_STRONG_TOKEN' -p 127.0.0.1:8080:8080 ms68/webp-server

Download Binary

webp-server is depending on libvips=>8.9. On Ubuntu 20.04 You can install it By the command below:

sudo apt install libvips

After installation, you can check your libvips version by running this command:

vips -v

Download the binary from here:

wget https://github.com/mehdipourfar/webp-server/releases/download/v1.0.0/webp-server_1.0.0_linux_amd64.tar.gz

Build From Source

sudo apt install libvips-dev git


## in Case you don't have Golang installed on your system.

wget https://golang.org/dl/go1.15.6.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.15.6.linux-amd64.tar.gz
export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin

go get -u -v github.com/mehdipourfar/webp-server
sudo cp $HOME/go/bin/webp-server /usr/bin/


# Download and edit `example-config.yml` to your desired config
wget https://raw.githubusercontent.com/mehdipourfar/webp-server/master/example-config.yml

# Run the server:
webp-server -config example-config.yml

Configuration

There is an example configuration file example-config.yml in the code directory. Here is the list of parameters that you can configure:

Backend APIs

Frontend APIs

Some example image urls:

http://example.com/image/w=500,h=500/lulRDHbMg
http://example.com/image/w=500,h=500,q=95/lulRDHbMg
http://example.com/image/w=500,h=500,fit=cover/lulRDHbMg
http://example.com/image/w=500,h=500,fit=contain/lulRDHbMg
http://example.com/image/w=500,fit=contain/lulRDHbMg

Reverse Proxy

webp-server does not support SSL or domain name validation. It is recommended to use a reverse proxy such as nginx in front of it. It should only cover frontend APIs. Backend APIs should be called locally. Here is a minimal nginx configuration that redirects all the paths which start with /image/ to webp-server.


upstream webp_server {
    server 127.0.0.1:8080 fail_timeout=0;
}

server {
   # ...

   location /image/ {
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Protocol $scheme;
        proxy_pass http://webp_server;
    }
}

Security Checklist