pass context as an argument,rename context keys

This commit is contained in:
Carlosokumu 2025-01-06 08:52:53 +03:00
parent 974af6b2a7
commit c69d3896f1
Signed by: carlos
GPG Key ID: 7BD6BC8160A5C953
4 changed files with 10 additions and 12 deletions

View File

@ -28,7 +28,7 @@ import (
)
var (
logg = logging.NewVanilla().WithDomain("ussdmenuhandler").WithContextKey("session-id")
logg = logging.NewVanilla().WithDomain("ussdmenuhandler").WithContextKey("SessionId")
scriptDir = path.Join("services", "registration")
translationDir = path.Join(scriptDir, "locale")
)
@ -124,7 +124,7 @@ func (h *Handlers) Init(ctx context.Context, sym string, input []byte) (resource
sessionId, ok := ctx.Value("SessionId").(string)
if ok {
context.WithValue(ctx, "session-id", sessionId)
ctx = context.WithValue(ctx, "SessionId", sessionId)
}
flag_admin_privilege, _ := h.flagManager.GetFlag("flag_admin_privilege")

View File

@ -15,16 +15,14 @@ import (
)
type ATRequestParser struct {
Context context.Context
}
func (arp *ATRequestParser) GetSessionId(rq any) (string, error) {
func (arp *ATRequestParser) GetSessionId(ctx context.Context, rq any) (string, error) {
rqv, ok := rq.(*http.Request)
if !ok {
logg.Warnf("got an invalid request", "req", rq)
return "", handlers.ErrInvalidRequest
}
// Capture body (if any) for logging
body, err := io.ReadAll(rqv.Body)
if err != nil {
@ -43,9 +41,9 @@ func (arp *ATRequestParser) GetSessionId(rq any) (string, error) {
decodedStr := string(logBytes)
sessionId, err := extractATSessionId(decodedStr)
if err != nil {
context.WithValue(arp.Context, "at-session-id", sessionId)
ctx = context.WithValue(ctx, "AT-SessionId", sessionId)
}
logg.Debugf("Received request:", decodedStr)
logg.DebugCtxf(ctx, "Received request:", decodedStr)
}
if err := rqv.ParseForm(); err != nil {

View File

@ -10,7 +10,7 @@ import (
)
var (
logg = logging.NewVanilla().WithDomain("atserver")
logg = logging.NewVanilla().WithDomain("atserver").WithContextKey("AT-SessionId")
)
type ATSessionHandler struct {
@ -34,7 +34,7 @@ func (ash *ATSessionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request)
rp := ash.GetRequestParser()
cfg := ash.GetConfig()
cfg.SessionId, err = rp.GetSessionId(req)
cfg.SessionId, err = rp.GetSessionId(req.Context(), req)
if err != nil {
logg.ErrorCtxf(rqs.Ctx, "", "header processing error", err)
ash.WriteError(w, 400, err)
@ -48,7 +48,7 @@ func (ash *ATSessionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request)
return
}
rqs, err = ash.Process(rqs)
rqs, err = ash.Process(rqs)
switch err {
case nil: // set code to 200 if no err
code = 200

View File

@ -10,7 +10,7 @@ import (
)
var (
logg = logging.NewVanilla().WithDomain("httpserver")
logg = logging.NewVanilla().WithDomain("httpserver").WithContextKey("at-session-id")
)
type SessionHandler struct {
@ -46,7 +46,7 @@ func (f *SessionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
rp := f.GetRequestParser()
cfg := f.GetConfig()
cfg.SessionId, err = rp.GetSessionId(req)
cfg.SessionId, err = rp.GetSessionId(req.Context(), req)
if err != nil {
logg.ErrorCtxf(rqs.Ctx, "", "header processing error", err)
f.WriteError(w, 400, err)