40 lines
739 B
Go
40 lines
739 B
Go
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"
|
|
contentBuffer.WriteString("<a href='/device'>Device</a>")
|
|
} 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)
|
|
}
|
|
}
|