This project provides a simple API for retrieving GitHub user statistics, built using Cloudflare Workers and Hono. The API caches responses to reduce the number of requests made to the GitHub API.
- Retrieves GitHub user statistics, including repository information
- Caches responses to reduce the number of requests made to the GitHub API
- Built using Cloudflare Workers and Hono for high performance and scalability
- Node.js (version 14 or higher)
- npm (version 6 or higher)
- Cloudflare Workers CLI (version 1.0.0 or higher)
- Clone the repository:
git clone https://github.com/gifflet/github-stats-cloudflare-worker
- Install dependencies:
npm install
- Configure the project:
cp wrangler.toml wrangler.toml.example
and update thewrangler.toml
file with your Cloudflare API credentials
GET /:username/repositories
: Retrieves paginated GitHub repositories for the specified username- Query Parameters:
page
: Page number (default: 1)per_page
: Items per page (default: 30)
- Example:
curl https://your-worker-url.com/gifflet/repositories?page=1&per_page=30
GET /:username/metrics
: Retrieves cached GitHub metrics for the specified username- Example:
curl https://your-worker-url.com/gifflet/metrics
GET /:username/badge
: Generates a dynamic SVG badge with user statistics- Example:
- Usage in markdown:
![GitHub Stats](https://your-worker-url.com/gifflet/badge)
- Direct access:
curl https://your-worker-url.com/gifflet/badge
- Usage in markdown:
{
"username": "gifflet",
"repositories": [...],
"pagination": {
"current_page": 1,
"per_page": 30,
"has_next_page": true,
"has_previous_page": false
}
}
{
"username": "gifflet",
"total_stars": 38,
"total_forks": 12,
"total_repos": 23,
"most_used_language": "TypeScript",
"updated_at": "2024-01-15T10:00:00.000Z"
}
CACHE
: The name of the KV namespace used for caching responses
wrangler.toml
: Configure the project with your Cloudflare API credentials and KV namespace settings
npx wrangler kv:namespace create <NAMESPACE_NAME>
: Creates a new KV namespace for caching responses.- Example:
npx wrangler kv:namespace create github-stats-api-cache
npx wrangler kv:namespace configure <NAMESPACE_NAME> --binding <BINDING_NAME>
: Configures the KV namespace with a binding name.- Example:
npx wrangler kv:namespace configure github-stats-api-cache --binding CACHE
npx wrangler d1 create <DATABASE_NAME>
: Creates a new D1 database.- Example:
npx wrangler d1 create github-metrics
- Create a migration file:
npx wrangler d1 migrations create <DATABASE_NAME> <MESSAGE>
- Example:
npx wrangler d1 migrations create github_metrics create-tables
- Apply migrations to the remote database:
-
npx wrangler d1 migrations apply <DATABASE_NAME>
: Applies all pending migrations to your database. -
Example:
npx wrangler d1 migrations apply github_metrics --remote
If you want to execute migrations locally (for development), you can use the following command:
npx wrangler d1 migrations apply <DATABASE_NAME> --local
: Applies migrations to local D1 database.- Example:
npx wrangler d1 migrations apply github_metrics --local
npx wrangler d1 execute <DATABASE_NAME> --command "<SQL_QUERY>"
: Executes SQL commands on your D1 database.- Example:
npx wrangler d1 execute github_metrics --command "SELECT * FROM github_metrics"
You can also execute queries locally by adding the --local
flag:
- Example:
npx wrangler d1 execute github_metrics --local --command "SELECT * FROM github_metrics"