miniwol/web/index.go

40 lines
739 B
Go
Raw Normal View History

2022-02-17 12:31:01 +00:00
package web
import (
"bytes"
"html/template"
"net/http"
)
func index(w http.ResponseWriter, r *http.Request) {
// Serve static files
if r.URL.Path != "/" {
fileServer.ServeHTTP(w, r)
return
}
page := struct {
Title string
Content template.HTML
}{}
var contentBuffer bytes.Buffer
sCookie, err := r.Cookie("session")
if err == nil && isAuthenticated(sCookie.Value) == nil {
page.Title = "Miniwol"
2022-02-17 12:54:52 +00:00
contentBuffer.WriteString("<a href='/device'>Device</a>")
2022-02-17 12:31:01 +00:00
} else {
page.Title = "Login"
err = loginTemplate.Execute(&contentBuffer, struct{}{})
if err != nil {
panic(err)
}
}
page.Content = template.HTML(contentBuffer.String())
err = pageTemplate.Execute(w, page)
if err != nil {
panic(err)
}
}