Redirect service allows the configuration of “domain pools” which define the target domains to redirect to, and weight associated with each domain.
The redirect endpoint (http://localhost:8006//v1/redirect_domain/{pool_id}) chooses a random domain of the given pool based on its weight and forces an HTTP redirect to it; Configuration can be done by using the service OpenAPI page. Any custom UI is out of scope.
- Docker
- FastAPI
- MySQL
- SQLAlchemy
Please install Docker
and Docker compose
first.
After installation, run the following command to create a local Docker container.
docker-compose up --build
If Docker is running successfully, the API and DB server will be launched as shown in the following:
- API server: http://localhost:8006
- API Docs: http://localhost:8006/v1/docs
- API ReDoc Docs: http://localhost:8006/v1/redoc
- DB server: http://localhost:3308
Be careful, it won't work if the port is occupied by another application.
The above setup can be used for development, but you can also setup dev env with using the VS Code Dev Containers extension.
- Install VS code and the Dev Containers extension.
- Run the
Dev Containers: Open Folder in Container...
command from the Command Palette or quick actions Status bar item, and select the project folder. - Wait until the building of the application is finished, then access the application url
If you're VS Code user, you can easily setup Python code formatter (black) and linter (flake8) by simply installing the extensions.
Automatic formatting settings have already been defined .vscode/settings.json
.
Just install following:
If you are using the Dev Container, this configuration is already done in the Dev Container settings, so you can skip it.
Use following command to go inside of docker container:
docker-compose exec redirect_db sh
Then use mysql
command to execute a query:
mysql -u appuser -p
mysql> USE redirect_app;
mysql> SHOW TABLES;
mysql> SELECT * FROM user;
Your initial MySQL password is defined in mysql/local.env
.
To reinitialize the DB, remove the named volumes declared in the "volumes" section of the Compose file.
https://docs.docker.com/engine/reference/commandline/compose_down/
docker-compose down -v
Python libraries used in this app are defined in api/requirements.txt
.
Also you may want to add libraries such as requests, in which case follow these steps:
- Add the library to requirements.txt
e.g., if you want to add requests
:
requests==2.32.3
Then try a re-build and see.
docker-compose down; docker-compose build; docker-compose up;
Some environment variables, like a database name and user are defined in docker-compose.yml
or Dockerfile
.
Then, run docker-compose up
to launch the development environment.
And confirm that your DB changes are reflected.
-
Create the pools and domains by using the OpenAPI helper page: http://localhost:8006/v1/docs#/Pools/create_pool_resource_v1_pools_post
You can use this sample payload:
{ "name": "First Pool", "domains": [ { "domain": "http://abc.com", "weight": 2 }, { "domain": "http://xyz.com", "weight": 6 }, { "domain": "http://google.com", "weight": 8 } ] }
-
Confirm the pool and domains were created by using http://localhost:8006/v1/docs#/Pools/get_pools_resource_v1_pools_get
-
Once the pools and domains are created you can use the redirect service with the pool id like this: http://localhost:8006/v1/redirect_domain/1 which will redirect you using the logic in the service description.
-
Logs for database statements as well as requests and responses are saved into the database thorough a middleware and a logger handler, you can review them in http://localhost:8006/v1/docs#/Logs/get_logs_resource_v1_logs_get