Skip to content

professoridx-del/AstraCDN

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

21 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AstraCDN Logo

AstraCDN

AstraCDN Banner

Tech Stack

Astro β€’ Cloudflare Workers β€’ Discord


A simple and fast CDN built for the modern web

Deploy License Discord


✨ Features

  • ⚑ 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

πŸ› οΈ Getting Started

Prerequisites

Before you begin, ensure you have the following installed:

πŸ“₯ Installation

  1. Clone the repository

    git clone https://github.com/swadhinbiswas/AstraCDN.git
    cd AstraCDN
  2. Install dependencies

    npm install
  3. Configure environment variables

    Create a .env file 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)
  4. Start the development server

    npm run dev

    Your CDN should now be running at http://localhost:4321 πŸŽ‰


πŸš€ Deployment

Deploy your CDN to Cloudflare Workers in two simple steps:

  1. Build the project

    npm run build
  2. Deploy to Cloudflare

    wrangler deploy

⚑ Cloudflare CDN Activation

Supercharge your deployment with Cloudflare's global CDN network:

Step 1: Get a Domain

Purchase a domain from any domain registrar if you don't already have one.

Step 2: Transfer to Cloudflare

  1. Sign up at cloudflare.com
  2. Add your domain through the setup wizard
  3. Update your domain's nameservers to Cloudflare's nameservers
  4. Wait for DNS propagation (typically a few hours, max 48 hours)

Step 3: Configure DNS

  • Point your A records or CNAME records to your Cloudflare Workers deployment
  • Cloudflare will automatically cache and serve your content globally

Step 4: Optimize (Optional)

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!


πŸ€– Discord Integration

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.


AstraCDN - Cloudflare Deployment Guide

This document provides a comprehensive guide to deploy your AstraCDN application to Cloudflare Workers.

Prerequisites

Before deploying your AstraCDN application, ensure you have:

  1. Node.js (v18 or higher)
  2. Git
  3. A Discord Bot with access to a channel where images can be uploaded
  4. Turso connection string (or self-hosted)
  5. Wrangler CLI installed globally (optional): npm install -g wrangler

Environment Variables Setup

Your application requires the following environment variables:

  • TURSO_URL: Your Turso database URL
  • TURSO_AUTH_TOKEN: Your Turso auth token
  • DB_NAME: Database name (defaults to "AstraCDN" in wrangler.toml)
  • BOT_TOKEN: Your Discord bot token
  • CHANNEL_ID: The Discord channel ID where images will be uploaded

Deployment Steps

1. Install Dependencies

npm install

2. Configure Environment Variables

Before 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_ID

When prompted, enter the corresponding values for each secret.

3. Build the Application

npm run build

This creates the optimized build in the dist/ directory.

4. Deploy to Cloudflare

npx wrangler deploy

5. Custom Domain (Optional)

To use a custom domain with your deployed application:

  1. Go to your Cloudflare Dashboard
  2. Navigate to "Workers & Pages" β†’ "Overview" β†’ "Domains"
  3. Click "Add domain"
  4. Enter your subdomain (e.g., cdn.yourdomain.com)
  5. Follow the instructions to set up DNS records

Configuration Details

wrangler.toml

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 debugging

Astro Configuration

Your 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...
});

Application Architecture

AstraCDN operates as follows:

  1. 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
  2. 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

Performance Considerations

  • 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

Monitoring and Logs

  • 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

Troubleshooting

Common Issues:

  1. Upload errors: Check that your Discord bot has proper permissions in the channel
  2. Database errors: Verify your Turso connection string is correct
  3. 404 on file access: Ensure the file exists in the Discord channel and database

Debugging:

Check logs via the Cloudflare Dashboard or use:

npx wrangler tail

This provides real-time logs from your deployed worker.

Updating Your Application

To update your deployed application:

  1. Make your code changes
  2. Test locally: npm run dev
  3. Build the application: npm run build
  4. Deploy: npx wrangler deploy

Security Best Practices

  • 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

Cost Considerations

  • 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.

πŸ—οΈ Project Structure

AstraCDN/
β”œβ”€β”€ public/
β”‚   β”œβ”€β”€ icon.svg
β”‚   └── image.png
β”œβ”€β”€ src/
β”‚   └── ...
β”œβ”€β”€ .env
β”œβ”€β”€ package.json
└── README.md

🀝 Contributing

Contributions are welcome! Feel free to:

  • πŸ› Report bugs
  • πŸ’‘ Suggest new features
  • πŸ”§ Submit pull requests

πŸ‘¨β€πŸ’» Creator


πŸ“„ License

This project is open source and available under the MIT License.


⬆ Back to Top

Made with ❀️ by Swadhin Biswas

About

Free, Fast, and Simple Image Hosting

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Astro 53.4%
  • TypeScript 36.1%
  • CSS 9.0%
  • JavaScript 1.5%