package webcontroller import ( "encoding/json" "html/template" "net/http" "time" "fornaxian.tech/log" "fornaxian.tech/util" ) // TemplateData is the data for the page template, res/template/wrap.html type TemplateData struct { APIEndpoint template.URL Hostname template.HTML CacheID int64 // Describe the site by default, or the file on a filesystem page Title string OGData ogData // Embedded in the page as window.user and window.initial_node, so the JS // doesn't have to request them. Left empty when they were not requested User json.RawMessage Node json.RawMessage } // The cache ID is used to invalidate caches when the webserver is restarted. // It's rounded to a full hour to prevent cached from being emptied too often if // the server is regularly restarted var cacheID = time.Now().Unix() / 3600 func (wc *WebController) newTemplateData(r *http.Request) *TemplateData { return &TemplateData{ APIEndpoint: template.URL(wc.config.APIURLExternal), Hostname: template.HTML(wc.hostname), CacheID: cacheID, Title: "Nova.storage", OGData: siteOGData(r), } } func (wc *WebController) parseTemplate() *template.Template { return template.Must(template.ParseFiles(wc.config.ResourceDir + "/template/wrap.html")) } // renderPage writes the page which loads the Svelte app. In debug mode the // template is parsed on every request, so changes show up without a restart func (wc *WebController) renderPage(w http.ResponseWriter, r *http.Request, td *TemplateData) { var tpl = wc.tpl if wc.config.DebugMode { tpl = wc.parseTemplate() } if r.Method == "HEAD" { return } if err := tpl.Execute(w, td); err != nil && !util.IsNetError(err) { log.Error("Error executing page template: %s", err) } }