Skip to content
Snippets Groups Projects
Select Git revision
  • a0455aeef58d73441e2951675f430ffe53dbde6e
  • main default protected
  • v1.298.0
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
  • v1.282.0
  • v1.281.0
  • v1.280.0
  • v1.279.0
22 results

handler.go

Blame
  • context.go 7.53 KiB
    package api
    
    import (
    	"context"
    	"encoding/json"
    	"fmt"
    	"reflect"
    	"strconv"
    	"strings"
    	"time"
    
    	"github.com/aws/aws-lambda-go/events"
    	"github.com/uptrace/bun"
    	"gitlab.com/uafrica/go-utils/errors"
    	"gitlab.com/uafrica/go-utils/logger"
    	"gitlab.com/uafrica/go-utils/queues"
    	"gitlab.com/uafrica/go-utils/service"
    )
    
    type IContext interface {
    	context.Context
    	logger.ILogger
    	queues.IProducer
    	StartTime() time.Time
    	MillisecondsSinceStart() int64
    	CheckValues(checkName string) interface{}
    	CheckValue(checkName, valueName string) interface{}
    }
    
    type Context struct {
    	service.Context
    	queues.IProducer
    	Request          events.APIGatewayProxyRequest
    	RequestID        string
    	ValuesFromChecks map[string]map[string]interface{} //also in context.Value(), but cannot retrieve iteratively from there for logging...
    	DB               *bun.DB
    }
    
    func (ctx Context) CheckValues(checkName string) interface{} {
    	if cv, ok := ctx.ValuesFromChecks[checkName]; ok {
    		return cv
    	}
    	return nil
    }
    
    func (ctx Context) CheckValue(checkName, valueName string) interface{} {
    	if cv, ok := ctx.ValuesFromChecks[checkName]; ok {
    		if v, ok := cv[valueName]; ok {
    			return v
    		}
    	}
    	return nil
    }
    
    // func (ctx Context) Audit(org, new interface{}, eventType types.AuditEventType) {
    // 	//call old function for now - should become part of context ONLY
    // 	audit.SaveAuditEvent(org, new, ctx.Claims, eventType, &ctx.RequestID)
    // }
    
    //todo: change to be a ctx method that defer to log so it does not have to be called explicitly
    //it should also capture metrics for the handler and automaticlaly write the audit record,
    //(but still allow for audit to be suppressed may be in some cases)
    func (ctx Context) LogAPIRequestAndResponse(res events.APIGatewayProxyResponse, err error) {
    	fields := map[string]interface{}{
    		"path":                   ctx.Request.Path,
    		"method":                 ctx.Request.HTTPMethod,
    		"status_code":            res.StatusCode,
    		"api_gateway_request_id": ctx.RequestID,
    	}