1
1
mirror of https://github.com/go-gitea/gitea.git synced 2026-03-01 19:16:31 +01:00
gitea/routers/common/maintenancemode.go
Nicolas 26d83c932a
Instance-wide (global) info banner and maintenance mode (#36571)
The banner allows site operators to communicate important announcements
(e.g., maintenance windows, policy updates, service notices) directly
within the UI.

The maintenance mode only allows admin to access the web UI.

* Fix #2345
* Fix #9618

---------

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
2026-02-26 23:16:11 +08:00

44 lines
1.2 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package common
import (
"net/http"
"strings"
"code.gitea.io/gitea/modules/setting"
)
func isMaintenanceModeAllowedRequest(req *http.Request) bool {
if strings.HasPrefix(req.URL.Path, "/-/") {
// URLs like "/-/admin", "/-/fetch-redirect" and "/-/markup" are still accessible in maintenance mode
return true
}
if strings.HasPrefix(req.URL.Path, "/api/internal/") {
// internal APIs should be allowed
return true
}
if strings.HasPrefix(req.URL.Path, "/user/") {
// URLs like "/user/signin" and "/user/signup" are still accessible in maintenance mode
return true
}
if strings.HasPrefix(req.URL.Path, "/assets/") {
return true
}
return false
}
func MaintenanceModeHandler() func(h http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
maintenanceMode := setting.Config().Instance.MaintenanceMode.Value(req.Context())
if maintenanceMode.IsActive() && !isMaintenanceModeAllowedRequest(req) {
renderServiceUnavailable(resp, req)
return
}
next.ServeHTTP(resp, req)
})
}
}