Coder Social home page Coder Social logo

rust_stack_example's Introduction

Project Stack Example

This project is an example Rust stack containing a database and server application.
The stack is comprised PostgreSQL for the database, and the Axum web application framework.

Setting Up and Running

When running locally, fill-in the .env file, the POSTGRES_PASSWORD is the password that Postgres will use, the ALPHA_VANTAGE_API_KEY is the key that will be used to make requests to the AlphaVantage Stock API, a free key can be requested here. An example of .env is available as .env.example. POSTGRES_HOST, POSTGRES_USER, and POSTGRES_DBNAME are exposed for further customization, but the default value of postgres can be used.

Example of how the .env should look like, this values are for example purposes only:

ALPHA_VANTAGE_API_KEY="example"
POSTGRES_HOST="postgres"
POSTGRES_PORT=5432
POSTGRES_USER="postgres"
POSTGRES_PASSWORD="example"
POSTGRES_DBNAME="postgres"

Install Docker on your system. With Docker installed, run the following command to build and run the stack:

docker compose up

After the stack is up and running, it should be possible to check the status of the containers using docker ps -a. The output should look something like:

$> docker ps -a
CONTAINER ID   IMAGE                         COMMAND                  CREATED         STATUS         PORTS                    NAMES
0649225aff88   rust_stack_example-api        "financial_data"         7 minutes ago   Up 7 seconds   0.0.0.0:8080->8000/tcp   rust_stack_example-api-1
6323931677a2   postgres:alpine               "docker-entrypoint.s…"   4 days ago      Up 7 seconds   5432/tcp                 rust_stack_example-postgres-1

Initialization

On startup the table on the database is created. A background task that runs daily is also started to upsert the values of the daily times series.

Logging

The logging level of the application can be set by adding RUST_LOG=<LEVEL> on the docker-compose.yml, in the environment section of the api service.

Queries

The API exposes 2 endpoints: financial_data and statistics.

financial_data

Recovers the symbol (name of the equity), date, open_price, close_price and volume.

Parameters

  • symbol: (Optional) Name of equity to recover data from.
  • start_date: (Optional) Filters dates that are earlier than this.
  • end_date: (Optional) Filters dates that are later than this.
  • limit: (Optional, Default=5) Limit the number of items in the response.
  • page: (Optional, Default=1) Get the page of number page for results that go over the limit.

Example

http://localhost:8881/api/financial_data?start_date=2023-04-01&end_date=2023-04-078&symbol=IBM&limit=5&page=1
Note: An empty response might mean that the dates are too old for when you are

statistics

Recovers the symbol (name of the equity), start_date, end_date, average_daily_open_price, average_daily_close_price and average_daily_volume.

Parameters

  • symbol: Name of equity to recover data from.
  • start_date: Filters dates that are earlier than this.
  • end_date: Filters dates that are later than this.

Example

http://localhost:8881/api/statistics?start_date=2023-04-01&end_date=2023-04-07&symbol=IBM
Note: An empty response might mean that the dates are too old for when you are

Security

For local development, the use of .env to set the environment variables of the docker compose is enough, but including it in the deployment of the production version is a security risk. Each provider has a proper way of setting enviroment variables securely, refer to the documentation of your server provider for the proper way of setting environment variables.

rust_stack_example's People

Contributors

dbrowne avatar hukasu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.