BlueGit

timfa bluegit

Files

bluegit / internal / server / setup.go

setup.go

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 v.Page = "setup"
140 if problem != "" {
141 v.Err = problem
142 }
143 v.MinFree = state.Format(state.MinFreeBytes)
144 v.StoragePath = strings.TrimSpace(prefill)
145 if v.StoragePath == "" {
146 v.StoragePath = s.StoragePath()
147 }
148 if v.StoragePath == "" && s.StateDir != "" {
149 if saved, err := state.Read(s.StateDir); err == nil {
150 v.StoragePath = saved
151 }
152 }
153 if v.StoragePath != "" {
154 info, err := state.Inspect(v.StoragePath)
155 if info.Total > 0 {
156 v.StorageFree = state.Format(info.Free)
157 v.StorageTotal = state.Format(info.Total)
158 }
159 if err != nil && v.Err == "" {
160 v.Err = s.trErr(r, err)
161 }
162 }
163 s.render(w, "setup", v)
164}
165
166// RefreshHooks zet de pre-receive-hook opnieuw in elke repository die op de datadisk staat.
167func (s *Server) RefreshHooks() {
168 root := s.StoragePath()
169 if root == "" || s.HookBin == "" || s.Store == nil {
170 return
171 }
172 repos, err := s.Store.AllRepos(context.Background())
173 if err != nil {
174 return
175 }
176 for _, repo := range repos {
177 dir := gitrepo.Dir(root, repo.OwnerPath, repo.Name)
178 if _, err := os.Stat(dir); err != nil {
179 continue
180 }
181 if err := gitrepo.InstallHook(dir, s.HookBin); err != nil && s.Log != nil {
182 s.Log.Error("hook", "repo", repo.FullPath(), "err", err)
183 }
184 }
185}