-
Notifications
You must be signed in to change notification settings - Fork 1
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 #48 from CSC207-2024/backend-api
feat(database): a minimal KV database API implementation
- Loading branch information
Showing
5 changed files
with
302 additions
and
217 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
deploy.log | ||
database_secrets.json |
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,64 @@ | ||
import base64 | ||
import json | ||
import os | ||
import subprocess | ||
|
||
current_dir = os.path.dirname(os.path.abspath(__file__)) | ||
root_dir = os.path.dirname(current_dir) | ||
database_dir = os.path.join(root_dir, "database") | ||
output_path = os.path.join(current_dir, "database_secrets.json") | ||
|
||
users = ( | ||
"[email protected]", | ||
"[email protected]", | ||
"[email protected]", | ||
"[email protected]", | ||
"[email protected]", | ||
) | ||
deployments = ("[email protected]",) | ||
|
||
authorized_users = [] | ||
for user in users: | ||
authorized_users.append( | ||
{ | ||
"user": user, | ||
"token": "test-" + base64.urlsafe_b64encode(os.urandom(36)).decode(), | ||
"comment": "Purpose: Local Test", | ||
} | ||
) | ||
for user in deployments: | ||
authorized_users.append( | ||
{ | ||
"user": user, | ||
"token": "deploy-" + base64.urlsafe_b64encode(os.urandom(36)).decode(), | ||
"comment": "Purpose: Deployment", | ||
} | ||
) | ||
|
||
|
||
def deploy(data: list[dict[str, str]]): | ||
fragments = [] | ||
|
||
def convert_dict(data: dict[str, str]) -> str: | ||
fragments = [] | ||
for key, value in data.items(): | ||
fragments.append(f"{key} = {json.dumps(value)}") | ||
# print(fragments) | ||
return "{{ {} }}".format(", ".join(fragments)) | ||
|
||
for part in data: | ||
fragments.append(convert_dict(part)) | ||
|
||
result = "[ {} ]".format(", ".join(fragments)) | ||
print("len(result) =", len(result)) | ||
os.chdir(database_dir) | ||
subprocess.run( | ||
args=("wsl", "npx", "wrangler", "secret", "put", "AUTHORIZED_USERS"), | ||
input=result.encode(), | ||
) | ||
os.chdir(current_dir) | ||
|
||
|
||
deploy(authorized_users) | ||
with open(output_path, "w", encoding="utf-8") as fout: | ||
json.dump(authorized_users, fout, indent=2) |
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,17 @@ | ||
# Database API | ||
|
||
This is a lightweight key-value (KV) database API implementation using Cloudflare KV as the database provider. It takes inspiration from MongoDB's concept of collections, so each item belongs to a collection—or a "namespace" for those familiar with C++ programming. The database ensures reasonable isolation between collections, so average users cannot access data across collections. | ||
|
||
The API is accessible at <https://csc207-db.joefang.org/>. Every request must be authenticated using the access token that has been sent to your emails. Please NEVER store the token in your code; instead, set it as an environment variable. For interoperability, please use `SAFETO_DB_TOKEN` as the name of the environment variable. | ||
|
||
## Action: `get` | ||
|
||
Endpoint: | ||
|
||
## Action: `put` | ||
|
||
## Action: `delete` | ||
|
||
## Action: `history` | ||
|
||
## Action: `list` |
Oops, something went wrong.