package server import ( "context" "errors" "net/http" "os" "strings" "time" "gitlab.com/Timfa/bluegit/internal/gitrepo" "gitlab.com/Timfa/bluegit/internal/state" ) // dirUsage is de laatst getelde grootte van de gekozen map. type dirUsage struct { path string n uint64 partial bool at time.Time } const ( usageTTL = time.Minute usageBudget = 2 * time.Second ) // bluegitUsage telt de map, met een cache en een tijdslimiet. // Een grote repository houdt de pagina zo hooguit usageBudget tegen. // partial betekent dat de telling voortijdig stopte. func (s *Server) bluegitUsage(path string) (uint64, bool, bool) { s.usageMu.Lock() cached := s.usage fresh := cached.path == path && !cached.at.IsZero() && time.Since(cached.at) < usageTTL s.usageMu.Unlock() if fresh { return cached.n, cached.partial, true } ctx, cancel := context.WithTimeout(context.Background(), usageBudget) defer cancel() n, err := state.DirSize(ctx, path) partial := errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) if err != nil && !partial { return 0, false, false } s.usageMu.Lock() s.usage = dirUsage{path: path, n: n, partial: partial, at: time.Now()} s.usageMu.Unlock() return n, partial, true } func (s *Server) noStorageText(r *http.Request) string { return s.tr(r, "err.noStorage") + "\n" } func allowsWithoutStorage(r *http.Request) bool { switch r.URL.Path { case "/healthz", "/login", "/auth/callback", "/logout", "/setup", "/-/info", "/api/v1/ci/spec", "/api/v1/ci/presets", "/api/v1/openapi.json", "/api/v1/webhooks/openapi.json", "/api/v1/admin/storage", "/api/v1/admin/self-update": return true } return strings.HasPrefix(r.URL.Path, "/assets/") } func isGitPath(path string) bool { return strings.HasSuffix(path, "/info/refs") || strings.HasSuffix(path, "/git-upload-pack") || strings.HasSuffix(path, "/git-receive-pack") } func (s *Server) gate(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if allowsWithoutStorage(r) || s.StoragePath() != "" { next.ServeHTTP(w, r) return } if isGitPath(r.URL.Path) || strings.HasPrefix(r.URL.Path, "/api/") { http.Error(w, s.noStorageText(r), http.StatusServiceUnavailable) return } if p := s.who(r); p.OK && p.User.InstanceAdmin { http.Redirect(w, r, "/setup", http.StatusFound) return } next.ServeHTTP(w, r) }) } // StoragePath is de datadisk. Leeg zolang de beheerder er geen heeft gekozen. func (s *Server) StoragePath() string { s.storageMu.RLock() set, path := s.storageSet, s.storage s.storageMu.RUnlock() if set { return path } s.storageMu.Lock() defer s.storageMu.Unlock() if !s.storageSet { s.storage = s.Cfg.StoragePath s.storageSet = true } return s.storage } // AdoptStorage zet het pad dat bij het starten al geldig was. func (s *Server) AdoptStorage(path string) { s.storageMu.Lock() s.storage = path s.storageSet = true s.storageMu.Unlock() if path != "" && s.AfterStorage != nil { s.AfterStorage(path) } } func (s *Server) setupPage(w http.ResponseWriter, r *http.Request) { if _, ok := s.adminOnly(w, r); !ok { return } s.renderSetup(w, r, "", "") } func (s *Server) setupSave(w http.ResponseWriter, r *http.Request) { if _, ok := s.adminOnly(w, r); !ok || !s.csrfOK(w, r) { return } info, moved, err := s.opSetStorage(r.FormValue("path")) if err != nil { s.renderSetup(w, r, s.trErr(r, err), strings.TrimSpace(r.FormValue("path"))) return } notice := s.tr(r, "err.storageSet", info.Path, state.Format(info.Free)) if moved { notice = s.tr(r, "err.storageMoved", info.Path, state.Format(info.Free)) } s.redirect(w, r, "/", notice, "") } func samePath(stateDir, path string) (bool, error) { if stateDir == "" { return false, nil } return strings.TrimRight(stateDir, "/") == strings.TrimRight(path, "/"), nil } func (s *Server) adminOnly(w http.ResponseWriter, r *http.Request) (bool, bool) { u, ok := s.loginRequired(w, r) if !ok { return false, false } if !u.InstanceAdmin { http.Error(w, s.tr(r, "err.noStorageAdmin"), http.StatusForbidden) return false, false } return true, true } func (s *Server) renderSetup(w http.ResponseWriter, r *http.Request, problem, prefill string) { v := s.view(w, r, "title.storage") v.Page = "setup" if problem != "" { v.Err = problem } v.MinFree = state.Format(state.MinFreeBytes) v.StoragePath = strings.TrimSpace(prefill) if v.StoragePath == "" { v.StoragePath = s.StoragePath() } if v.StoragePath == "" && s.StateDir != "" { if saved, err := state.Read(s.StateDir); err == nil { v.StoragePath = saved } } if v.StoragePath != "" { info, err := state.Inspect(v.StoragePath) if info.Total > 0 { v.StorageFree = state.Format(info.Free) v.StorageUsed = state.Format(info.Used()) v.StorageTotal = state.Format(info.Total) v.StoragePercent = state.Percent(info.Used(), info.Total) if n, partial, ok := s.bluegitUsage(info.Path); ok { v.StorageBlueGit = state.Format(n) v.StoragePartial = partial } } if err != nil && v.Err == "" { v.Err = s.trErr(r, err) } } s.attachUpdate(&v, s.langOf(r)) s.render(w, "setup", v) } // RefreshHooks zet de pre-receive-hook opnieuw in elke repository die op de datadisk staat. func (s *Server) RefreshHooks() { root := s.StoragePath() if root == "" || s.HookBin == "" || s.Store == nil { return } repos, err := s.Store.AllRepos(context.Background()) if err != nil { return } for _, repo := range repos { dir := gitrepo.Dir(root, repo.OwnerPath, repo.Name) if _, err := os.Stat(dir); err != nil { continue } if err := gitrepo.InstallHook(dir, s.HookBin); err != nil && s.Log != nil { s.Log.Error("hook", "repo", repo.FullPath(), "err", err) } } }