mirror of
https://github.com/Theodor-Springmann-Stiftung/lenz-web.git
synced 2026-03-21 13:55:30 +00:00
Better inital
This commit is contained in:
114
app/app.go
114
app/app.go
@@ -1,27 +1,121 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
gitpkg "github.com/Theodor-Springmann-Stiftung/lenz-web/git"
|
||||
"github.com/Theodor-Springmann-Stiftung/lenz-web/git"
|
||||
"github.com/Theodor-Springmann-Stiftung/lenz-web/xmlmodels"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
mu sync.Mutex
|
||||
lib atomic.Pointer[xmlmodels.Library]
|
||||
mu sync.Mutex
|
||||
lib atomic.Pointer[xmlmodels.Library]
|
||||
repo atomic.Pointer[git.Repo]
|
||||
tmpl *template.Template
|
||||
routes []Route
|
||||
err error
|
||||
}
|
||||
|
||||
func New() *App {
|
||||
return &App{}
|
||||
func New(cfg Config) (*App, error) {
|
||||
a := &App{}
|
||||
if err := a.init(cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
func (a *App) Library() *xmlmodels.Library {
|
||||
return a.lib.Load()
|
||||
}
|
||||
|
||||
func (a *App) RefreshLibrary(dir string, commit *gitpkg.Commit) (*xmlmodels.Library, error) {
|
||||
func (a *App) Repo() *git.Repo {
|
||||
return a.repo.Load()
|
||||
}
|
||||
|
||||
func (a *App) Templates() *template.Template {
|
||||
return a.tmpl
|
||||
}
|
||||
|
||||
func (a *App) Routes() []Route {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
|
||||
ret := make([]Route, len(a.routes))
|
||||
copy(ret, a.routes)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (a *App) RenderPath(path string) ([]byte, error) {
|
||||
a.mu.Lock()
|
||||
var route *Route
|
||||
for i := range a.routes {
|
||||
if a.routes[i].Path == path {
|
||||
r := a.routes[i]
|
||||
route = &r
|
||||
break
|
||||
}
|
||||
}
|
||||
a.mu.Unlock()
|
||||
|
||||
if route == nil || route.page == nil {
|
||||
return nil, fmt.Errorf("no route for path: %s", path)
|
||||
}
|
||||
|
||||
model, err := route.page.Model(a, *route)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return route.page.Render(a, *route, model)
|
||||
}
|
||||
|
||||
func (a *App) init(cfg Config) error {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
|
||||
path := filepath.Join(cfg.BaseDIR, cfg.GITPath)
|
||||
|
||||
repo, err := git.OpenOrClone(path, cfg.GitURL, cfg.GitBranch)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := repo.Pull(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
latest, err := repo.Latest()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
lib, err := xmlmodels.Parse(repo.Path, latest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tmpl, err := parseTemplates()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
a.repo.Store(repo)
|
||||
a.lib.Store(lib)
|
||||
a.tmpl = tmpl
|
||||
|
||||
routes, err := discoverPages(a)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
a.routes = routes
|
||||
a.err = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) RefreshLibrary(dir string, commit *git.Commit) (*xmlmodels.Library, error) {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
|
||||
@@ -31,5 +125,13 @@ func (a *App) RefreshLibrary(dir string, commit *gitpkg.Commit) (*xmlmodels.Libr
|
||||
}
|
||||
|
||||
a.lib.Store(lib)
|
||||
|
||||
routes, err := discoverPages(a)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
a.routes = routes
|
||||
a.err = nil
|
||||
return lib, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user