[IBP ] Node / Heroku buildpack backend for Mailgun service
- This is express backend application that is based on earlier backend which implemented all CRUD API endpoints using the provided data.
- Added functionality: using Mailgun to send/track emails.
- sample heroku backend is on http://glacial-cove-96986.herokuapp.com
- added readme entities, which are neccessary for running this app in this submission are emphasized
Files added
IBP.email.postman_collection.json: latest postman collectionIBP.email.postman_environment.json: latest postman environmentsrc/modules/email/..: email moduletest_files/sql/email.sql: sql file to setup email module.eslintrc: eslintrc, change rules if you think some are not proper.gitignore: basic gitignore file for node_modules
Dependencies
- runs on node.js v5.11.1 / node.js v6
- postgres 9.5
- express 4+
- [heroku][https://www.heroku.com]
- [eslint][http://eslint.org/]
- postman chrome extension for verification https://www.getpostman.com
Important: Mailgun configuration
Configuration
- Edit configuration in
config/default.jsonand - custom environment variables names in
config/custom-environment-variables.json, - for example it will read DATABASE_URL as url of database from heroku.
Following variables can be configured:
authSecretthe secret for authportthe port to listenlogLevelthe log leveldebugorinfoversionthe version of apidbConfigthe configurations for database and contains url, pool size and pool idle timeout.
Heroku deployment
-
You will need to install Heroku Toolbelt for this step if you don't already have it installed.
-
In the main project folder, run the following commands:
heroku login git init git add . git commit -m "init" heroku create heroku addons:create heroku-postgresql:hobby-dev git push heroku master heroku logs -t heroku open
Local Deployment
- for development it is very handy to have buildpack deployed to your local machine.
- Please make sure to configure url of database rightly in
config/default.jsonor use environment variable DATABASE_URL.- current default
db_url: postgres://test:test@localhost:5432/test - so, you should have postgres running on port
5432of yourlocalhost - with database name
test - which is given access to user
test - who can log in with password
test
- current default
- Install dependencies
npm i - run lint check
npm run lint - Start app
npm start
Database restore (existing functionality)
-
Please follow this article restore
-
or restore on heroku to restore dump file in
test_files\games.psql. -
I actually create sample module to reset database easily you can just configure databae url rightly, start application and run reset request in demo folder in postman.
-
manually restore required database dump by above guides, or launch postman(after deployed to heroku), goto demo -> reset, click send
Email Database Setup
-
all email data will go to table
emails -
after you did database restore above,
-
execute
test_files/sql/email.sqlon heroku postgres.$ cat test_files/sql/email.sql | heroku pg:psql ---> Connecting to DATABASE_URL SET SET SET SET SET SET SET SET SET SET DROP TABLE CREATE TABLE GRANT GRANT
Setup postman
-
Load postman collection:
- endpoints: IBP.email.postman_collection.json
- environment: IBP.email.postman_environment.json
-
postman environment variables:
URLthe base API url for local testing usehttp://localhost:3000or heroku app urlTOKENsample super role tokenADMIN_TOKENsample admin role tokenUSER_TOKENsample user role tokenNOROLES_TOKENsample user without roles tokenINVALID_TOKENsample invalid tokenmailIdid assigned to email, after you sucessfully sent/queued an email you'll be assigned oneaddressemail address to send/receive, because mailgun doesn't support mailing to outside or mailing to many receivers unless you own a domain, this address will be used as sender/receiver. so use your address to verify a mail is sent to you. use the mail address you used in registering to mailgunb64imagetopcoder logo encoded in base64 to test image attachment functionality
Email Postman verification
Email API
-
check
api/emailApi.yamlfor swagger definition. -
POST
/emailsendpoint to send an email -
to set custom headers:
"headers": [ "x-test-header:1234" <== {{headerName}}:{{headerValue}} ] -
to schedule a mail to be sent in the future, set
delivery_timein ISO 8601 date format. -
for attachments,
content_bytesshould be string in base64 of original binary data."attachments": [{ "file_name": "blank.txt", "file_type": "text", "content_bytes": "IAo=" }] -
after a successful POST to
/emailsendpoint, an email ID will be assigned, sample response:200 Ok { "result": { "success": true, "code": 200 }, "id": 3 } -
use above
idto track delivery status, by/emails/{id}/deliveryStatusendpoint.
On Mailgun Api
- If you set multiple mail receivers, their api doesn't support tracking properly
- Mailgun explicitly states (https://documentation.mailgun.com/api-events.html) that their event api isn't updated right on time, and users are required to throw away the result reported from the mailgun api and repeat the request on their discretion. so, for tracking, it's possible that in some cases 1) mail is sent (it is in my inbox) 2) but their event api isn't updated.
Additional functionality
GET /emails/statsendpoint for tracking Mailgun usage for past 1 month.
Module system for future developers
- structure modules in each folder,
src/modules/<module name> - put controllers for each module in
src/modules/<module name>/controllers - put services for each module in
src/modules/<module name>/services - a module should have
routes.json top,src/modules/<module name>/routes.js. declare routes to controllers in this file. - use relative imports, e.g )
const service = require('../services/fooService');to load services from controllers within a module, or import between services within a module. - currently existing modules:
crud,sample,mail - how the modules are loaded:
src/app-routes.jswill globsrc/modules/*/routes.jsand load them. - remove a module from application by deleting a module directory.
Authentication & Authorization
- It will use passport and it could support social feature to login as twitter, facebook easily.
- It will use
src/app-passport.jsto load passport strategies in src/passports, you can defineauth:{name of passport strategy}inroutes.jsof module. - You can also define
access:[role1,role2], the user role name is fromsrc/constants.js. - It will check Authentication & Authorization in
src/app-routes.jsto secure your APIs.