This is a simple microservice developed to complement my talk about Phoenix Framework. The main purpose of this microservice is storing audit log messages about business relevant events happening in an imaginary distributed system.
Audit Log Entry
consists of the following fields:
actor_id
- id of an actor that has performed an actionactor_type
- type of the actor: e.g "user", "system"action
- the action performedresource_id
- id of resource on which the action was performedresource_type
- type of resourcecreated_at
- timestamp
Audit Log Entry
creation happens as an async reaction to a message published to AWS SQS queue.
- it listens to messages from AWS SQS queue
- it stores an audit log entry into database
- it provides API to fetch audit log entries
- it has a web UI to display audit log entries
- it has a web UI to tail audit log (new entries are broadcaset from server via websockets)
- Install Elixir:
brew install elixir
- Install Node.js:
brew install node
- Install Docker https://docs.docker.com/docker-for-mac/install/
- Install aws cli:
brew install awscli
- Add your AWS credentials to
~/.aws/credentials
- Start a Docker container with database:
docker-compose up -d
- Install dependencies with
mix deps.get
- Create, migrate and populate your database with
mix ecto.setup
- Install Node.js dependencies with
cd assets && npm install
Start Phoenix endpoint with QUEUE_NAME=<your AWS account>/<queue name> mix phx.server mix phx.server
Now you can visit localhost:4000
from your browser and see list of audit log entries.
Execute this command in your terminal:
aws sqs send-message --queue-url https://eu-west-1.amazonaws.com/<your AWS account>/<queue name> --message-body '{ "event_type": "CREATE_BUSINESS_AUDIT_LOG_ENTRY", "created_at": "2017-03-14T11:17:00Z", "payload": { "actor_id": "2", "actor_type": "user", "action": "Got excited about Elixir", "resource_type": "user", "resource_id": "2" } }'
There is only one API endpoint:
/api/entries/:resource_type/:resource_id
It returns a paginated list of audit log entries for given resource in JSON format.
Here is usage example:
http://localhost:4000/api/entries/user/2?page_size=5&page=1
-
Open
http://localhost:4000
to see a paginated list of audit log entries. -
Open
http://localhost:4000/tail
to tail the log. Try to publish some messages to the queue and see how they are delivered to your browser via Websocket connection.