Merge pull request #19 from webp-sh/autoUpdate

auto update in background
This commit is contained in:
Nova Kwok 2020-03-04 21:02:03 +08:00 committed by GitHub
commit e6ba6d4045
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 1 deletions

View File

@ -30,6 +30,8 @@ It's basically between `ExpressJS` and `Fiber`, much faster than the `http` pack
* `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
Regarding the `IMG_PATH` section in `config.json`.

View File

@ -314,10 +314,56 @@ func prefetchImages(confImgPath string, ExhaustPath string, QUALITY string) {
_, _ = fmt.Fprintf(os.Stdout, "Prefetch completeY(^_^)Y\n\n")
}
func autoUpdate() {
defer func() {
if err := recover(); err != nil {
log.Println("Download error.", err)
}
}()
var api = "https://api.github.com/repos/webp-sh/webp_server_go/releases/latest"
type Result struct {
TagName string `json:"tag_name"`
}
var res Result
resp1, _ := http.Get(api)
data1, _ := ioutil.ReadAll(resp1.Body)
_ = json.Unmarshal(data1, &res)
var gitVersion = res.TagName
if gitVersion > version {
log.Printf("Time to update! New version %s found!", gitVersion)
} else {
log.Println("No new version found.")
return
}
var filename = fmt.Sprintf("webp-server-%s-%s", runtime.GOOS, runtime.GOARCH)
if runtime.GOARCH == "windows" {
filename += ".exe"
}
var releaseUrl = "https://github.com/webp-sh/webp_server_go/releases/latest/download/" + filename
log.Println("Downloading binary...")
resp, _ := http.Get(releaseUrl)
if resp.StatusCode != 200 {
log.Printf("%s-%s not found on release. "+
"Contact developers to supply your version", runtime.GOOS, runtime.GOARCH)
return
}
data, _ := ioutil.ReadAll(resp.Body)
_ = os.Mkdir("update", 0755)
err := ioutil.WriteFile(path.Join("update", filename), data, 0755)
if err == nil {
log.Println("Update complete. Please find your binary from update directory.")
}
_ = resp.Body.Close()
}
func main() {
go autoUpdate()
config := loadConfig(configPath)
HOST := config.HOST
PORT := config.PORT
confImgPath := path.Clean(config.ImgPath)