Skip to content

Commit

Permalink
Cleanup code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
NHAS committed Nov 17, 2024
1 parent 116cb65 commit 9c65626
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 398 deletions.
140 changes: 0 additions & 140 deletions adminui/clustering.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"encoding/json"
"log"
"net/http"
"time"

"github.com/NHAS/wag/internal/data"
"go.etcd.io/etcd/client/pkg/v3/types"
"go.etcd.io/etcd/server/v3/etcdserver/api/membership"
)

Expand All @@ -21,102 +19,6 @@ type MembershipDTO struct {
Status string
}

func (au *AdminUI) clusterMembersUI(w http.ResponseWriter, r *http.Request) {
_, u := au.sessionManager.GetSessionFromRequest(r)
if u == nil {
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
return
}

d := struct {
Page
Members []MembershipDTO
Leader types.ID
CurrentNode string
}{
Page: Page{

Description: "Clustering Management Page",
Title: "Clustering",
},

Leader: data.GetLeader(),
CurrentNode: data.GetServerID().String(),
}

members := data.GetMembers()
for i := range data.GetMembers() {
drained, err := data.IsDrained(members[i].ID.String())
if err != nil {
log.Println("unable to get drained state: ", err)
w.WriteHeader(http.StatusInternalServerError)
return
}

witness, err := data.IsWitness(members[i].ID.String())
if err != nil {
log.Println("unable to witness state: ", err)
w.WriteHeader(http.StatusInternalServerError)
return
}

version, err := data.GetVersion(members[i].ID.String())
if err != nil {
log.Println("unable to get version: ", err)
version = "unknown"
}

status := "healthy" // full liveness
if drained {
status = "drained"
} else if !members[i].IsStarted() {
status = "wait for first connection..."
} else if members[i].IsLearner {
status = "learner"
}

ping := ""
if status != "learner" {
lastPing, err := data.GetLastPing(members[i].ID.String())
if err != nil {
log.Println("unable to fetch last ping: ", err)
status = "no last ping"
} else {

if lastPing.Before(time.Now().Add(-6 * time.Second)) {
status += "(lagging ping)"
}

if lastPing.Before(time.Now().Add(-14 * time.Second)) {
status = "dead"
}

ping = lastPing.Format(time.RFC822)
}
}

d.Members = append(d.Members, MembershipDTO{
Member: members[i],
IsDrained: drained,
IsWitness: witness,
Status: status,
Ping: ping,
Version: version,
})

}

err := au.renderDefaults(w, r, d, "cluster/members.html", "delete_modal.html")

if err != nil {
log.Println("unable to render clustering page: ", err)

w.WriteHeader(http.StatusInternalServerError)
au.renderDefaults(w, r, nil, "error.html")
return
}
}

