-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from OpenMined/eelco/server-settings
add server settings, remove server globals
- Loading branch information
Showing
5 changed files
with
270 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from pathlib import Path | ||
|
||
from fastapi import Request | ||
from pydantic_settings import BaseSettings, SettingsConfigDict | ||
|
||
|
||
class ServerSettings(BaseSettings): | ||
""" | ||
Reads the server settings from the environment variables, using the prefix SYFTBOX_. | ||
example: | ||
`export SYFTBOX_DATA_FOLDER=data/data_folder` | ||
will set the server_settings.data_folder to `data/data_folder` | ||
see: https://docs.pydantic.dev/latest/concepts/pydantic_settings/#parsing-environment-variable-values | ||
""" | ||
|
||
model_config = SettingsConfigDict(env_prefix="SYFTBOX_") | ||
|
||
data_folder: Path = Path("data") | ||
snapshot_folder: Path = Path("data/snapshot") | ||
user_file_path: Path = Path("data/users.json") | ||
|
||
@property | ||
def folders(self) -> list[Path]: | ||
return [self.data_folder, self.snapshot_folder] | ||
|
||
|
||
def get_server_settings(request: Request) -> ServerSettings: | ||
return request.state.server_settings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import os | ||
from pathlib import Path | ||
|
||
from syftbox.server.settings import ServerSettings | ||
|
||
|
||
def test_server_settings_from_env(): | ||
os.environ["SYFTBOX_DATA_FOLDER"] = "data_folder" | ||
os.environ["SYFTBOX_SNAPSHOT_FOLDER"] = "data_folder/snapshot_folder" | ||
os.environ["SYFTBOX_USER_FILE_PATH"] = "data_folder/user_file_path.json" | ||
|
||
settings = ServerSettings() | ||
print(settings) | ||
assert settings.data_folder == Path("data_folder") | ||
assert settings.snapshot_folder == Path("data_folder/snapshot_folder") | ||
assert settings.user_file_path == Path("data_folder/user_file_path.json") |
Oops, something went wrong.