mirror of
https://github.com/woodchen-ink/webp_server_go.git
synced 2025-07-18 13:42:02 +08:00
Added custom exhaust path. (#16)
* Added custom exhaust path. Bump to 0.0.4. * Fix typo
This commit is contained in:
parent
cb1b48647c
commit
95183ab1c3
@ -33,6 +33,8 @@ 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.
|
||||
@ -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)
|
||||
|
||||
|
@ -3,5 +3,6 @@
|
||||
"PORT": "3333",
|
||||
"QUALITY": "80",
|
||||
"IMG_PATH": "/path/to/pics",
|
||||
"EXHAUST_PATH": "/path/to/exhaust",
|
||||
"ALLOWED_TYPES": ["jpg","png","jpeg"]
|
||||
}
|
||||
|
@ -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.
|
||||
|
||||
|
@ -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,10 @@ func main() {
|
||||
confImgPath := path.Clean(config.ImgPath)
|
||||
QUALITY := config.QUALITY
|
||||
AllowedTypes := config.AllowedTypes
|
||||
ExhaustPath := config.ExhaustPath
|
||||
|
||||
if prefetch {
|
||||
prefetchImages(confImgPath, QUALITY)
|
||||
prefetchImages(confImgPath, ExhaustPath, QUALITY)
|
||||
}
|
||||
|
||||
app := fiber.New()
|
||||
@ -291,7 +294,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)
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user