Select Git revision
-
Jan Semmelink authoredJan Semmelink authored
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