Merge branch 'master' of github.com:webp-sh/webp_server_go

This commit is contained in:
keshanechen 2020-03-12 14:11:46 +08:00
commit 4288d19abc
9 changed files with 52 additions and 66 deletions

4
.gitattributes vendored
View File

@ -1,4 +0,0 @@
*.jpg filter=lfs diff=lfs merge=lfs -text
*.png filter=lfs diff=lfs merge=lfs -text
*.webp filter=lfs diff=lfs merge=lfs -text
*.jpeg filter=lfs diff=lfs merge=lfs -text

View File

@ -3,8 +3,6 @@
</p> </p>
<img src="https://api.travis-ci.org/webp-sh/webp_server_go.svg?branch=master"/> <img src="https://api.travis-ci.org/webp-sh/webp_server_go.svg?branch=master"/>
After the [n0vad3v/webp_server](https://github.com/n0vad3v/webp_server), I decide to rewrite the whole program with Go, as there will be no more `npm install`s or `docker-compose`s.
This is a Server based on Golang, which allows you to serve WebP images on the fly. This is a Server based on Golang, which allows you to serve WebP images on the fly.
It will convert `jpg,jpeg,png` files by default, this can be customized by editing the `config.json`.. It will convert `jpg,jpeg,png` files by default, this can be customized by editing the `config.json`..
* currently supported image format: JPEG, PNG, BMP, GIF(static image for now) * currently supported image format: JPEG, PNG, BMP, GIF(static image for now)
@ -14,34 +12,15 @@ It will convert `jpg,jpeg,png` files by default, this can be customized by editi
> >
> For Safari and Opera users, the original image will be used. > For Safari and Opera users, the original image will be used.
## Compare to [n0vad3v/webp_server](https://github.com/n0vad3v/webp_server)
### Size
* `webp_server` with `node_modules`: 43M
* `webp-server(go)` single binary: 15M
### Performance
It's basically between `ExpressJS` and `Fiber`, much faster than the `http` package of course.
### Convenience
* `webp_server`: Clone the repo -> `npm install` -> run with `pm2`
* `webp-server(go)`: Download a single binary -> Run
### Auto update
This tool will check for new release whenever you run it. The updated binary will be save to `update` dir.
## General Usage Steps ## General Usage Steps
### 1. Download or build the binary
## 1. Download or build the binary
Download the `webp-server` from [release](https://github.com/n0vad3v/webp_server_go/releases) page. Download the `webp-server` from [release](https://github.com/n0vad3v/webp_server_go/releases) page.
Wanna build your own binary? Check out [build](#build-your-own-binaries) section Wanna build your own binary? Check out [build](#build-your-own-binaries) section
## 2. Dump config file ### 2. Dump config file
``` ```
./webp-server -dump-config > config.json ./webp-server -dump-config > config.json
@ -65,7 +44,7 @@ If you are serving images at `https://example.com/pics/tsuki.jpg` and your files
`EXHAUST_PATH` is cache folder for output `webp` images, with `EXHAUST_PATH` set to `/var/cache/webp` `EXHAUST_PATH` is cache folder for output `webp` images, with `EXHAUST_PATH` set to `/var/cache/webp`
in the example above, your `webp` image will be saved at `/var/cache/webp/pics/tsuki.jpg.1582558990.webp`. in the example above, your `webp` image will be saved at `/var/cache/webp/pics/tsuki.jpg.1582558990.webp`.
## 3. Run ### 3. Run
``` ```
./webp-server --help ./webp-server --help
@ -85,7 +64,7 @@ Usage of ./webp-server:
-prefork -prefork
use prefork use prefork
``` ```
### Prefetch #### Prefetch
Prefetch will convert all your images to webp. Don't worry, WebP Server will start, you don't have to wait until prefetch completes. Prefetch will convert all your images to webp. Don't worry, WebP Server will start, you don't have to wait until prefetch completes.
``` ```
./webp-server -prefetch ./webp-server -prefetch
@ -97,14 +76,14 @@ By default, it will utilize all your CPU cores.
./webp-server -prefetch -jobs=4 ./webp-server -prefetch -jobs=4
``` ```
### dump systemd service file #### dump systemd service file
The standard systemd service file will show on your screen. You many want to use `>` to redirect to a file. The standard systemd service file will show on your screen. You many want to use `>` to redirect to a file.
``` ```
./webp-server -dump-systemd ./webp-server -dump-systemd
``` ```
### screen or tmux #### screen or tmux
Use `screen` or `tmux` to avoid being terminated. Let's take `screen` for example Use `screen` or `tmux` to avoid being terminated. Let's take `screen` for example
``` ```
screen -S webp screen -S webp
@ -112,7 +91,7 @@ screen -S webp
``` ```
(Use Ctrl-A-D to detach the `screen` with `webp-server` running.) (Use Ctrl-A-D to detach the `screen` with `webp-server` running.)
### systemd #### systemd
Don't worry, we've got you covered! Don't worry, we've got you covered!
Download `webp-server` to `/opt/webps/webp-server`, and create a config file to `/opt/webps/config.json`, then, Download `webp-server` to `/opt/webps/webp-server`, and create a config file to `/opt/webps/config.json`, then,
@ -123,9 +102,9 @@ systemctl daemon-reload
systemctl enable webp-server.service systemctl enable webp-server.service
systemctl start webp-server.service systemctl start webp-server.service
``` ```
## 4. Nginx proxy_pass ### 4. Nginx proxy_pass
Let Nginx to `proxy_pass http://localhost:3333/;`, and your webp-server is on-the-fly Let Nginx to `proxy_pass http://localhost:3333/;`, and your webp-server is on-the-fly
### WordPress example #### WordPress example
``` ```
location ^~ /wp-content/uploads/ { location ^~ /wp-content/uploads/ {
proxy_pass http://127.0.0.1:3333; proxy_pass http://127.0.0.1:3333;
@ -133,7 +112,8 @@ location ^~ /wp-content/uploads/ {
``` ```
If you use Caddy, you may refer to [优雅的让 Halo 支持 webp 图片输出](https://halo.run/archives/halo-and-webp). If you use Caddy, you may refer to [优雅的让 Halo 支持 webp 图片输出](https://halo.run/archives/halo-and-webp).
## Advanced usage ### Auto update
This tool will check for new release whenever you run it. The updated binary will be save to `update` dir.
## Build your own binaries ## Build your own binaries
Install latest version of golang, enable go module, clone the repo, and then... Install latest version of golang, enable go module, clone the repo, and then...
@ -143,6 +123,23 @@ make
**Due to the limitations of webp module, you can't cross compile this tool. **Due to the limitations of webp module, you can't cross compile this tool.
But the binary will work instantly on your platform and arch** But the binary will work instantly on your platform and arch**
## Compare to [n0vad3v/webp_server](https://github.com/n0vad3v/webp_server)
### Size
* `webp_server` with `node_modules`: 43M
* `webp-server(go)` single binary: 15M
### Performance
It's basically between `ExpressJS` and `Fiber`, much faster than the `http` package of course.
### Convenience
* `webp_server`: Clone the repo -> `npm install` -> run with `pm2`
* `webp-server(go)`: Download a single binary -> Run
## TODO ## TODO
- [x] This version doesn't support header-based-output, which means Safari users will not see the converted `webp` images, this should be fixed in later releases. - [x] This version doesn't support header-based-output, which means Safari users will not see the converted `webp` images, this should be fixed in later releases.
- [ ] Multi platform support. - [ ] Multi platform support.

View File

@ -56,7 +56,7 @@ func WebpEncoder(p1, p2 string, quality float32, Log bool, c chan int) (err erro
ChanErr(c) ChanErr(c)
return return
} }
if err = ioutil.WriteFile(p2, buf.Bytes(), 0755); err != nil { if err = ioutil.WriteFile(p2, buf.Bytes(), 0644); err != nil {
log.Error(err) log.Error(err)
ChanErr(c) ChanErr(c)
return return

Binary file not shown.

Before

Width:  |  Height:  |  Size: 131 B

After

Width:  |  Height:  |  Size: 206 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 132 B

After

Width:  |  Height:  |  Size: 997 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 131 B

After

Width:  |  Height:  |  Size: 206 KiB

BIN
pics/webp_server.png Executable file → Normal file

Binary file not shown.

Before

Width:  |  Height:  |  Size: 131 B

After

Width:  |  Height:  |  Size: 263 KiB

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"bufio"
"fmt" "fmt"
"os" "os"
"path" "path"
@ -13,18 +12,13 @@ import (
) )
func PrefetchImages(confImgPath string, ExhaustPath string, QUALITY string) { func PrefetchImages(confImgPath string, ExhaustPath string, QUALITY string) {
fmt.Println(`Prefetch will convert all your images to webp,
it may take some time and consume a lot of CPU resource. Do you want to proceed(Y/n)`)
reader := bufio.NewReader(os.Stdin)
char, _, _ := reader.ReadRune() //y Y enter
// maximum ongoing prefetch is depending on your core of CPU // maximum ongoing prefetch is depending on your core of CPU
log.Infof("Prefetching using %d cores", jobs) log.Infof("Prefetching using %d cores", jobs)
var finishChan = make(chan int, jobs) var finishChan = make(chan int, jobs)
for i := 0; i < jobs; i++ { for i := 0; i < jobs; i++ {
finishChan <- 0 finishChan <- 0
} }
if char == 121 || char == 10 || char == 89 {
//prefetch, recursive through the dir //prefetch, recursive through the dir
all := FileCount(confImgPath) all := FileCount(confImgPath)
count := 0 count := 0
@ -47,7 +41,6 @@ it may take some time and consume a lot of CPU resource. Do you want to proceed(
if err != nil { if err != nil {
log.Debug(err) log.Debug(err)
} }
}
_, _ = fmt.Fprintf(os.Stdout, "Prefetch completeY(^_^)Y\n\n") _, _ = fmt.Fprintf(os.Stdout, "Prefetch completeY(^_^)Y\n\n")

View File

@ -15,7 +15,7 @@ import (
func autoUpdate() { func autoUpdate() {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
log.Error("Download error.", err) log.Errorf("Download error.", err)
} }
}() }()