Files
fnx_web/webcontroller/template_data.go

63 lines
1.4 KiB
Go
Raw Normal View History

package webcontroller
import (
2018-06-21 23:41:50 +02:00
"html/template"
"net/http"
2019-02-07 23:09:54 +01:00
"net/url"
2018-06-23 21:17:53 +02:00
"time"
"fornaxian.com/pixeldrain-web/pixelapi"
"github.com/Fornaxian/log"
)
// TemplateData is a struct that every template expects when being rendered. In
// the field Other you can pass your own template-specific variables.
type TemplateData struct {
Authenticated bool
Username string
UserStyle template.CSS
2018-06-21 23:41:50 +02:00
APIEndpoint template.URL
2018-07-09 21:41:17 +02:00
PixelAPI *pixelapi.PixelAPI
2019-02-07 23:09:54 +01:00
Other interface{}
URLQuery url.Values
// Only used on file viewer page
Title string
OGData OGData
}
2018-06-23 21:17:53 +02:00
func (wc *WebController) newTemplateData(w http.ResponseWriter, r *http.Request) *TemplateData {
2018-06-21 23:41:50 +02:00
var t = &TemplateData{
Authenticated: false,
2018-07-08 14:40:20 +02:00
Username: "",
UserStyle: userStyle(r),
2018-06-21 23:41:50 +02:00
APIEndpoint: template.URL(wc.conf.APIURLExternal),
2019-02-07 23:09:54 +01:00
URLQuery: r.URL.Query(),
2018-06-21 23:41:50 +02:00
}
2018-06-23 21:17:53 +02:00
if key, err := wc.getAPIKey(r); err == nil {
2018-07-09 21:41:17 +02:00
t.PixelAPI = pixelapi.New(wc.conf.APIURLInternal, key)
uinf, err := t.PixelAPI.UserInfo()
2018-06-23 21:17:53 +02:00
if err != nil {
// This session key doesn't work, delete it
2018-07-09 21:41:17 +02:00
log.Debug("Session check for key '%s' failed: %s", key, err)
2018-06-23 21:17:53 +02:00
http.SetCookie(w, &http.Cookie{
Name: "pd_auth_key",
Value: "",
Path: "/",
Expires: time.Unix(0, 0),
})
return t
}
// Authentication succeeded
t.Authenticated = true
t.Username = uinf.Username
2018-07-09 21:41:17 +02:00
} else {
t.PixelAPI = pixelapi.New(wc.conf.APIURLInternal, "")
2018-06-23 21:17:53 +02:00
}
return t
}