Select Git revision
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,
}