Skip to content

Commit

Permalink
add ddnupdater new version notification
Browse files Browse the repository at this point in the history
  • Loading branch information
eshirvana committed Dec 29, 2024
1 parent da5b994 commit fd336c9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
8 changes: 5 additions & 3 deletions internal/models/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package models
// HTMLData is a list of HTML fields to be rendered.
// It is exported so that the HTML template engine can render it.
type HTMLData struct {
Rows []HTMLRow
Version string
Rows []HTMLRow
Version string
UpdateAvailable bool
LatestVersion string
}

// HTMLRow contains HTML fields to be rendered
Expand All @@ -17,4 +19,4 @@ type HTMLRow struct {
Status string
CurrentIP string
PreviousIPs string
}
}
15 changes: 14 additions & 1 deletion internal/server/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@ func (h *handlers) index(w http.ResponseWriter, _ *http.Request) {
row := record.HTML(h.timeNow())
htmlData.Rows = append(htmlData.Rows, row)
}
err := h.indexTemplate.ExecuteTemplate(w, "index.html", htmlData)

currentVersion := getCurrentVersion()
latestVersion, err := getLatestRelease()
if err != nil {
httpError(w, http.StatusInternalServerError, "failed getting latest release: "+err.Error())
return
}
htmlData.Version = currentVersion
if currentVersion != latestVersion {
htmlData.UpdateAvailable = true
htmlData.LatestVersion = latestVersion
}

err = h.indexTemplate.ExecuteTemplate(w, "index.html", htmlData)
if err != nil {
httpError(w, http.StatusInternalServerError, "failed generating webpage: "+err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion internal/server/ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
d="M8 0c4.42 0 8 3.58 8 8a8.013 8.013 0 0 1-5.45 7.59c-.4.08-.55-.17-.55-.38 0-.27.01-1.13.01-2.2 0-.75-.25-1.23-.54-1.48 1.78-.2 3.65-.88 3.65-3.95 0-.88-.31-1.59-.82-2.15.08-.2.36-1.02-.08-2.12 0 0-.67-.22-2.2.82-.64-.18-1.32-.27-2-.27-.68 0-1.36.09-2 .27-1.53-1.03-2.2-.82-2.2-.82-.44 1.1-.16 1.92-.08 2.12-.51.56-.82 1.28-.82 2.15 0 3.06 1.86 3.75 3.64 3.95-.23.2-.44.55-.51 1.07-.46.21-1.61.55-2.33-.66-.15-.24-.6-.83-1.23-.82-.67.01-.27.38.01.53.34.19.73.9.82 1.13.16.45.68 1.31 2.69.94 0 .67.01 1.3.01 1.49 0 .21-.15.45-.55.38A7.995 7.995 0 0 1 0 8c0-4.42 3.58-8 8-8Z">
</path>
</svg>
<p>Version: {{.Version}}</p>
</a>
</div>
<div>Current Version: {{.Version}} {{if .UpdateAvailable}} Update Available: {{.LatestVersion}}{{end}}</div>
<div>by <a href="https://github.com/qdm12">Quentin McGaw</a> / UI reworked by <a
href="https://github.com/fuse314">Gottfried Mayer</a></div>
</footer>
Expand Down
34 changes: 20 additions & 14 deletions internal/server/version.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
package server

import (
"encoding/json"
"net/http"
"encoding/json"
"net/http"
)

type GitHubRelease struct {
TagName string `json:"tag_name"`
TagName string `json:"tag_name"`
}

func getLatestRelease() (string, error) {
resp, err := http.Get("https://api.github.com/repos/qdm12/ddns-updater/releases/latest")
if err != nil {
return "", err
}
defer resp.Body.Close()
resp, err := http.Get("https://api.github.com/repos/qdm12/ddns-updater/releases/latest")
if err != nil {
return "", err
}
defer resp.Body.Close()

var release GitHubRelease
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
return "", err
}
var release GitHubRelease
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
return "", err
}

return release.TagName, nil
}
return release.TagName, nil
}

// Add a function to get the current version
func getCurrentVersion() string {
// Replace with actual logic to get the current version
return "v1.0.0"
}

0 comments on commit fd336c9

Please sign in to comment.