Skip to content

Commit

Permalink
Add check updates functionality to fix #35
Browse files Browse the repository at this point in the history
  • Loading branch information
NHAS committed May 1, 2023
1 parent dba5d8a commit 4ed4f47
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type Config struct {
path string
Socket string `json:",omitempty"`

CheckVersion bool `json:",omitempty"`
CheckUpdates bool `json:",omitempty"`
Proxied bool
ExposePorts []string `json:",omitempty"`
NAT *bool
Expand Down
63 changes: 63 additions & 0 deletions ui/check_updates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package ui

import (
"encoding/json"
"net/http"
"strings"
"time"

"github.com/NHAS/wag/internal/config"
)

type githubResponse struct {
Body string
Prerelease bool `json:"prerelease"`
TagName string `json:"tag_name"`
Published string `json:"published_at"`
Url string `json:"html_url"`
}

type Update struct {
New bool
UpdateVersion string
Message []string
Url string
Released string
}

var (
mostRecentUpdate *Update
lastChecked time.Time
)

func getUpdate() Update {

if !config.Values().CheckUpdates {
return Update{}
}

if time.Now().After(lastChecked.Add(15*time.Minute)) || mostRecentUpdate == nil {
resp, err := http.Get("https://api.github.com/repos/NHAS/wag/releases/latest")
if err != nil {
return Update{}
}
defer resp.Body.Close()

var gr githubResponse
err = json.NewDecoder(resp.Body).Decode(&gr)
if err != nil {
return Update{}
}

mostRecentUpdate = &Update{
UpdateVersion: gr.TagName,
Message: strings.Split(gr.Body, "\r\n"),
Url: gr.Url,
Released: gr.Published,
}
}

mostRecentUpdate.New = strings.Split(config.Version, "-")[0] != mostRecentUpdate.UpdateVersion

return *mostRecentUpdate
}
1 change: 1 addition & 0 deletions ui/structs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ui

type Page struct {
Update
Description string
Title string
User string
Expand Down
14 changes: 12 additions & 2 deletions ui/templates/menus.html
Original file line number Diff line number Diff line change
Expand Up @@ -170,24 +170,34 @@ <h6 class="collapse-header">Tools:</h6>
<!-- Topbar Navbar -->
<ul class="navbar-nav ml-auto">


{{if .Url}}
<li class="nav-item dropdown no-arrow mx-1">
<a class="nav-link dropdown-toggle" href="#" id="alertsDropdown" role="button"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="icon-bell"></i>
<!-- Counter - Alerts -->
{{if .New}}
<span class="badge badge-danger badge-counter">1</span>
{{end}}
</a>
<!-- Dropdown - Alerts -->
<div class="dropdown-list dropdown-menu dropdown-menu-right shadow animated--grow-in"
aria-labelledby="alertsDropdown">
<a class="dropdown-item d-flex align-items-center" href="#">
<div>
<div class="small text-gray-500">{{.UpdateDate}}</div>
<span class="font-weight-bold">Wag {{.NewVersion}} is now available!</span>
<div class="small text-gray-500">{{.Released}}</div>
<span class="font-weight-bold">{{.UpdateVersion}}</span>
{{range $val := .Message}}
<p>{{$val}}</p>
{{end}}
</div>
</a>
</div>
</li>
{{end}}



<!-- Nav Item - User Information -->
<li class="nav-item dropdown no-arrow">
Expand Down
4 changes: 4 additions & 0 deletions ui/ui_webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ func populateDashboard(w http.ResponseWriter, r *http.Request) {

d := Dashboard{
Page: Page{
Update: getUpdate(),
Description: "Dashboard",
Title: "Dashboard",
User: u.Username,
Expand Down Expand Up @@ -403,6 +404,7 @@ func StartWebServer(errs chan<- error) {
XDPState string
}{
Page: Page{
Update: getUpdate(),
Description: "Firewall state page",
Title: "Firewall",
User: u.Username,
Expand Down Expand Up @@ -599,6 +601,7 @@ func StartWebServer(errs chan<- error) {

d := GeneralSettings{
Page: Page{
Update: getUpdate(),
Description: "Wag settings",
Title: "Settings - General",
User: u.Username,
Expand Down Expand Up @@ -755,6 +758,7 @@ func changePassword(w http.ResponseWriter, r *http.Request) {

d := ChangePassword{
Page: Page{
Update: getUpdate(),
Description: "Change password page",
Title: "Change password",
User: u.Username,
Expand Down

0 comments on commit 4ed4f47

Please sign in to comment.