@impesa/common v1.5.15
@impesa/common
This library is a set of utilities that uses googleapis and general functions commonly used in the impesa APIs infraestructure.
Microservice
Start a preconfigured Express server. This uses helmet, cors, body-parser and start the gcloud debugger and logger for issue tracking. It is also capable of loading configurations from the spanner setting table and datastore setting table entity using the settings parameter if it is set to true.
microservice.start({
port: 8090,
basepath: '/service',
spanner: {
instance: '{YOUR-INSTANCE-NAME}',
database: '{YOUR-DATABASE-NAME}',
settings: true
},
datastore: {
namespace: '{YOUR-NAMESPACE-NAME}',
settings: true
},
middleware: function (app) {
console.log('...using middleware');
},
routing: function () {
return router_paths;
},
listening: function (server) {
console.log('...server listening');
}
});
Endpoint Authorization
Set the following variables by settings loader (spanner or datastore)
AUTH_DOMAIN = '{YOUR-AUTHORIZATION-DOMAIN}';
// auth audience depends of each microservice basepath
AUTH_AUDIENCE = `{YOUR-API-DOMAIN}${basepath}`;
To access an endpoint set the Authorization header as "Bearer {access_token}".
Example: Authorization: Bearer 874887d6ecd0b106a47448c5beca1...
Add the auth function with scopes (optional) to the endpoint request handlers:
const auth = require('@impesa/common').auth;
router.route('/hi')
.all(auth(['read:hi'])) //scopes is optional
.get((req, res) => {
res.send('Hello world!');
});
Spanner
Add an spanner database by running spanner.add(options)
using the following parameters: projectId
, instance
, database
.
Then get an spanner instance by running spanner.db(name:optional)
.
Datastore
Add an datastore c by running datastore.add(options)
using the following parameters: projectId
, datastore
.
Then get a datastore instance by running datastore.db(namespace:optional)
.
Subscriptions
Create many PubSub subscriptions
subs: [{
name: 'topic-subscription-name',
func: (msj, att) => {
console.log('receiving...');
}
}]
WebSockets support
Listen many ws events
sockets: {
login: (data)=>{ }, //login function, will set user property of client
events: [{
name: 'message',
func: (event) => {
//message received as {sender, user, ...event}
}
}]
}
Debugger
In production mode, the debugger instance is initialized by running debugger.start()
; this starts the debugger using the following environment variables: PROJECTID
.
Logger
In production mode, the logger is initialized to write different kind of logs ('INFO', 'WARM', 'ERROR') in gcloud Logging or track errors in gcloud Error Reporting; the logger uses the following environment variables: PROJECTID
.
Log: logger.log('Info message', 'General', 'INFO');
Error: logger.error(error);
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago