chore: better upgrade

This commit is contained in:
Larvan2
2023-04-02 15:16:42 +08:00
parent 526ac3906d
commit affc453b6e
3 changed files with 47 additions and 32 deletions

View File

@@ -1,12 +1,15 @@
package route
import (
"fmt"
"net/http"
"os"
"github.com/Dreamacro/clash/hub/updater"
"github.com/Dreamacro/clash/log"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
)
func upgradeRouter() http.Handler {
@@ -18,11 +21,24 @@ func upgradeRouter() http.Handler {
func upgrade(w http.ResponseWriter, r *http.Request) {
// modify from https://github.com/AdguardTeam/AdGuardHome/blob/595484e0b3fb4c457f9bb727a6b94faa78a66c5f/internal/home/controlupdate.go#L108
log.Infoln("start update")
err := updater.Update()
execPath, err := os.Executable()
if err != nil {
log.Errorln("err:%s", err)
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, newError(fmt.Sprintf("getting path: %s", err)))
return
}
restart(w, r)
err = updater.Update(execPath)
if err != nil {
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, newError(fmt.Sprintf("Upgrade: %s", err)))
return
}
render.JSON(w, r, render.M{"status": "ok"})
if f, ok := w.(http.Flusher); ok {
f.Flush()
}
go runRestart(execPath)
}