- β‘ Lightning Fast: Built on Cloudflare Workers for edge-first delivery
- π Global Scale: Automatically distributed across Cloudflare's global network
- π€ Discord Integration: Seamless notifications and monitoring via Discord
- π Easy Deploy: Get your CDN running in minutes, not hours
- π¨ Customizable: Tailor the CDN to your specific needs
- π¦ Modern Stack: Built with cutting-edge technologies
Before you begin, ensure you have the following installed:
- Node.js (v18 or higher)
- Git
- A Discord Bot Token
-
Clone the repository
git clone https://github.com/swadhinbiswas/AstraCDN.git cd AstraCDN -
Install dependencies
npm install
-
Configure environment variables
Create a
.envfile in the root directory:TURSO_URL="your-turso-database-url" TURSO_AUTH_TOKEN="your-turso-auth-token" DB_NAME="AstraCDN" BOT_TOKEN="your-discord-bot-token" CHANNEL_ID="your-discord-channel-id"
How to get your Discord credentials:
- BOT_TOKEN: Create a bot at Discord Developer Portal
- CHANNEL_ID: Right-click on a Discord channel and select "Copy ID" (requires Developer Mode enabled)
-
Start the development server
npm run dev
Your CDN should now be running at
http://localhost:4321π
Deploy your CDN to Cloudflare Workers in two simple steps:
-
Build the project
npm run build
-
Deploy to Cloudflare
wrangler deploy
Supercharge your deployment with Cloudflare's global CDN network:
Purchase a domain from any domain registrar if you don't already have one.
- Sign up at cloudflare.com
- Add your domain through the setup wizard
- Update your domain's nameservers to Cloudflare's nameservers
- Wait for DNS propagation (typically a few hours, max 48 hours)
- Point your A records or CNAME records to your Cloudflare Workers deployment
- Cloudflare will automatically cache and serve your content globally
Enable additional features in your Cloudflare dashboard:
- Browser Cache Expiration - Control client-side caching
- Development Mode - Bypass cache during testing
- Always Online - Serve cached versions if origin is down
- Polish - Automatic image optimization
- Rocket Loader - JavaScript optimization
π‘ Pro Tip: Simply pointing your domain to Cloudflare automatically activates the CDN. No additional code changes needed!
AstraCDN includes Discord integration for real-time notifications and monitoring:
- Upload Notifications: Get notified when files are uploaded
- CDN Analytics: Monitor usage and performance metrics
- Error Alerts: Receive instant alerts for critical issues
Make sure to configure your BOT_TOKEN and CHANNEL_ID in the .env file to enable Discord features.
This document provides a comprehensive guide to deploy your AstraCDN application to Cloudflare Workers.
Before deploying your AstraCDN application, ensure you have:
- Node.js (v18 or higher)
- Git
- A Discord Bot with access to a channel where images can be uploaded
- Turso connection string (or self-hosted)
- Wrangler CLI installed globally (optional):
npm install -g wrangler
Your application requires the following environment variables:
TURSO_URL: Your Turso database URLTURSO_AUTH_TOKEN: Your Turso auth tokenDB_NAME: Database name (defaults to "AstraCDN" in wrangler.toml)BOT_TOKEN: Your Discord bot tokenCHANNEL_ID: The Discord channel ID where images will be uploaded
npm installBefore deploying, you must set your environment secrets using the Wrangler CLI:
# Set your Discord bot token
npx wrangler secret put BOT_TOKEN
# Set your Turso URL
npx wrangler secret put TURSO_URL
# Set your Turso Auth Token
npx wrangler secret put TURSO_AUTH_TOKEN
# Set your Discord channel ID
npx wrangler secret put CHANNEL_IDWhen prompted, enter the corresponding values for each secret.
npm run buildThis creates the optimized build in the dist/ directory.
npx wrangler deployTo use a custom domain with your deployed application:
- Go to your Cloudflare Dashboard
- Navigate to "Workers & Pages" β "Overview" β "Domains"
- Click "Add domain"
- Enter your subdomain (e.g.,
cdn.yourdomain.com) - Follow the instructions to set up DNS records
The wrangler.toml file in your project is configured for optimal deployment:
name = "astracdn" # Your worker name
main = "dist/_worker.js" # Entry point for your application
account_id = "86d20513ca452bfb3c1c1de241e869f6" # Your Cloudflare account ID
compatibility_date = "2024-03-12"
compatibility_flags = ["nodejs_compat"] # Enables Node.js compatibility
# Environment variables
[vars]
DB_NAME = "AstraCDN"
[build]
command = "npm run build"
[observability.logs]
enabled = true # Enables logging for debuggingYour astro.config.mjs is already configured with the Cloudflare adapter:
import cloudflare from '@astrojs/cloudflare';
export default defineConfig({
output: 'server', // Server-side rendering
adapter: cloudflare(), // Cloudflare adapter
// Additional configuration...
});AstraCDN operates as follows:
-
Upload Process:
- Client uploads an image to
/api/upload - Application stores the image in a Discord channel via the Discord API
- Metadata is stored in Turso
- Returns a URL like
/api/file/{fileId}for accessing the image
- Client uploads an image to
-
Retrieval Process:
- When someone accesses
/api/file/{fileId}, the application:- Retrieves the Discord message metadata from Turso
- Fetches the image from Discord CDN
- Serves the image with appropriate headers and caching
- When someone accesses
- The application uses Discord's fast global CDN for image delivery
- Images are cached in the worker with a 1-hour cache-control header
- The application is deployed on Cloudflare's global network for low latency
- Access logs via the Cloudflare Dashboard under Workers
- The application has observability enabled to track requests and errors
- Monitor your Turso logs for database performance
- Upload errors: Check that your Discord bot has proper permissions in the channel
- Database errors: Verify your Turso connection string is correct
- 404 on file access: Ensure the file exists in the Discord channel and database
Check logs via the Cloudflare Dashboard or use:
npx wrangler tailThis provides real-time logs from your deployed worker.
To update your deployed application:
- Make your code changes
- Test locally:
npm run dev - Build the application:
npm run build - Deploy:
npx wrangler deploy
- Store sensitive information (Discord token, Turso URL, Turso Auth Token) as secret variables
- Use environment-specific configuration
- Regularly rotate your Discord bot token
- Monitor your application logs for unusual activity
- Cloudflare Workers: First 100,000 requests per day are free
- Turso: Offers a generous free tier for development
- Discord: No direct costs for storing images in channels
For production usage, review the pricing of each service based on your expected traffic.
AstraCDN/
βββ public/
β βββ icon.svg
β βββ image.png
βββ src/
β βββ ...
βββ .env
βββ package.json
βββ README.md
Contributions are welcome! Feel free to:
- π Report bugs
- π‘ Suggest new features
- π§ Submit pull requests
This project is open source and available under the MIT License.
Made with β€οΈ by Swadhin Biswas
