diff --git a/.env.dev b/.env.dev deleted file mode 100644 index 6b0c291..0000000 --- a/.env.dev +++ /dev/null @@ -1,5 +0,0 @@ -POSTGRES_HOST=localhost -POSTGRES_PORT=5432 -POSTGRES_DB=to-dos-api-cpp_db -POSTGRES_USER=postgres -POSTGRES_PASSWORD=admin \ No newline at end of file diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..ba3f92e --- /dev/null +++ b/.env.example @@ -0,0 +1,6 @@ +# Database +POSTGRES_HOST=tobemodified +POSTGRES_PORT=tobemodified +POSTGRES_DB=tobemodified +POSTGRES_USER=tobemodified +POSTGRES_PASSWORD=tobemodified \ No newline at end of file diff --git a/README.md b/README.md index 34c6b7d..d88f327 100644 --- a/README.md +++ b/README.md @@ -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.

cmakeLaunch

diff --git a/docker-compose.dev.yaml b/docker-compose.yml similarity index 86% rename from docker-compose.dev.yaml rename to docker-compose.yml index 5aeaa82..37cd437 100644 --- a/docker-compose.dev.yaml +++ b/docker-compose.yml @@ -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: \ No newline at end of file + postgresql-data: diff --git a/src/data/alembic.ini b/src/data/alembic.ini index 8666a89..fe66372 100644 --- a/src/data/alembic.ini +++ b/src/data/alembic.ini @@ -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 diff --git a/src/data/migrations/env.py b/src/data/migrations/env.py index bcdb47f..f960b3f 100644 --- a/src/data/migrations/env.py +++ b/src/data/migrations/env.py @@ -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. @@ -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, @@ -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.", diff --git a/src/main.cpp b/src/main.cpp index 794a34e..6f08590 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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 db(new odb::pgsql::database(conninfo));