Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .env.dev

This file was deleted.

6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Database
POSTGRES_HOST=tobemodified
POSTGRES_PORT=tobemodified
POSTGRES_DB=tobemodified
POSTGRES_USER=tobemodified
POSTGRES_PASSWORD=tobemodified
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ When the project configuration is finished, click Build to build the project.
### Project run

#### Before launching web server:
- Run the database container via docker compose command `docker compose -f docker-compose.dev.yaml up -d` from workspace.
- Run the database container via docker compose command `docker compose up -d` from workspace.
- Import environment variables declared in the `.env` file while in the dev container and using the command `export $(grep -v '^#' .env | xargs)`. If the file containing the environment variables is named something other than `.env`, you should modify the command to specify the correct name.

To launch the executable, click Launch in the CMake extension.
<p style="text-align: center;"><img src="docs/images/cmakeLaunch.png" alt="cmakeLaunch" width="400"/></p>
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.dev.yaml → docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ services:
container_name: postgresql
image: postgres:14-alpine
env_file:
- .env.dev
- .env
volumes:
- postgresql-data:/var/lib/postgresql/data
ports:
- "5432:5432"

volumes:
postgresql-data:
postgresql-data:
8 changes: 0 additions & 8 deletions src/data/alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,6 @@ path_separator = os
# are written from script.py.mako
# output_encoding = utf-8

# database URL. This is consumed by the user-maintained env.py script only.
# other means of configuring database URLs may be customized within the env.py
# file.

# TODO(https://github.com/TourmalineCore/to-dos-api-cpp/issues/22): parse connection data variables from .env or .env.dev
sqlalchemy.url = postgresql://postgres:admin@localhost/to-dos-api-cpp_db


[post_write_hooks]
# post_write_hooks defines scripts or Python functions that are run
# on newly generated revision scripts. See the documentation for further
Expand Down
14 changes: 13 additions & 1 deletion src/data/migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@
# my_important_option = config.get_main_option("my_important_option")
# ... etc.

def get_database_url():
"""Getting the Database URL from Environment Variables"""
user = os.getenv('POSTGRES_USER')
password = os.getenv('POSTGRES_PASSWORD')
host = os.getenv('POSTGRES_HOST')
port = os.getenv('POSTGRES_PORT')
name = os.getenv('POSTGRES_DB')

return f"postgresql://{user}:{password}@{host}:{port}/{name}"

url = get_database_url()

def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
Expand All @@ -42,7 +53,6 @@ def run_migrations_offline() -> None:
script output.

"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
Expand All @@ -61,6 +71,8 @@ def run_migrations_online() -> None:
and associate a connection with the context.

"""
config.set_main_option('sqlalchemy.url', url)

connectable = engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
Expand Down
13 changes: 6 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ int main()
{
// TODO(https://github.com/TourmalineCore/to-dos-api-cpp/issues/27): add migration update on startup

// TODO(https://github.com/TourmalineCore/to-dos-api-cpp/issues/22): parse that variables from .env or .env.dev
const std::string user = "postgres";
const std::string password = "admin";
const std::string db_name = "to-dos-api-cpp_db";
const std::string host = "localhost";
const unsigned port = 5432;
const std::string user = std::getenv("POSTGRES_USER");
const std::string password = std::getenv("POSTGRES_PASSWORD");
const std::string db_name = std::getenv("POSTGRES_DB");
const std::string host = std::getenv("POSTGRES_HOST");
const std::string port = std::getenv("POSTGRES_PORT");

std::string conninfo = "host=" + host + " port=" + std::to_string(port) + " dbname=" + db_name + " user=" + user + " password=" + password;
std::string conninfo = "host=" + host + " port=" + port + " dbname=" + db_name + " user=" + user + " password=" + password;

std::unique_ptr<odb::database> db(new odb::pgsql::database(conninfo));

Expand Down