Restructure & refactor codebase
This commit is contained in:
@@ -8,133 +8,41 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/glanceapp/glance/internal/assets"
|
||||
"github.com/glanceapp/glance/internal/widget"
|
||||
)
|
||||
|
||||
var buildVersion = "dev"
|
||||
var pageThemeStyleTemplate = mustParseTemplate("theme-style.gotmpl")
|
||||
|
||||
var sequentialWhitespacePattern = regexp.MustCompile(`\s+`)
|
||||
type application struct {
|
||||
Version string
|
||||
Config config
|
||||
ParsedThemeStyle template.HTML
|
||||
|
||||
type Application struct {
|
||||
Version string
|
||||
Config Config
|
||||
slugToPage map[string]*Page
|
||||
widgetByID map[uint64]widget.Widget
|
||||
slugToPage map[string]*page
|
||||
widgetByID map[uint64]widget
|
||||
}
|
||||
|
||||
type Theme struct {
|
||||
BackgroundColor *widget.HSLColorField `yaml:"background-color"`
|
||||
PrimaryColor *widget.HSLColorField `yaml:"primary-color"`
|
||||
PositiveColor *widget.HSLColorField `yaml:"positive-color"`
|
||||
NegativeColor *widget.HSLColorField `yaml:"negative-color"`
|
||||
Light bool `yaml:"light"`
|
||||
ContrastMultiplier float32 `yaml:"contrast-multiplier"`
|
||||
TextSaturationMultiplier float32 `yaml:"text-saturation-multiplier"`
|
||||
CustomCSSFile string `yaml:"custom-css-file"`
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
Host string `yaml:"host"`
|
||||
Port uint16 `yaml:"port"`
|
||||
AssetsPath string `yaml:"assets-path"`
|
||||
BaseURL string `yaml:"base-url"`
|
||||
AssetsHash string `yaml:"-"`
|
||||
StartedAt time.Time `yaml:"-"` // used in custom css file
|
||||
}
|
||||
|
||||
type Branding struct {
|
||||
HideFooter bool `yaml:"hide-footer"`
|
||||
CustomFooter template.HTML `yaml:"custom-footer"`
|
||||
LogoText string `yaml:"logo-text"`
|
||||
LogoURL string `yaml:"logo-url"`
|
||||
FaviconURL string `yaml:"favicon-url"`
|
||||
}
|
||||
|
||||
type Column struct {
|
||||
Size string `yaml:"size"`
|
||||
Widgets widget.Widgets `yaml:"widgets"`
|
||||
}
|
||||
|
||||
type templateData struct {
|
||||
App *Application
|
||||
Page *Page
|
||||
}
|
||||
|
||||
type Page struct {
|
||||
Title string `yaml:"name"`
|
||||
Slug string `yaml:"slug"`
|
||||
Width string `yaml:"width"`
|
||||
ShowMobileHeader bool `yaml:"show-mobile-header"`
|
||||
ExpandMobilePageNavigation bool `yaml:"expand-mobile-page-navigation"`
|
||||
HideDesktopNavigation bool `yaml:"hide-desktop-navigation"`
|
||||
CenterVertically bool `yaml:"center-vertically"`
|
||||
Columns []Column `yaml:"columns"`
|
||||
PrimaryColumnIndex int8 `yaml:"-"`
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func (p *Page) UpdateOutdatedWidgets() {
|
||||
now := time.Now()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
context := context.Background()
|
||||
|
||||
for c := range p.Columns {
|
||||
for w := range p.Columns[c].Widgets {
|
||||
widget := p.Columns[c].Widgets[w]
|
||||
|
||||
if !widget.RequiresUpdate(&now) {
|
||||
continue
|
||||
}
|
||||
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
widget.Update(context)
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
// TODO: fix, currently very simple, lots of uncovered edge cases
|
||||
func titleToSlug(s string) string {
|
||||
s = strings.ToLower(s)
|
||||
s = sequentialWhitespacePattern.ReplaceAllString(s, "-")
|
||||
s = strings.Trim(s, "-")
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func (a *Application) TransformUserDefinedAssetPath(path string) string {
|
||||
if strings.HasPrefix(path, "/assets/") {
|
||||
return a.Config.Server.BaseURL + path
|
||||
}
|
||||
|
||||
return path
|
||||
}
|
||||
|
||||
func newApplication(config *Config) *Application {
|
||||
app := &Application{
|
||||
func newApplication(config *config) (*application, error) {
|
||||
app := &application{
|
||||
Version: buildVersion,
|
||||
Config: *config,
|
||||
slugToPage: make(map[string]*Page),
|
||||
widgetByID: make(map[uint64]widget.Widget),
|
||||
slugToPage: make(map[string]*page),
|
||||
widgetByID: make(map[uint64]widget),
|
||||
}
|
||||
|
||||
app.Config.Server.AssetsHash = assets.PublicFSHash
|
||||
app.slugToPage[""] = &config.Pages[0]
|
||||
|
||||
providers := &widget.Providers{
|
||||
AssetResolver: app.AssetPath,
|
||||
providers := &widgetProviders{
|
||||
assetResolver: app.AssetPath,
|
||||
}
|
||||
|
||||
var err error
|
||||
app.ParsedThemeStyle, err = executeTemplateToHTML(pageThemeStyleTemplate, &app.Config.Theme)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parsing theme style: %v", err)
|
||||
}
|
||||
|
||||
for p := range config.Pages {
|
||||
@@ -156,9 +64,9 @@ func newApplication(config *Config) *Application {
|
||||
|
||||
for w := range column.Widgets {
|
||||
widget := column.Widgets[w]
|
||||
app.widgetByID[widget.GetID()] = widget
|
||||
app.widgetByID[widget.id()] = widget
|
||||
|
||||
widget.SetProviders(providers)
|
||||
widget.setProviders(providers)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -166,34 +74,75 @@ func newApplication(config *Config) *Application {
|
||||
config = &app.Config
|
||||
|
||||
config.Server.BaseURL = strings.TrimRight(config.Server.BaseURL, "/")
|
||||
config.Theme.CustomCSSFile = app.TransformUserDefinedAssetPath(config.Theme.CustomCSSFile)
|
||||
config.Theme.CustomCSSFile = app.transformUserDefinedAssetPath(config.Theme.CustomCSSFile)
|
||||
|
||||
if config.Branding.FaviconURL == "" {
|
||||
config.Branding.FaviconURL = app.AssetPath("favicon.png")
|
||||
} else {
|
||||
config.Branding.FaviconURL = app.TransformUserDefinedAssetPath(config.Branding.FaviconURL)
|
||||
config.Branding.FaviconURL = app.transformUserDefinedAssetPath(config.Branding.FaviconURL)
|
||||
}
|
||||
|
||||
config.Branding.LogoURL = app.TransformUserDefinedAssetPath(config.Branding.LogoURL)
|
||||
config.Branding.LogoURL = app.transformUserDefinedAssetPath(config.Branding.LogoURL)
|
||||
|
||||
return app
|
||||
return app, nil
|
||||
}
|
||||
|
||||
func (a *Application) HandlePageRequest(w http.ResponseWriter, r *http.Request) {
|
||||
func (p *page) updateOutdatedWidgets() {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
now := time.Now()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
context := context.Background()
|
||||
|
||||
for c := range p.Columns {
|
||||
for w := range p.Columns[c].Widgets {
|
||||
widget := p.Columns[c].Widgets[w]
|
||||
|
||||
if !widget.requiresUpdate(&now) {
|
||||
continue
|
||||
}
|
||||
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
widget.update(context)
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func (a *application) transformUserDefinedAssetPath(path string) string {
|
||||
if strings.HasPrefix(path, "/assets/") {
|
||||
return a.Config.Server.BaseURL + path
|
||||
}
|
||||
|
||||
return path
|
||||
}
|
||||
|
||||
type pageTemplateData struct {
|
||||
App *application
|
||||
Page *page
|
||||
}
|
||||
|
||||
func (a *application) handlePageRequest(w http.ResponseWriter, r *http.Request) {
|
||||
page, exists := a.slugToPage[r.PathValue("page")]
|
||||
|
||||
if !exists {
|
||||
a.HandleNotFound(w, r)
|
||||
a.handleNotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
pageData := templateData{
|
||||
pageData := pageTemplateData{
|
||||
Page: page,
|
||||
App: a,
|
||||
}
|
||||
|
||||
var responseBytes bytes.Buffer
|
||||
err := assets.PageTemplate.Execute(&responseBytes, pageData)
|
||||
err := pageTemplate.Execute(&responseBytes, pageData)
|
||||
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
@@ -204,24 +153,22 @@ func (a *Application) HandlePageRequest(w http.ResponseWriter, r *http.Request)
|
||||
w.Write(responseBytes.Bytes())
|
||||
}
|
||||
|
||||
func (a *Application) HandlePageContentRequest(w http.ResponseWriter, r *http.Request) {
|
||||
func (a *application) handlePageContentRequest(w http.ResponseWriter, r *http.Request) {
|
||||
page, exists := a.slugToPage[r.PathValue("page")]
|
||||
|
||||
if !exists {
|
||||
a.HandleNotFound(w, r)
|
||||
a.handleNotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
pageData := templateData{
|
||||
pageData := pageTemplateData{
|
||||
Page: page,
|
||||
}
|
||||
|
||||
page.mu.Lock()
|
||||
defer page.mu.Unlock()
|
||||
page.UpdateOutdatedWidgets()
|
||||
page.updateOutdatedWidgets()
|
||||
|
||||
var responseBytes bytes.Buffer
|
||||
err := assets.PageContentTemplate.Execute(&responseBytes, pageData)
|
||||
err := pageContentTemplate.Execute(&responseBytes, pageData)
|
||||
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
@@ -232,69 +179,59 @@ func (a *Application) HandlePageContentRequest(w http.ResponseWriter, r *http.Re
|
||||
w.Write(responseBytes.Bytes())
|
||||
}
|
||||
|
||||
func (a *Application) HandleNotFound(w http.ResponseWriter, r *http.Request) {
|
||||
func (a *application) handleNotFound(w http.ResponseWriter, _ *http.Request) {
|
||||
// TODO: add proper not found page
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte("Page not found"))
|
||||
}
|
||||
|
||||
func FileServerWithCache(fs http.FileSystem, cacheDuration time.Duration) http.Handler {
|
||||
server := http.FileServer(fs)
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// TODO: fix always setting cache control even if the file doesn't exist
|
||||
w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%d", int(cacheDuration.Seconds())))
|
||||
server.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func (a *Application) HandleWidgetRequest(w http.ResponseWriter, r *http.Request) {
|
||||
func (a *application) handleWidgetRequest(w http.ResponseWriter, r *http.Request) {
|
||||
widgetValue := r.PathValue("widget")
|
||||
|
||||
widgetID, err := strconv.ParseUint(widgetValue, 10, 64)
|
||||
|
||||
if err != nil {
|
||||
a.HandleNotFound(w, r)
|
||||
a.handleNotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
widget, exists := a.widgetByID[widgetID]
|
||||
|
||||
if !exists {
|
||||
a.HandleNotFound(w, r)
|
||||
a.handleNotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
widget.HandleRequest(w, r)
|
||||
widget.handleRequest(w, r)
|
||||
}
|
||||
|
||||
func (a *Application) AssetPath(asset string) string {
|
||||
return a.Config.Server.BaseURL + "/static/" + a.Config.Server.AssetsHash + "/" + asset
|
||||
func (a *application) AssetPath(asset string) string {
|
||||
return a.Config.Server.BaseURL + "/static/" + staticFSHash + "/" + asset
|
||||
}
|
||||
|
||||
func (a *Application) Server() (func() error, func() error) {
|
||||
func (a *application) server() (func() error, func() error) {
|
||||
// TODO: add gzip support, static files must have their gzipped contents cached
|
||||
// TODO: add HTTPS support
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("GET /{$}", a.HandlePageRequest)
|
||||
mux.HandleFunc("GET /{page}", a.HandlePageRequest)
|
||||
mux.HandleFunc("GET /{$}", a.handlePageRequest)
|
||||
mux.HandleFunc("GET /{page}", a.handlePageRequest)
|
||||
|
||||
mux.HandleFunc("GET /api/pages/{page}/content/{$}", a.HandlePageContentRequest)
|
||||
mux.HandleFunc("/api/widgets/{widget}/{path...}", a.HandleWidgetRequest)
|
||||
mux.HandleFunc("GET /api/pages/{page}/content/{$}", a.handlePageContentRequest)
|
||||
mux.HandleFunc("/api/widgets/{widget}/{path...}", a.handleWidgetRequest)
|
||||
mux.HandleFunc("GET /api/healthz", func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
mux.Handle(
|
||||
fmt.Sprintf("GET /static/%s/{path...}", a.Config.Server.AssetsHash),
|
||||
http.StripPrefix("/static/"+a.Config.Server.AssetsHash, FileServerWithCache(http.FS(assets.PublicFS), 24*time.Hour)),
|
||||
fmt.Sprintf("GET /static/%s/{path...}", staticFSHash),
|
||||
http.StripPrefix("/static/"+staticFSHash, fileServerWithCache(http.FS(staticFS), 24*time.Hour)),
|
||||
)
|
||||
|
||||
var absAssetsPath string
|
||||
if a.Config.Server.AssetsPath != "" {
|
||||
absAssetsPath, _ = filepath.Abs(a.Config.Server.AssetsPath)
|
||||
assetsFS := FileServerWithCache(http.Dir(a.Config.Server.AssetsPath), 2*time.Hour)
|
||||
assetsFS := fileServerWithCache(http.Dir(a.Config.Server.AssetsPath), 2*time.Hour)
|
||||
mux.Handle("/assets/{path...}", http.StripPrefix("/assets/", assetsFS))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user