The backend of this project is built using the Hono framework for API routes, SQLite via Val Town's std/sqlite module for data persistence, and OAuth middleware for authentication.
BACKEND/ ├── README.md # This file ├── routes.users.ts # Hono API routes for user operations └── storage.users.ts # SQLite database operations for users
To maintain consistency and clarity across the backend codebase, follow these naming conventions:
Route files should follow the pattern:
routes.{resource}.ts
Examples:
routes.users.ts - User-related API endpointsroutes.posts.ts - Post-related API endpointsroutes.comments.ts - Comment-related API endpointsStorage files (database operations) should follow the pattern:
storage.{resource}.ts
Examples:
storage.users.ts - User database operationsstorage.posts.ts - Post database operationsstorage.comments.ts - Comment database operationsPurpose: Hono API routes for user operations.
Key Features:
/user endpoint (GET) to retrieve current user informationstorage.users.ts for database operationsEndpoints:
| Method | Path | Description | Auth Required |
|---|---|---|---|
| GET | /user | Get current user info | Yes |
Example Response:
{ "user": { "id": 1, "email": "user@example.com", "username": "johndoe", "profile_image_url": "https://example.com/avatar.jpg" } }
Purpose: SQLite database operations for user management.
Key Features:
users_v1 table (configured in shared/config.js)std/sqlite moduleExported Functions:
| Function | Description | Parameters | Returns |
|---|---|---|---|
getOrCreateUser | Retrieves an existing user by email or creates a new one | email, username?, profileImageUrl? | Promise<User> |
User Schema:
| Column | Type | Constraints |
|---|---|---|
id | INTEGER | PRIMARY KEY AUTOINCREMENT |
email | TEXT | UNIQUE NOT NULL |
username | TEXT | Optional |
profile_image_url | TEXT | Optional |
created_at | TEXT | DEFAULT CURRENT_TIMESTAMP |
updated_at | TEXT | DEFAULT CURRENT_TIMESTAMP |
Type Definition: See shared/types.ts for the User interface.
Hono is a lightweight, fast web framework for building API routes. It provides:
Val Town provides a hosted SQLite service via https://esm.town/v/std/sqlite/main.ts:
Authentication is handled by Val Town's OAuth middleware (https://esm.town/v/std/oauth/middleware.ts):
/auth/login and /auth/logout routesgetOAuthUserData() to retrieve session informationWhen adding a new resource to the backend:
Create the storage file:
storage.{resource}.tsCreate the routes file:
routes.{resource}.tsUpdate the main app:
app.http.ts/api prefixAdd types (if needed):
shared/types.ts with new interfacesUpdate configuration (if needed):
shared/config.jsusers_v2) rather than altering the existing tablegetOAuthUserData() to check authentication status in protected routesDeno.env.get() for secrets, though prefer APIs that don't require keys when possible