func (au *AdminUI) newNode(w http.ResponseWriter, r *http.Request) {
var newNodeReq data.NewNodeRequest
err := json.NewDecoder(r.Body).Decode(&newNodeReq)
Expand Down Expand Up @@ -204,48 +106,6 @@ func (au *AdminUI) nodeControl(w http.ResponseWriter, r *http.Request) {

}

func (au *AdminUI) clusterEventsUI(w http.ResponseWriter, r *http.Request) {
_, u := au.sessionManager.GetSessionFromRequest(r)
if u == nil {
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
return
}

d := struct {
Page
EventLog []string
Errors []data.EventError
}{
Page: Page{

Description: "Clustering Management Page",
Title: "Clustering",
},

EventLog: data.EventsQueue.ReadAll(),
}

var err error
d.Errors, err = data.GetAllErrors()
if err != nil {
log.Println("unable to render clustering events page: ", err)

w.WriteHeader(http.StatusInternalServerError)
au.renderDefaults(w, r, nil, "error.html")
return
}

err = au.renderDefaults(w, r, d, "cluster/events.html", "delete_modal.html")

if err != nil {
log.Println("unable to render clustering events page: ", err)

w.WriteHeader(http.StatusInternalServerError)
au.renderDefaults(w, r, nil, "error.html")
return
}
}

func (au *AdminUI) clusterEventsAcknowledge(w http.ResponseWriter, r *http.Request) {

var acknowledgeError struct {
Expand Down
25 changes: 0 additions & 25 deletions adminui/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,6 @@ import (
"net/http"
)

func (au *AdminUI) devicesMgmtUI(w http.ResponseWriter, r *http.Request) {

_, u := au.sessionManager.GetSessionFromRequest(r)
if u == nil {
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
return
}

d := Page{

Description: "Devices Management Page",
Title: "Devices",
}

err := au.renderDefaults(w, r, d, "management/devices.html", "delete_modal.html")

if err != nil {
log.Println("unable to render devices page: ", err)

w.WriteHeader(http.StatusInternalServerError)
au.renderDefaults(w, r, nil, "error.html")
return
}
}

func (au *AdminUI) getAllDevices(w http.ResponseWriter, r *http.Request) {
allDevices, err := au.ctrl.ListDevice("")
if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions adminui/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func (au *AdminUI) getAllGroups(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Content-Type", "application/json")
w.Write(b)
return
}

func (au *AdminUI) editGroup(w http.ResponseWriter, r *http.Request) {
var group control.GroupData
err := json.NewDecoder(r.Body).Decode(&group)
Expand All @@ -42,8 +42,8 @@ func (au *AdminUI) editGroup(w http.ResponseWriter, r *http.Request) {
}

w.Write([]byte("OK"))
return
}

func (au *AdminUI) createGroup(w http.ResponseWriter, r *http.Request) {
var group control.GroupData
err := json.NewDecoder(r.Body).Decode(&group)
Expand All @@ -62,8 +62,8 @@ func (au *AdminUI) createGroup(w http.ResponseWriter, r *http.Request) {
}

w.Write([]byte("OK"))
return
}

func (au *AdminUI) deleteGroups(w http.ResponseWriter, r *http.Request) {
var groupsToRemove []string
err := json.NewDecoder(r.Body).Decode(&groupsToRemove)
Expand All @@ -80,5 +80,4 @@ func (au *AdminUI) deleteGroups(w http.ResponseWriter, r *http.Request) {
}

w.Write([]byte("OK"))
return
}
4 changes: 0 additions & 4 deletions adminui/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func (au *AdminUI) getAllPolicies(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Content-Type", "application/json")
w.Write(b)
return
}
func (au *AdminUI) editPolicy(w http.ResponseWriter, r *http.Request) {
var group control.PolicyData
Expand All @@ -45,7 +44,6 @@ func (au *AdminUI) editPolicy(w http.ResponseWriter, r *http.Request) {
}

w.Write([]byte("OK"))
return
}
func (au *AdminUI) createPolicy(w http.ResponseWriter, r *http.Request) {
var policy control.PolicyData
Expand All @@ -65,7 +63,6 @@ func (au *AdminUI) createPolicy(w http.ResponseWriter, r *http.Request) {
}

w.Write([]byte("OK"))
return
}

func (au *AdminUI) deletePolices(w http.ResponseWriter, r *http.Request) {
Expand All @@ -85,5 +82,4 @@ func (au *AdminUI) deletePolices(w http.ResponseWriter, r *http.Request) {
}

w.Write([]byte("OK"))
return
}
27 changes: 0 additions & 27 deletions adminui/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,6 @@ import (
"strings"
)

func (au *AdminUI) registrationUI(w http.ResponseWriter, r *http.Request) {
_, u := au.sessionManager.GetSessionFromRequest(r)
if u == nil {
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
return
}

d := Page{

Description: "Registration Tokens Management Page",
Title: "Registration",
}

err := au.renderDefaults(w, r, d, "management/registration_tokens.html", "delete_modal.html")
if err != nil {
log.Println("unable to render registration_tokens page: ", err)

w.WriteHeader(http.StatusInternalServerError)
au.renderDefaults(w, r, nil, "error.html")
return
}
}

func (au *AdminUI) getAllRegistrationTokens(w http.ResponseWriter, r *http.Request) {
registrations, err := au.ctrl.Registrations()
if err != nil {
Expand Down Expand Up @@ -61,8 +38,6 @@ func (au *AdminUI) getAllRegistrationTokens(w http.ResponseWriter, r *http.Reque

w.Header().Set("Content-Type", "application/json")
w.Write(b)
return

}

func (au *AdminUI) createRegistrationToken(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -109,7 +84,6 @@ func (au *AdminUI) createRegistrationToken(w http.ResponseWriter, r *http.Reques
}

w.Write([]byte("OK"))
return
}

func (au *AdminUI) deleteRegistrationTokens(w http.ResponseWriter, r *http.Request) {
Expand All @@ -136,5 +110,4 @@ func (au *AdminUI) deleteRegistrationTokens(w http.ResponseWriter, r *http.Reque
}

w.Write([]byte("OK"))
return
}
65 changes: 0 additions & 65 deletions adminui/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,8 @@ import (
"net/http"

"github.com/NHAS/wag/internal/data"
"github.com/NHAS/wag/internal/mfaportal/authenticators"
)

func (au *AdminUI) adminUsersUI(w http.ResponseWriter, r *http.Request) {
_, u := au.sessionManager.GetSessionFromRequest(r)
if u == nil {
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
return
}

d := Page{
Description: "Wag settings",
Title: "Settings - Admin Users",
}

err := au.renderDefaults(w, r, d, "settings/management_users.html")

if err != nil {
log.Println("unable to render management_users: ", err)

w.WriteHeader(http.StatusInternalServerError)
au.renderDefaults(w, r, nil, "error.html")
return
}
}

func (au *AdminUI) adminUsersData(w http.ResponseWriter, r *http.Request) {
adminUsers, err := au.ctrl.ListAdminUsers("")
if err != nil {
Expand All @@ -51,47 +27,6 @@ func (au *AdminUI) adminUsersData(w http.ResponseWriter, r *http.Request) {
w.Write(b)
}

func (au *AdminUI) generalSettingsUI(w http.ResponseWriter, r *http.Request) {
_, u := au.sessionManager.GetSessionFromRequest(r)
if u == nil {
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
return
}

datastoreSettings, err := data.GetAllSettings()
if err != nil {
log.Println("could not get settings from datastore: ", err)

w.WriteHeader(http.StatusInternalServerError)
au.renderDefaults(w, r, nil, "error.html")
return
}

d := struct {
Page
Settings data.AllSettings
MFAMethods []authenticators.Authenticator
}{
Page: Page{

Description: "Wag settings",
Title: "Settings - General",
},

Settings: datastoreSettings,
MFAMethods: authenticators.GetAllAvaliableMethods(),
}

err = au.renderDefaults(w, r, d, "settings/general.html")
if err != nil {
log.Println("unable to render general: ", err)

w.WriteHeader(http.StatusInternalServerError)
au.renderDefaults(w, r, nil, "error.html")
return
}
}

func (au *AdminUI) generalSettings(w http.ResponseWriter, r *http.Request) {
_, u := au.sessionManager.GetSessionFromRequest(r)
if u == nil {
Expand Down
Loading

0 comments on commit 9c65626

Please sign in to comment.