8356
Wx
www-xonecas-io / files
www-xonecas-io / internal / server / state.go
3.3 KB
1package server 2 3import ( 4 "encoding/json" 5 "log" 6 "os" 7 "slices" 8 "sync" 9 "time" 10 11 "xonecas.io/site/internal/project" 12 "xonecas.io/site/internal/site" 13) 14 15// state is what the pages read: the shown projects from the data file, and what git says about each one, 16// read once when a page first needs it. site index rewrites the data file after every push, import and 17// remote change, and a newer data file starts a new state. 18type state struct { 19 mod time.Time 20 shown []project.Project 21 rows []site.ProjectRow 22 named map[string]int // index into shown and rows 23 seo map[string]string 24 repos []repoState 25 feed lazy[[]site.Commit] 26} 27 28// repoState is what git says about one shown project. 29type repoState struct { 30 files lazy[files] 31 log lazy[[]site.LogEntry] 32} 33 34type files struct { 35 tree site.Tree 36 kids map[string][]site.TreeEntry 37} 38 39// lazy is a value computed once, on first use. 40type lazy[T any] struct { 41 once sync.Once 42 v T 43 err error 44} 45 46func (l *lazy[T]) get(f func() (T, error)) (T, error) { 47 l.once.Do(func() { l.v, l.err = f() }) 48 return l.v, l.err 49} 50 51// state is the current state, loaded again when the data file has changed. A data file that does not 52// read, such as one site index is still writing, keeps the state there is. 53func (s *Server) state() (*state, error) { 54 fi, err := os.Stat(s.data) 55 s.mu.Lock() 56 defer s.mu.Unlock() 57 if err == nil && s.st != nil && fi.ModTime().Equal(s.st.mod) { 58 return s.st, nil 59 } 60 var st *state 61 if err == nil { 62 st, err = load(s.data, fi.ModTime()) 63 } 64 if err != nil { 65 if s.st != nil { 66 log.Printf("keeping the loaded data: %v", err) 67 return s.st, nil 68 } 69 return nil, err 70 } 71 s.st = st 72 go st.feedOf() // the activity pages read every project's log 73 return st, nil 74} 75 76func load(path string, mod time.Time) (*state, error) { 77 b, err := os.ReadFile(path) 78 if err != nil { 79 return nil, err 80 } 81 var projects []project.Project 82 if err := json.Unmarshal(b, &projects); err != nil { 83 return nil, err 84 } 85 shown := slices.DeleteFunc(projects, func(p project.Project) bool { return p.Hidden != "" }) 86 st := &state{mod: mod, shown: shown, rows: site.Rows(shown), named: map[string]int{}, seo: site.SEO(shown), repos: make([]repoState, len(shown))} 87 for i, p := range shown { 88 st.named[p.Name] = i 89 } 90 return st, nil 91} 92 93// project is the index of the shown project name. 94func (st *state) project(name string) (int, error) { 95 i, ok := st.named[name] 96 if !ok { 97 return 0, site.ErrNotFound 98 } 99 return i, nil 100} 101 102// files is the tree on HEAD of project i, and each folder's entries. 103func (st *state) files(i int) (files, error) { 104 return st.repos[i].files.get(func() (files, error) { 105 t, err := site.ReadTree(st.shown[i].Sources[0].Dir) 106 return files{t, site.Children(st.shown[i].Name, t)}, err 107 }) 108} 109 110// log is every commit of project i, newest first. 111func (st *state) log(i int) ([]site.LogEntry, error) { 112 return st.repos[i].log.get(func() ([]site.LogEntry, error) { return site.ReadLog(st.shown[i]) }) 113} 114 115// feedOf is my commits across the shown projects, newest first. 116func (st *state) feedOf() ([]site.Commit, error) { 117 return st.feed.get(func() ([]site.Commit, error) { 118 logs := make([][]site.LogEntry, len(st.shown)) 119 var err error 120 for i := range st.shown { 121 if logs[i], err = st.log(i); err != nil { 122 return nil, err 123 } 124 } 125 return site.Feed(st.rows, logs), nil 126 }) 127}