Files
Jonathan Miller 9a5720e8ad
Universal: PR Check / Branch Policy (pull_request) Successful in 1s
Branch Policy Check / Verify merge target (pull_request) Successful in 1s
PR RC Release / Build RC Release (pull_request) Successful in 3s
Universal: PR Check / Validate PR (pull_request) Failing after 6s
Branch Cleanup / Delete merged branch (pull_request) Successful in 1s
Universal: PR Check / Build RC Package (pull_request) Has been cancelled
chore: rename Go module from git. to code.mokoconsulting.tech (#336)
Full namespace migration: update the Go module path and all import
statements from git.mokoconsulting.tech to code.mokoconsulting.tech.
Also updates all URL references in templates, workflows, configs,
tests, and documentation.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-05-31 10:28:25 -05:00

93 lines
2.4 KiB
Go

// Copyright 2021 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package routing
import (
"context"
"net/http"
"code.mokoconsulting.tech/MokoConsulting/MokoGitea/modules/gtprof"
"code.mokoconsulting.tech/MokoConsulting/MokoGitea/modules/log"
"code.mokoconsulting.tech/MokoConsulting/MokoGitea/modules/reqctx"
"code.mokoconsulting.tech/MokoConsulting/MokoGitea/modules/web/types"
)
type contextKeyType struct{}
var contextKey contextKeyType
func getRequestRecord(ctx context.Context) *requestRecord {
record, _ := ctx.Value(contextKey).(*requestRecord)
return record
}
// RecordFuncInfo records a func info into context
func RecordFuncInfo(ctx context.Context, funcInfo *FuncInfo) (end func()) {
end = func() {}
if reqCtx := reqctx.FromContext(ctx); reqCtx != nil {
var traceSpan *gtprof.TraceSpan
traceSpan, end = gtprof.GetTracer().StartInContext(reqCtx, "http.func")
traceSpan.SetAttributeString("func", funcInfo.shortName)
}
if record := getRequestRecord(ctx); record != nil {
record.lock.Lock()
record.funcInfo = funcInfo
record.lock.Unlock()
}
return end
}
func GetRequestRecordInfo(reqCtx context.Context) (ret struct {
HasRecord bool
IsLongPolling bool
},
) {
record := getRequestRecord(reqCtx)
if record == nil {
return ret
}
ret.HasRecord = true
record.lock.RLock()
ret.IsLongPolling = record.isLongPolling
record.lock.RUnlock()
return ret
}
// MarkLongPolling marks the request is a long-polling request, and the logger may output different message for it
func MarkLongPolling() types.PreMiddlewareProvider {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
record := getRequestRecord(req.Context()) // it must exist
record.lock.Lock()
record.isLongPolling = true
record.logLevel = log.TRACE
record.lock.Unlock()
next.ServeHTTP(w, req)
})
}
}
func MarkLogLevelTrace(resp http.ResponseWriter, req *http.Request) {
record := getRequestRecord(req.Context())
if record == nil {
return
}
record.lock.Lock()
record.logLevel = log.TRACE
record.lock.Unlock()
}
// UpdatePanicError updates a context's error info, a panic may be recovered by other middlewares, but we still need to know that.
func UpdatePanicError(ctx context.Context, err error) {
record := getRequestRecord(ctx)
if record == nil {
return
}
record.lock.Lock()
record.panicError = err
record.lock.Unlock()
}