From 71569207984b8c72fd2e52260d0920ac00bf1b5f Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Sat, 14 Jul 2018 15:04:24 +0200 Subject: [PATCH] Improve settings page, add logs page --- cmd/eksterd/http.go | 43 +++++++++++++++++++++++++++++++++-- templates/channel.html | 16 +++++++++---- templates/index.html | 3 +++ templates/logs.html | 50 +++++++++++++++++++++++++++++++++++++++++ templates/settings.html | 3 +++ 5 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 templates/logs.html diff --git a/cmd/eksterd/http.go b/cmd/eksterd/http.go index f8c6773..1b08940 100644 --- a/cmd/eksterd/http.go +++ b/cmd/eksterd/http.go @@ -46,6 +46,9 @@ type settingsPage struct { Channels map[string]microsub.Channel Feeds map[string][]microsub.Feed } +type logsPage struct { + Session session +} func newMainHandler(backend *memoryBackend) (*mainHandler, error) { h := &mainHandler{Backend: backend} @@ -141,6 +144,18 @@ func verifyAuthCode(code, redirectURI, authEndpoint string) (bool, *authResponse return false, nil, fmt.Errorf("ERROR: HTTP response code from authorization_endpoint (%s) %d \n", authEndpoint, resp.StatusCode) } +func isLoggedIn(backend *memoryBackend, sess *session) bool { + if !sess.LoggedIn { + return false + } + + if sess.Me != backend.Me { + return false + } + + return true +} + func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { conn := pool.Get() defer conn.Close() @@ -210,7 +225,7 @@ func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { sessionVar := c.Value sess, err := loadSession(sessionVar, conn) - if !sess.LoggedIn { + if !isLoggedIn(h.Backend, &sess) { w.WriteHeader(401) fmt.Fprintf(w, "Unauthorized") return @@ -228,6 +243,30 @@ func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } return + } else if r.URL.Path == "/logs" { + c, err := r.Cookie("session") + if err == http.ErrNoCookie { + http.Redirect(w, r, "/", 302) + return + } + sessionVar := c.Value + sess, err := loadSession(sessionVar, conn) + + if !isLoggedIn(h.Backend, &sess) { + w.WriteHeader(401) + fmt.Fprintf(w, "Unauthorized") + return + } + + var page logsPage + page.Session = sess + + err = h.Templates.ExecuteTemplate(w, "logs.html", page) + if err != nil { + fmt.Fprintf(w, "ERROR: %q\n", err) + return + } + return } else if r.URL.Path == "/settings" { c, err := r.Cookie("session") if err == http.ErrNoCookie { @@ -237,7 +276,7 @@ func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { sessionVar := c.Value sess, err := loadSession(sessionVar, conn) - if !sess.LoggedIn { + if !isLoggedIn(h.Backend, &sess) { w.WriteHeader(401) fmt.Fprintf(w, "Unauthorized") return diff --git a/templates/channel.html b/templates/channel.html index 874ac02..8f6cf3d 100644 --- a/templates/channel.html +++ b/templates/channel.html @@ -29,6 +29,9 @@ Settings + + Logs + Profile @@ -38,17 +41,22 @@

Ekster - Microsub server

- {{ if .Session.LoggedIn }} - {{ end }} + {{ $channel := index .Channels .CurrentChannel }} + -

Channel

+

{{ $channel.Name }}

{{ range index .Feeds .CurrentChannel }} {{ else }} diff --git a/templates/index.html b/templates/index.html index 7a74b96..07fa4e8 100644 --- a/templates/index.html +++ b/templates/index.html @@ -29,6 +29,9 @@ Settings + + Logs + Profile diff --git a/templates/logs.html b/templates/logs.html new file mode 100644 index 0000000..cea2a57 --- /dev/null +++ b/templates/logs.html @@ -0,0 +1,50 @@ + + + + + +Ekster + + + +
+
+ + + + +

Ekster - Microsub server

+ +

Logs

+ +

Logs

+
+
+ + diff --git a/templates/settings.html b/templates/settings.html index 08df622..4beed16 100644 --- a/templates/settings.html +++ b/templates/settings.html @@ -29,6 +29,9 @@ Settings + + Logs + Profile