BlueGit

timfa bluegit

Files

bluegit / internal / server / setup.go

setup.go

feat: nieuw-formulier, groepsoverzicht, logo's en talen
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Failed d1203d3b History
setup.go 4.7 KiB Download Raw
1package server
2
3import (
4 "context"
5 "net/http"
6 "os"
7 "strings"
8
9 "gitlab.com/Timfa/bluegit/internal/gitrepo"
10 "gitlab.com/Timfa/bluegit/internal/state"
11)
12
13func (s *Server) noStorageText(r *http.Request) string {
14 return s.tr(r, "err.noStorage") + "\n"
15}
16
17func allowsWithoutStorage(r *http.Request) bool {
18 switch r.URL.Path {
19 case "/healthz", "/login", "/auth/callback", "/logout", "/setup":
20 return true
21 }
22 return strings.HasPrefix(r.URL.Path, "/assets/")
23}
24
25func isGitPath(path string) bool {
26 return strings.HasSuffix(path, "/info/refs") ||
27 strings.HasSuffix(path, "/git-upload-pack") ||
28 strings.HasSuffix(path, "/git-receive-pack")
29}
30
31func (s *Server) gate(next http.Handler) http.Handler {
32 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
33 if allowsWithoutStorage(r) || s.StoragePath() != "" {
34 next.ServeHTTP(w, r)
35 return
36 }
37 if isGitPath(r.URL.Path) || strings.HasPrefix(r.URL.Path, "/api/") {
38 http.Error(w, s.noStorageText(r), http.StatusServiceUnavailable)
39 return
40 }
41 if p := s.who(r); p.OK && p.User.InstanceAdmin {
42 http.Redirect(w, r, "/setup", http.StatusFound)
43 return
44 }
45 next.ServeHTTP(w, r)
46 })
47}
48
49// StoragePath is de datadisk. Leeg zolang de beheerder er geen heeft gekozen.
50func (s *Server) StoragePath() string {
51 s.storageMu.RLock()
52 set, path := s.storageSet, s.storage
53 s.storageMu.RUnlock()
54 if set {
55 return path
56 }
57 s.storageMu.Lock()
58 defer s.storageMu.Unlock()
59 if !s.storageSet {
60 s.storage = s.Cfg.StoragePath
61 s.storageSet = true
62 }
63 return s.storage
64}
65
66// AdoptStorage zet het pad dat bij het starten al geldig was.
67func (s *Server) AdoptStorage(path string) {
68 s.storageMu.Lock()
69 s.storage = path
70 s.storageSet = true
71 s.storageMu.Unlock()
72 if path != "" && s.AfterStorage != nil {
73 s.AfterStorage(path)
74 }
75}
76
77func (s *Server) setupPage(w http.ResponseWriter, r *http.Request) {
78 if _, ok := s.adminOnly(w, r); !ok {
79 return
80 }
81 s.renderSetup(w, r, "", "")
82}
83
84func (s *Server) setupSave(w http.ResponseWriter, r *http.Request) {
85 if _, ok := s.adminOnly(w, r); !ok || !s.csrfOK(w, r) {
86 return
87 }
88 if s.StateDir == "" {
89 s.renderSetup(w, r, s.tr(r, "err.storageSave"), r.FormValue("path"))
90 return
91 }
92 info, err := state.Inspect(r.FormValue("path"))
93 if err != nil {
94 s.renderSetup(w, r, s.trErr(r, err), strings.TrimSpace(r.FormValue("path")))
95 return
96 }
97 if same, err := samePath(s.StateDir, info.Path); err != nil {
98 s.renderSetup(w, r, s.trErr(r, err), info.Path)
99 return
100 } else if same {
101 s.renderSetup(w, r, s.tr(r, "err.storageSame"), info.Path)
102 return
103 }
104 previous := s.StoragePath()
105 if err := state.Write(s.StateDir, info.Path); err != nil {
106 s.renderSetup(w, r, s.trErr(r, err), info.Path)
107 return
108 }
109 s.AdoptStorage(info.Path)
110 s.RefreshHooks()
111 notice := s.tr(r, "err.storageSet", info.Path, state.Format(info.Free))
112 if previous != "" && previous != info.Path {
113 notice = s.tr(r, "err.storageMoved", info.Path, state.Format(info.Free))
114 }
115 s.redirect(w, r, "/", notice, "")
116}
117
118func samePath(stateDir, path string) (bool, error) {
119 if stateDir == "" {
120 return false, nil
121 }
122 return strings.TrimRight(stateDir, "/") == strings.TrimRight(path, "/"), nil
123}
124
125func (s *Server) adminOnly(w http.ResponseWriter, r *http.Request) (bool, bool) {
126 u, ok := s.loginRequired(w, r)
127 if !ok {
128 return false, false
129 }
130 if !u.InstanceAdmin {
131 http.Error(w, s.tr(r, "err.noStorageAdmin"), http.StatusForbidden)
132 return false, false
133 }
134 return true, true
135}
136
137func (s *Server) renderSetup(w http.ResponseWriter, r *http.Request, problem, prefill string) {
138 v := s.view(w, r, "title.storage")
139 if problem != "" {
140 v.Err = problem
141 }
142 v.MinFree = state.Format(state.MinFreeBytes)
143 v.StoragePath = strings.TrimSpace(prefill)
144 if v.StoragePath == "" {
145 v.StoragePath = s.StoragePath()
146 }
147 if v.StoragePath == "" && s.StateDir != "" {
148 if saved, err := state.Read(s.StateDir); err == nil {
149 v.StoragePath = saved
150 }
151 }
152 if v.StoragePath != "" {
153 info, err := state.Inspect(v.StoragePath)
154 if info.Total > 0 {
155 v.StorageFree = state.Format(info.Free)
156 v.StorageTotal = state.Format(info.Total)
157 }
158 if err != nil && v.Err == "" {
159 v.Err = s.trErr(r, err)
160 }
161 }
162 s.render(w, "setup", v)
163}
164
165// RefreshHooks zet de pre-receive-hook opnieuw in elke repository die op de datadisk staat.
166func (s *Server) RefreshHooks() {
167 root := s.StoragePath()
168 if root == "" || s.HookBin == "" || s.Store == nil {
169 return
170 }
171 repos, err := s.Store.AllRepos(context.Background())
172 if err != nil {
173 return
174 }
175 for _, repo := range repos {
176 dir := gitrepo.Dir(root, repo.OwnerPath, repo.Name)
177 if _, err := os.Stat(dir); err != nil {
178 continue
179 }
180 if err := gitrepo.InstallHook(dir, s.HookBin); err != nil && s.Log != nil {
181 s.Log.Error("hook", "repo", repo.FullPath(), "err", err)
182 }
183 }
184}