mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-06 06:06:49 +00:00

* [NOD-310] Implement REST server in API server * [NOD-310] MetaData -> Metadata * [NOD-310] Make custom context methods instead of custom request functions * [NOD-310] change "Request ID" prefix to "RID" and convert to apiServerContext with newAPIServerContext everywhere
63 lines
1.5 KiB
Go
63 lines
1.5 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
)
|
|
|
|
type contextKey string
|
|
|
|
const (
|
|
contextKeyRequestID contextKey = "REQUEST_ID"
|
|
)
|
|
|
|
type apiServerContext struct {
|
|
context.Context
|
|
}
|
|
|
|
func newAPIServerContext(ctx context.Context) *apiServerContext {
|
|
if asCtx, ok := ctx.(*apiServerContext); ok {
|
|
return asCtx
|
|
}
|
|
return &apiServerContext{Context: ctx}
|
|
}
|
|
|
|
func (ctx *apiServerContext) setRequestID(requestID uint64) context.Context {
|
|
context.WithValue(ctx, contextKeyRequestID, nextRequestID)
|
|
return ctx
|
|
}
|
|
|
|
func (ctx *apiServerContext) requestID() uint64 {
|
|
id := ctx.Value(contextKeyRequestID)
|
|
return id.(uint64)
|
|
}
|
|
|
|
func (ctx *apiServerContext) getLogString(format string, params ...interface{}) string {
|
|
params = append(params, ctx.requestID())
|
|
return fmt.Sprintf("RID %d: "+format, params)
|
|
}
|
|
|
|
func (ctx *apiServerContext) tracef(format string, params ...interface{}) {
|
|
log.Tracef(ctx.getLogString(format, params))
|
|
}
|
|
|
|
func (ctx *apiServerContext) debugf(format string, params ...interface{}) {
|
|
log.Debugf(ctx.getLogString(format, params))
|
|
}
|
|
|
|
func (ctx *apiServerContext) infof(format string, params ...interface{}) {
|
|
log.Infof(ctx.getLogString(format, params))
|
|
}
|
|
|
|
func (ctx *apiServerContext) warnf(format string, params ...interface{}) {
|
|
log.Warnf(ctx.getLogString(format, params))
|
|
}
|
|
|
|
func (ctx *apiServerContext) errorf(format string, params ...interface{}) {
|
|
log.Errorf(ctx.getLogString(format, params))
|
|
}
|
|
|
|
func (ctx *apiServerContext) criticalf(format string, params ...interface{}) {
|
|
log.Criticalf(ctx.getLogString(format, params))
|
|
}
|