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

This commit is contained in:
n0vad3v 2020-03-03 00:08:00 +08:00
commit 2fa8db6b02
6 changed files with 51 additions and 10 deletions

View File

@ -33,11 +33,13 @@ It's basically between `ExpressJS` and `Fiber`, much faster than the `http` pack
Regarding the `IMG_PATH` section in `config.json`.
If you are serving images at `https://example.com/pics/tsuki.jpg` and
your files are at `/var/www/image/pics/tsuki.jpg`, then `IMG_PATH` shall be `/var/www/image`.
`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`.
## 1. Download or build the binary
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. config file
Create a `config.json` as follows to face your need, default convert quality is 80%.
@ -47,6 +49,7 @@ Create a `config.json` as follows to face your need, default convert quality is
"PORT": "3333",
"QUALITY": "80",
"IMG_PATH": "/path/to/pics",
"EXHAUST_PATH": "/path/to/exhaust",
"ALLOWED_TYPES": ["jpg","png","jpeg"]
}
```
@ -95,6 +98,8 @@ But the binary will work instantly on your platform and arch**
- [x] A better way to supervise the program.
- [ ] Get rid of render-blocking effect on first render.
- [x] Prefetch on server initialization.
- [x] Custom exhaust path.
- [ ] Multiple listen address.
## Related Articles(In chronological order)

View File

@ -3,5 +3,6 @@
"PORT": "3333",
"QUALITY": "80",
"IMG_PATH": "/path/to/pics",
"EXHAUST_PATH": "/path/to/exhaust",
"ALLOWED_TYPES": ["jpg","png","jpeg"]
}

13
scripts/unix.sh Normal file
View File

@ -0,0 +1,13 @@
#!/usr/bin/env bash
cd ..
git pull
platform=$(uname -a)
if [[ $platform =~ "Darwin" ]]
then
go build -o webp-server-darwin-amd64 webp-server.go
elif [[ $platform =~ "x86_64" ]];then
go build -o webp-server-unix-amd64 webp-server.go
else
go build -o webp-server-linux-i386 webp-server.go
fi

View File

@ -1,5 +1,5 @@
Name: webp-server
Version: 0.0.3
Version: 0.0.4
Release: 1%{?dist}
Summary: Go version of WebP Server. A tool that will serve your JPG/PNGs as WebP format with compression, on-the-fly.

14
scripts/windows.bat Normal file
View File

@ -0,0 +1,14 @@
cd ..
git pull
IF EXIST "%PROGRAMFILES(X86)%" (GOTO 64BIT) ELSE (GOTO 32BIT)
:64BIT
go build -o webp-server-windows-amd64.exe webp-server.go
GOTO END
:32BIT
echo 32-bit...
go build -o webp-server-windows-i386.exe webp-server.go
GOTO END
pause

View File

@ -30,6 +30,7 @@ type Config struct {
ImgPath string `json:"IMG_PATH"`
QUALITY string
AllowedTypes []string `json:"ALLOWED_TYPES"`
ExhaustPath string `json:"EXHAUST_PATH"`
}
var configPath string
@ -122,7 +123,7 @@ func init() {
flag.Parse()
}
func Convert(ImgPath string, AllowedTypes []string, QUALITY string) func(c *fiber.Ctx) {
func Convert(ImgPath string, ExhaustPath string, AllowedTypes []string, QUALITY string) func(c *fiber.Ctx) {
return func(c *fiber.Ctx) {
//basic vars
var reqURI = c.Path() // mypic/123.jpg
@ -163,7 +164,7 @@ func Convert(ImgPath string, AllowedTypes []string, QUALITY string) func(c *fibe
return
}
cwd, WebpAbsPath := genWebpAbs(RawImageAbs, ImgFilename, reqURI)
cwd, WebpAbsPath := genWebpAbs(RawImageAbs, ExhaustPath, ImgFilename, reqURI)
if imageExists(WebpAbsPath) {
finalFile = WebpAbsPath
@ -214,7 +215,7 @@ func fileCount(dir string) int {
return count
}
func genWebpAbs(RawImagePath string, ImgFilename string, reqURI string) (string, string) {
func genWebpAbs(RawImagePath string, ExhaustPath string, ImgFilename string, reqURI string) (string, string) {
// get file mod time
STAT, err := os.Stat(RawImagePath)
if err != nil {
@ -226,11 +227,12 @@ func genWebpAbs(RawImagePath string, ImgFilename string, reqURI string) (string,
cwd, _ := os.Getwd()
// /home/webp_server/exhaust/path/to/tsuki.jpg.1582558990.webp
WebpAbsolutePath := path.Clean(path.Join(cwd, "exhaust", path.Dir(reqURI), WebpFilename))
// Custom Exhaust: /path/to/exhaust/web_path/web_to/tsuki.jpg.1582558990.webp
WebpAbsolutePath := path.Clean(path.Join(ExhaustPath, path.Dir(reqURI), WebpFilename))
return cwd, WebpAbsolutePath
}
func prefetchImages(confImgPath 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
@ -250,7 +252,7 @@ func prefetchImages(confImgPath string, QUALITY string) {
}
// RawImagePath string, ImgFilename string, reqURI string
proposedURI := strings.Replace(picAbsPath, confImgPath, "", 1)
_, p2 := genWebpAbs(picAbsPath, info.Name(), proposedURI)
_, p2 := genWebpAbs(picAbsPath, ExhaustPath, info.Name(), proposedURI)
q, _ := strconv.ParseFloat(QUALITY, 32)
_ = os.MkdirAll(path.Dir(p2), 0755)
go webpEncoder(picAbsPath, p2, float32(q), false, finishChan)
@ -276,9 +278,15 @@ func main() {
confImgPath := path.Clean(config.ImgPath)
QUALITY := config.QUALITY
AllowedTypes := config.AllowedTypes
var ExhaustPath string
if len(config.ExhaustPath) == 0 {
ExhaustPath = "./exhaust"
} else {
ExhaustPath = config.ExhaustPath
}
if prefetch {
prefetchImages(confImgPath, QUALITY)
prefetchImages(confImgPath, ExhaustPath, QUALITY)
}
app := fiber.New()
@ -291,7 +299,7 @@ func main() {
ServerInfo := "WebP Server is running at " + ListenAddress
fmt.Println(ServerInfo)
app.Get("/*", Convert(confImgPath, AllowedTypes, QUALITY))
app.Get("/*", Convert(confImgPath, ExhaustPath, AllowedTypes, QUALITY))
app.Listen(ListenAddress)
}