show pages

This commit is contained in:
Cadey Ratio 2019-11-19 17:25:17 +00:00
parent 7497950a7a
commit ed7eab99f8
4 changed files with 64 additions and 2 deletions

58
html.go
View File

@ -1,8 +1,8 @@
package main
import (
"html/template"
"net/http"
"text/template"
)
func showRegisterForm(w http.ResponseWriter, r *http.Request) {
@ -22,3 +22,59 @@ func showLoginForm(w http.ResponseWriter, r *http.Request) {
t.Execute(w, nil)
}
func showPage(w http.ResponseWriter, tmpl string, loggedIn bool, data interface{}) error {
t, err := template.ParseFiles("tmpl/base.html", "tmpl/"+tmpl+".html")
if err != nil {
return err
}
_ = t.Execute(w, struct {
LoggedIn bool
Data interface{}
}{
LoggedIn: loggedIn,
Data: data,
})
return nil
}
type wasmcloudHandler func(http.ResponseWriter, *http.Request, *User)
func makeHandler(wantAuth bool, fn wasmcloudHandler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("wasmcloud-token")
if err != nil {
if wantAuth {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
fn(w, r, nil)
return
}
u, _, err := getUserAndToken(cookie.Value)
if err != nil {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
fn(w, r, &u)
}
}
func authenticatedShowAPage(name string) http.HandlerFunc {
return makeHandler(true, func(w http.ResponseWriter, r *http.Request, u *User) {
showPage(w, name, true, u)
})
}
func unauthenticatedShowAPage(name string) http.HandlerFunc {
return makeHandler(false, func(w http.ResponseWriter, r *http.Request, u *User) {
loggedIn := u != nil
showPage(w, name, loggedIn, u)
})
}

View File

@ -43,6 +43,10 @@ func main() {
rtr.HandleFunc("/login", loginUser).Methods("POST")
rtr.HandleFunc("/logout", logoutUser)
// pages
rtr.HandleFunc("/", unauthenticatedShowAPage("index"))
rtr.HandleFunc("/control/", authenticatedShowAPage("controlindex"))
rtr.PathPrefix("/static/").Handler(http.FileServer(http.Dir(".")))
log.Printf("listening on http://wasmcloud.kahless.cetacean.club:%s", *port)

View File

@ -8,7 +8,7 @@
<body id="top">
<main>
<header>
<p><a href="/">wasmcloud</a> - <a href="docs.wasmcloud.within.website">Documentation</a> - <a href="/support">Support</a> - {{ if .LoggedIn }} <a href="/control">Control Panel</a> {{ else }} <a href="/login">Login</a> {{ end }}</p>
<p><a href="/">wasmcloud</a> - <a href="docs.wasmcloud.within.website">Documentation</a> - <a href="/support">Support</a> - {{ if .LoggedIn }} <a href="/control/">Control Panel</a> {{ else }} <a href="/login">Login</a> {{ end }}</p>
</header>
{{ template "content" . }}
<footer>

View File

@ -6,4 +6,6 @@
<h1>Control Panel</h1>
<big>TODO(cadey): all of this</big>
<p>logged in as {{ .Data.Username }}</p>
{{ end }}