webp_server_go/update.go
Benny a53f0ac44a
check image size (#170)
* check image size

could be useful for #169

* Remove more deprecated ioutil lib, and fix CI problem

* Fix deps

* Write a bigger file for testing

---------

Co-authored-by: n0vad3v <n0vad3v@riseup.net>
2023-02-07 13:39:48 +08:00

60 lines
1.3 KiB
Go

package main
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/staktrace/go-update"
"io"
"net/http"
"runtime"
)
func autoUpdate() {
defer func() {
if err := recover(); err != nil {
log.Errorf("Download error: %s", 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
log.Debugf("Requesting to %s", api)
resp1, _ := http.Get(api)
data1, _ := io.ReadAll(resp1.Body)
_ = json.Unmarshal(data1, &res)
var gitVersion = res.TagName
if gitVersion > version {
log.Infof("Time to update! New version %s found", gitVersion)
} else {
log.Debug("No new version found.")
return
}
var filename = fmt.Sprintf("webp-server-%s-%s", runtime.GOOS, runtime.GOARCH)
if runtime.GOOS == "windows" {
filename += ".exe"
}
log.Info("Downloading binary to update...")
resp, _ := http.Get(releaseUrl + filename)
if resp.StatusCode != 200 {
log.Debugf("%s-%s not found on release.", runtime.GOOS, runtime.GOARCH)
return
}
err := update.Apply(resp.Body, update.Options{})
if err != nil {
// error handling
log.Errorf("Update error. %v", err)
} else {
log.Info("Update complete. Please restart to apply changes.")
}
_ = resp.Body.Close()
}