A Phoenix API app, with a single endpoint that returns at max 2 users with more points than a random value.
| Elixir 1.12.2 | Erlang 24.0 | Postgres 13.3 |
|---|
- Here you can track the Issues's list from this Repo.
- To understand my development process you can read the past Pull Requests or the Technical Decisions's file.
- Here you can access this Project Demo(Temporarily unavailable) deployed with Gigalixir
If you have any question or suggestion feel free to reach me out by E-mail or Twitter, let's learn together :)
The business logic has been explained in details and breaks down into the Issues's list, but here is a summary of it:
mix ecto.setupwill create a Users table with 3 columns:id,points(any number between 0..100) andtimestamp. This should generate1_000_000 user seeds, each with 0 points.- When the app starts, a GenServer should be launched which will:
- Have 2 elements as state: random (0..100)
max_numberand atimestamp(defaults tonil). - Run every minute and when it runs:
- Should update every user's points in the database (using a random number generator [0-100] for each) and refresh
max_numberwith a new random number.
- Should update every user's points in the database (using a random number generator [0-100] for each) and refresh
- Should accept a handle_call that:
- Queries the database for all users with more points than
max_numberbut only retrieve a max of 2 users, updatestimestampwith the current timestamp and returns the users just retrieved, as well as the timestamp of the previoushandle_call.
- Queries the database for all users with more points than
- Have 2 elements as state: random (0..100)
- The app should also have a single endpoint, root
/:
- This can be handled by an action which will call the previously mentioned
GenServerand return the result, example:GET localhost:4000/{ 'users': [{id: 15, points: 42}, {id: 42, points: 45}], 'timestamp': `2021-08-08 12:30:15` }
Score App have the following dependencies:
Erlang, Elixir, Postgres and Docker (optional)
To prepare your environment, I made this Installation Guide:
- How to install dependencies
After the environment setup (if you haven't cloned Repo yet), clone the Repo and access the project folder created:
git clone git@github.com:JulianaHelena5/score.git
cd score-
Database Setup
Check the
config/dev.exsandconfig/test.exsfiles and make sure that the settings match your localPostgresenvironment variables, example:# Configure your database - config/dev.exs config :score, Score.Repo, username: System.get_env("PGUSER", "postgres"), password: System.get_env("PGPASSWORD", "postgres"), database: System.get_env("PGDATABASE", "score_dev"), hostname: System.get_env("PGHOST", "localhost")
Now, we have two ways to run it:
-
Running with Docker
Run
docker-composecommands:
docker-compose build
docker-compose run --rm phx mix ecto.setup
docker-compose up -d-
Running without Docker
Run the following commands:
mix deps.get
mix ecto.setup
mix phx.serverIn both ways, access the app through: http://localhost:4000/
To run the Tests you should only execute:
mix testIf you face any error please check the configuration of the
config/test.exsfile explained at the Database Setup step.
Run this line to check the Test coverage report:
mix coveralls #Show coverage
mix coveralls.detail #Show coverage with details by fileThis project is using Github Actions and is being deployed to Gigalixir. (Temporarily unavailable)
You can check the Elixir CI and Gigalixir - Deploy files to understand the flow and also check the history of runs from all workflows.
In this step you'll find some choices I made to try to improve the Project Quality:
-
To format the code I'm using
mix format:mix format
-
As a static code analysis tool I'm using Credo:
mix credo
"It can show you refactoring opportunities in your code, complex code fragments, warn you about common mistakes, show inconsistencies in your naming scheme and - if needed - help you enforce a desired coding style" (Credo's Readme).
-
Configured GitHub hooks to prevent unformatted code or failing Tests from being pushed. You can check the
pre-pushandpre-commitfiles here. -
To help with the quality of code changes description I added this Pull Request Template.
Keep moving forward :)
- Fix Gigalixir database access.
- Improve GenServer tests.
- Increase test coverage.
- Study a better way to schedule the GenServer call if needed.
- Metrics and monitoring.
Do you have any suggestion? Send me, please!
- Official website: https://www.phoenixframework.org/
- Guides: https://hexdocs.pm/phoenix/overview.html
- Docs: https://hexdocs.pm/phoenix
- Forum: https://elixirforum.com/c/phoenix-forum
- Source: https://github.com/phoenixframework/phoenix