Starting with a reminder, the configuration file is a .py file, which means you can code in it, making a more dynamic configuration, and that’s what we did.
Starting top to bottom:
Over here we start the actual configuration, and addressing the elephant in the room… Yes the configuration is in a variable CONFIG, which is a dictionary.
CORS (Cross-Origin Resource Sharing), controls the access to reasources for external request.
* means all
Logging options for the entire server, from the message format, date format to handlers
List of search engine connections to handle, all the connections can be access via the connection manager, all across the code
From the properties, the ones to highlight are:
Reference name, you gave to this engine connection, by default is the constant DEFAULT_ENGINE_NAME (= Elastic), but you can choose a different name
You can add the name as a constant in the file utils/constants/__init__.py, that way you can reference your engine name anywhere in the code
Type of search engine, currently Opensearch or Elasticsearch, by default EngineTypes.ELASTIC
Use enum class EngineTypes, you can import EngineType with
from models.engines import EngineTypes
You can get a more detail explanation in Engine Framework
In security section we found authentication, encryption, and roles (currently under development)
Secret Key
You can get a more detail explanation in Security
Search API also allows to configure a mail service, to sent emails based on templates extension .tlp
Currently supports sending emails via Gmail or a custom SMTP
If enable, it will accept messages via web socket with the activity done in the UI (if one is used)
It will also record activity performed in the server, where it has been recorded
Each message is sent to the default engine connection, to an analytics index to be later process
If you need to modify configuration for different environments, you need to create this new configuration in config/env. This configuration files can be exactly the same as the default configuration in config/config.py or just fragments of the overwrite configuration needed, like in the example below
from os.path import join from models.security import AuthenticationType from utils.constants import SERVER_PATH CONFIG = { 'security': { 'authentication': { 'enabled': True, 'type': AuthenticationType.LOCAL, 'local': { 'file': join(SERVER_PATH, 'config', 'auth', 'users.csv') }, } } }
This configuration will just overwrite the security authentication section, enabling the local type authentication and adding the configuration for it, everything else not specified will remain as it is in the default configuration.
No Environment by Default
By default there is not environment set, so only config.py applies
To specify which environment file you want to use, there are 2 options from which to choose, in both cases the environment name must match the name of the environment file
Environment name = Environment File Name
python uvicorn_server.py --env=production
Or if executing in docker
CMD ["uvicorn", "app.webapp:app", "--host", "0.0.0.0", "--port", "8085", "--env", "production"]
To set an environment variable in Windows using CMD, you can use the set command:
:: Set a temporary environment variable for the current session set VARIABLE_NAME=value :: Set a permanent environment variable for the current user (requires admin privileges) setx VARIABLE_NAME value :: Set a permanent environment variable for all users (requires admin privileges) setx VARIABLE_NAME value /M
To set an environment variable in PowerShell, you can use either Set-Item or New-ItemProperty cmdlets:
# Set a temporary environment variable for the current session $env:VARIABLE_NAME = "value" # Set a permanent environment variable for the current user (requires admin privileges) New-ItemProperty -Path "HKCU:\Environment" -Name "VARIABLE_NAME" -Value "value" -Force # Set a permanent environment variable for all users (requires admin privileges) New-ItemProperty -Path "HKLM:\System\CurrentControlSet\Control\Session Manager\Environment" -Name "VARIABLE_NAME" -Value "value" -PropertyType "String" -Force
To set an environment variable in macOS or Linux using the terminal (bash), you can use the export
command:
# Set a temporary environment variable for the current session export VARIABLE_NAME=value # Set a permanent environment variable for the current user echo 'export VARIABLE_NAME=value' >> ~/.bashrc # After updating the configuration file, apply the changes without restarting source ~/.bashrc
The Search API provides access to the Server configuration through the use of SERVER_CONFIG from the config module. By importing SERVER_CONFIG, developers can access all the server configuration properties as if they were regular objects. This allows for seamless interaction with and customization of the server configuration within your application.
To access the Server configuration using the Search API, follow these steps:
Import the SERVER_CONFIG object from the config module in your Python code.
Utilize SERVER_CONFIG to access the desired server configuration properties.
from config import SERVER_CONFIG # Accessing the server configuration properties host = SERVER_CONFIG.host port = SERVER_CONFIG.port auth_type = SERVER_CONFIG.security.authentication.type # Use the server configuration properties in your application print(f"Server Host: {host}") print(f"Server Port: {port}") print(f"Max Results: {auth_type}")
In the above example, the SERVER_CONFIG object is imported from the config module. The server configuration properties such as host, port, and auth_type are accessed directly from SERVER_CONFIG as regular object attributes. These properties can then be used within your application for further processing or customization.
Ensure that the config module is properly imported and available in your application environment to access the SERVER_CONFIG object.
The current default configuration can be found here in case the sample below is outdated config.py