Skip to content
Snippets Groups Projects
Select Git revision
  • 8190f92a5f6eb416f138574dc3c42f15dcc54d4c
  • 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

api.go

Blame
  • api.go 5.73 KiB
    package api
    
    import (
    	"fmt"
    	"net/http"
    	"runtime/debug"
    	"sync"
    
    	"github.com/aws/aws-lambda-go/events"
    	"github.com/aws/aws-lambda-go/lambda"
    	"gitlab.com/uafrica/go-utils/audit"
    	"gitlab.com/uafrica/go-utils/errors"
    	"gitlab.com/uafrica/go-utils/logger"
    	queues_mem "gitlab.com/uafrica/go-utils/queues/mem"
    	queues_sqs "gitlab.com/uafrica/go-utils/queues/sqs"
    	"gitlab.com/uafrica/go-utils/service"
    	"gitlab.com/uafrica/go-utils/string_utils"
    )
    
    //LEGACY: global variable is set only for backward compatibility
    //When handlers are changed to accept context, they should get this from the context
    var CurrentRequestID *string
    
    //New creates the API with the specified routes keys on [path][method]
    //value could be any of the handler function signatures supported by the api.Router
    //requestIDHeaderKey is defined in the response header to match the requestID from the request
    func New(requestIDHeaderKey string, routes map[string]map[string]interface{}) Api {
    	if requestIDHeaderKey == "" {
    		requestIDHeaderKey = "request-id"
    	}
    
    	router, err := NewRouter(routes)
    	if err != nil {
    		panic(fmt.Sprintf("cannot create router: %+v", err))
    	}
    
    	return Api{
    		Service:                 service.New(),
    		router:                  router,
    		requestIDHeaderKey:      requestIDHeaderKey,
    		checks:                  map[string]ICheck{},
    		crashReporter:           defaultCrashReporter{},
    		cors:                    nil,
    		localPort:               0,
    		localQueueEventHandlers: nil,
    	}
    }
    
    type Api struct {
    	service.Service
    	router                  Router
    	requestIDHeaderKey      string
    	checks                  map[string]ICheck
    	crashReporter           ICrashReporter
    	cors                    ICORS
    	localPort               int                    //==0 for default lambda, >0 for http.ListenAndServe to run locally
    	localQueueEventHandlers map[string]interface{} //only applies when running locally for local in-memory queues
    }
    
    //wrap Service.WithStarter to return api, else cannot be chained
    func (api Api) WithStarter(name string, starter service.IStarter) Api {
    	api.Service = api.Service.WithStarter(name, starter)
    	return api
    }
    
    //wrap Service.WithErrorReporter to return api, else cannot be chained
    func (api Api) WithErrorReporter(reporter service.IErrorReporter) Api {
    	api.Service = api.Service.WithErrorReporter(reporter)
    	return api
    }