Skip to content

Commit

Permalink
Merge pull request #48 from CSC207-2024/backend-api
Browse files Browse the repository at this point in the history
feat(database): a minimal KV database API implementation
  • Loading branch information
MinecraftFuns authored Aug 7, 2024
2 parents dfe7d62 + 37fbc9b commit b2a8e64
Show file tree
Hide file tree
Showing 5 changed files with 302 additions and 217 deletions.
1 change: 1 addition & 0 deletions .scripts/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
deploy.log
database_secrets.json
64 changes: 64 additions & 0 deletions .scripts/generate_database_secrets.py
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)
17 changes: 17 additions & 0 deletions database/README.md
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`
Loading

0 comments on commit b2a8e64

Please sign in to comment.