merge incident

This commit is contained in:
Benny~ 2020-03-05 19:57:25 +08:00
parent 3aa872e0ff
commit d5f7977d97
No known key found for this signature in database
GPG Key ID: 6CD0DBDA5235D481
2 changed files with 61 additions and 0 deletions

60
update.go Normal file
View File

@ -0,0 +1,60 @@
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"runtime"
)
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)
// TODO: checksum
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()
}

View File

@ -79,6 +79,7 @@ func init() {
} }
func main() { func main() {
go autoUpdate()
config := loadConfig(configPath) config := loadConfig(configPath)
HOST := config.HOST HOST := config.HOST