Skip to content
Snippets Groups Projects
Select Git revision
  • main default protected
  • trading_hours
  • refactor_trading_hours
  • audit_cleaning_cater_for_non_struct_fields
  • remove-info-logs
  • sl-refactor
  • 18-use-scan-for-param-values
  • 17-order-search-results
  • 4-simplify-framework-2
  • 1-http-error
  • 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
  • v1.278.0
  • v1.277.0
  • v1.276.0
  • v1.275.0
30 results

api.go

Blame
  • api.go 5.60 KiB
    package api
    
    import (
    	"fmt"
    	"net/http"
    	"os"
    	"runtime/debug"
    
    	"github.com/aws/aws-lambda-go/lambda"
    	"gitlab.com/uafrica/go-utils/audit"
    	"gitlab.com/uafrica/go-utils/errors"
    	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
    }
    
    //wrap else cannot be chained