package
0.0.0-20200917143433-b1b50a11e74b
Repository: https://github.com/al-un/alun-api.git
Documentation: pkg.go.dev
# Functions
AddCorsHeaders set the usual CORS headers.
AddJSONHeaders add the required header for accepting and providing JSON.
BuildJWT generate a JWT from a specific list of claims.
DecodeJWT extracts the claims from a JWT if it is valid.
DoIfAccess ensures that the provided accessChecker passes before proceeding to the authenticatedHandler.
GetVar fetch the variable defined in the route.
HandleServerError is the generic way to handle server error: just send a 500 and the message with it.
LoggerInOutRequest displays information for inbound request and outbound result.
MongoConnectFromEnvVar connects to a Mongo database with the provided environment variable
This helper does not guarantee that dotenv files are properly loaded.
MongoConnectToDb creates a Mongo client instance from an URI as well as the Mongo database instance depending on the database name in the URI.
MongoParseDbURI parse the DB URL.
NewAPI is the API constructor
- Allowed CORS headers and hosts are, for the moment, "*" - By default, the JSON middleware is always added.
NewServiceErrorMessage generate a ServiceMessage from an error.
SetupRouter loads all API.
# Constants
APIMicroservice to enable microservice mode.
APIMonolithic to enable monolithic mode.
APIv1 is the standardisation for first version of an API endpoint.
APIv2 is the standardisation for second version of an API endpoint.
TrackedCreatedAt is the createdAt key.
TrackedCreatedBy is the createdBy key.
TrackedUpdatedAt is the updatedAt key.
TrackedUpdatedBy is the updatedBy key.
# Variables
CheckIfAdmin simply checks if the JWT has admin privilege.
CheckIfLogged is the AuthChecher ensuring the request has a properly logged-in user.
CheckPublic always returns true to provided public access.
ClientDomain refers to the expected domain of the client application.
URLDefaultBuilder concatenates "/{version}/{root}/{url}"".
# Structs
API exposes the list of handler with a specific URL root
Each APIHandler will be linked to the route URL.
APIEndpoint maps a handler (authenticated or standard) with an URL pattern and a HTTP method
If both "publicHandler" and "protectedHandler" are defined, the public version takes precedence.
CorsConfig allows a flexible way to handle CORS stuff.
JwtClaims extends standard claims for our User model.
ServiceMessage is a token to forward the status of an action to the next function / whatever handler processing it.
TrackedEntity is the basic structure for all entities which require tracking: user tracking and time tracking
User reference are `primitive.ObjectID` to match "primary keys" of the users collection.
# Type aliases
AccessChecker ensures that the provided request is allowed to proceed or not.
AuthenticatedHandler is meant to be the core logic of the handler with the user informations already extracted from the request.
EndpointAdapter (or Decorator design pattern) wrapper consecutive middlewares.
URLBuilder defines how an API generates a final endpoint URL given an (optional) root url, a version and the endpoint url
Root URL is optional for microservices-ready structure.