A beginner-friendly project that connects a Svelte frontend with a Flask backend using SQLite for data storage. This project demonstrates how to build a simple full-stack web application with a blog-style interface that supports post-specific comments and role-based access control.
This project consists of two main parts:
The frontend makes API calls to the backend to fetch data, which is then displayed to the user.
old_svelte/
├── backend/ # Flask backend application
│ ├── app.py # Main Flask application
│ ├── schema.sql # SQL schema for database (users and comments)
│ ├── init_db.py # Script to initialize/reset the local database
│ ├── scripts/ # Utility scripts for administration
│ │ ├── list_users.py # Script to list all users in the database
│ │ └── admin_scripts/ # Admin user management scripts (gitignored)
│ ├── requirements.txt # Python dependencies
│ ├── database.db # Local SQLite database file (Gitignored)
│ └── build/ # Build, deployment and test directories
│ ├── deploy/ # Deployment scripts and configurations
│ │ ├── deploy-to-azure.sh # Script to deploy backend to Azure
│ │ └── debug_azure.py # Debug script for Azure DB issues
│ ├── tests/ # Backend tests (pytest)
│ └── pytest.ini # Pytest configuration
│
├── frontend/ # Svelte frontend application
│ ├── public/ # Static assets and HTML template
│ ├── src/ # Source code
│ │ ├── components/ # UI components
│ │ │ ├── Navbar.svelte
│ │ │ ├── Home.svelte
│ │ │ ├── Post.svelte
│ │ │ ├── CreatePost.svelte
│ │ │ ├── Comments.svelte
│ │ │ ├── Footer.svelte
│ │ │ └── About.svelte
│ │ ├── App.svelte # Main application component with routing
│ │ ├── main.js # Entry point
│ │ ├── authStore.js # Authentication and user role management
│ │ └── config.js # Backend API URL configuration
│ ├── package.json # NPM dependencies and scripts
│ └── rollup.config.js # Rollup bundler configuration
│
├── .github/ # (Removed - Workflows deleted)
├── .venv/ # Python virtual environment (Gitignored)
├── .gitattributes
└── README.md # This documentation file
Navigate to the backend directory:
cd backend
Create and activate a virtual environment (recommended):
python -m venv ../.venv # Create venv in project root
source ../.venv/bin/activate # On Linux/macOS
# or ..\\.venv\\Scripts\\activate on Windows
Install dependencies:
pip install -r requirements.txt
Initialize the local database with sample data (only needed once or for reset):
python init_db.py
Warning: This script drops existing tables. For production, database initialization is handled differently.
Create an admin user (optional, test_user1 is already an admin):
python scripts/admin_scripts/create_admin.py
# Or use the convenience script:
./scripts/admin_scripts/make_admin.sh myusername mypassword
Run the Flask application for local development: ```bash
export FLASK_DEBUG=true # Linux/macOS
python app.py
The backend will be available at http://localhost:5001.
### Frontend (Svelte)
1. Navigate to the frontend directory:
```bash
cd frontend
Install dependencies:
npm install
Run the development server:
npm run dev
The frontend will be available at http://localhost:8080
This application implements role-based access control with two main roles:
Admin:
Regular User:
By default, the following test users are created:
test_user1
with password password1
test_user2
with password password2
You can also create a custom admin user with:
cd backend
python scripts/admin_scripts/create_admin.py <username> <password>
# Or use the convenience script:
./scripts/admin_scripts/make_admin.sh <username> <password>
The RBAC system is implemented through:
The backend/build/tests/
directory contains automated tests for the Flask application using pytest
.
To run these tests and generate coverage reports, please refer to the instructions in the backend/build/tests/README.md
file.
To specifically test role-based access control functionality:
cd backend
python build/tests/run_coverage.py --rbac
The frontend is a blog application with the following features:
/post/:id
The backend provides these API endpoints:
GET /api/health - Health check endpoint to verify the backend is running
{"status": "ok", "message": "Flask backend is running"}
GET /api/users - Retrieves the list of users from the database
GET /api/posts - Retrieves all blog posts
GET /api/posts/:id - Retrieves a specific post by ID
POST /api/posts - Creates a new post (admin only)
DELETE /api/posts/:id - Deletes a post by ID (admin only)
PUT /api/posts/:id - Updates a post (owner or admin only)
GET /api/posts/:post_id/comments - Retrieves all comments for a specific post
POST /api/posts/:post_id/comments - Adds a new comment to a specific post
POST /api/login - Authenticates a user and returns a JWT
POST /api/register - Registers a new user
GET /api/admin/users - Admin-only endpoint that returns detailed user information
The project uses a flexible configuration system that allows the frontend to connect to different backend environments:
http://localhost:5001
https://sarada-hbegajbsfxekdyex.canadacentral-01.azurewebsites.net
This configuration is managed in frontend/src/config.js
and automatically detects which environment is being used.
The backend is configured with proper CORS headers to allow the frontend to make cross-origin requests for all HTTP methods (GET, POST, DELETE) regardless of where it's hosted.
src/main.js: The entry point of the application. It creates and mounts the main Svelte component.
src/App.svelte: The main component that sets up routing and includes the Navbar and Footer components.
src/components/Navbar.svelte: Navigation component with client-side routing.
src/components/Home.svelte: Blog listing page that displays a list of posts.
src/components/Post.svelte: Individual post view that displays a single blog post based on the ID and includes the Comments component.
src/components/Comments.svelte: Component for displaying, adding, and deleting comments for a specific post.
src/components/Footer.svelte: Simple footer with credits.
src/components/About.svelte: Simple about page with information.
public/index.html: The HTML template that loads the bundled JavaScript and CSS.
package.json: Contains the project metadata, dependencies, and npm scripts.
rollup.config.js: Configuration for the Rollup bundler that builds the project.
src/config.js: Manages backend API URL configuration based on the current environment.
app.py: The main Flask application that defines API routes, database connection, and handles requests.
schema.sql: Defines the database tables and structure (users and comments).
init_db.py: Script that initializes the database with the schema and adds sample users and comments.
scripts/admin_scripts/create_admin.py: Script to create an admin user.
requirements.txt: Lists the Python packages needed to run the application.
database.db: The SQLite database file that stores the data.
build/deploy/deploy-to-azure.sh: The primary script for deploying the backend to Azure App Service. It packages the application, handles dependencies, and configures Azure for persistent database storage.
build/deploy/debug_azure.py: An optional utility script to help diagnose database persistence issues directly on the Azure App Service environment if needed.
The backend provides these API endpoints:
GET / - Simple test endpoint that verifies database connection
GET /api/health - Health check endpoint to verify the backend is running
{"status": "ok", "message": "Flask backend is running"}
GET /api/users - Retrieves the list of users from the database
GET /api/posts - Retrieves all blog posts
GET /api/posts/:id - Retrieves a specific post by ID
POST /api/posts - Creates a new post (admin only)
DELETE /api/posts/:id - Deletes a post by ID (admin only)
PUT /api/posts/:id - Updates a post (owner or admin only)
GET /api/posts/:post_id/comments - Retrieves all comments for a specific post
POST /api/posts/:post_id/comments - Adds a new comment to a specific post
POST /api/login - Authenticates a user and returns a JWT
POST /api/register - Registers a new user
GET /api/admin/users - Admin-only endpoint that returns detailed user information
To fully implement the blog functionality, consider adding these API endpoints:
fetch
APIThe connection between frontend and backend is enabled by CORS (Cross-Origin Resource Sharing), which allows the frontend running on port 8080 to access the backend running on port 5001.
database.db
file within the /home/site/wwwroot/data
directory on your Azure App Service (using Kudu tools or SSH) and redeploying. Warning: This will delete all existing comments.az webapp log tail --resource-group YOUR_RESOURCE_GROUP --name YOUR_APP_NAME