An open API service indexing awesome lists of open source software.

https://github.com/montasim/github-readme-counter

A simple web service that generates a dynamic SVG image showing a visitor count. It increments a counter every time the endpoint is called and returns an SVG image displaying the updated count.
https://github.com/montasim/github-readme-counter

api counter customizable-svg dynamic-svg expressjs github glitch nodejs real-time server svg visitor-counter web-application web-service

Last synced: 3 months ago
JSON representation

A simple web service that generates a dynamic SVG image showing a visitor count. It increments a counter every time the endpoint is called and returns an SVG image displaying the updated count.

Awesome Lists containing this project

README

          

# GitHub Readme Counter


GitHub contributors badge
GitHub language count badge
GitHub top language badge
GitHub code size badge
GitHub repo size badge
GitHub file count badge
GitHub open issues badge
GitHub closed issues badge
GitHub open pull requests badge
GitHub closed pull requests badge
GitHub active milestones badge
GitHub completed milestones badge
GitHub license badge
GitHub last commit badge
GitHub Discussions badge

A modern, well-architected web service that generates dynamic SVG images showing visitor counts. Built with Express.js following Clean Code principles, SOLID design patterns, and industry best practices.

## โœจ Features

- **Dynamic SVG Generation**: Creates customizable SVG images with visitor counts
- **Real-time Counter**: Increments on each request with persistent storage
- **Customizable Appearance**: Customize background and text colors via URL parameters
- **Health Check Endpoint**: Monitor service status and uptime
- **Comprehensive Error Handling**: Proper HTTP status codes and error messages
- **Request Logging**: Detailed logging with color-coded status codes
- **Input Validation**: Validates hex color codes with proper error messages
- **Graceful Shutdown**: Handles termination signals properly
- **Backward Compatible**: Supports both old and new query parameter formats

## ๐Ÿ—๏ธ Architecture

This project follows a modular, layered architecture with clear separation of concerns:

```
src/
โ”œโ”€โ”€ config/ # Configuration management
โ”‚ โ”œโ”€โ”€ constants.js # Magic numbers, default values, SVG config
โ”‚ โ”œโ”€โ”€ paths.js # Path configurations
โ”‚ โ””โ”€โ”€ index.js # Export all configs
โ”œโ”€โ”€ services/ # Business logic layer
โ”‚ โ”œโ”€โ”€ CounterService.js # Counter operations (read/write/increment)
โ”‚ โ””โ”€โ”€ ImageService.js # SVG generation
โ”œโ”€โ”€ controllers/ # Request handling layer
โ”‚ โ””โ”€โ”€ CounterController.js # HTTP request handler
โ”œโ”€โ”€ routes/ # Route definitions
โ”‚ โ”œโ”€โ”€ counterRoutes.js # Counter endpoint routes
โ”‚ โ”œโ”€โ”€ healthRoutes.js # Health check endpoint
โ”‚ โ””โ”€โ”€ index.js # Export all routes
โ”œโ”€โ”€ middleware/ # Express middleware
โ”‚ โ”œโ”€โ”€ errorHandler.js # Global error handling
โ”‚ โ”œโ”€โ”€ logger.js # Request/response logging
โ”‚ โ””โ”€โ”€ notFound.js # 404 handler
โ”œโ”€โ”€ utils/ # Utility functions
โ”‚ โ”œโ”€โ”€ validators.js # Input validation
โ”‚ โ””โ”€โ”€ helpers.js # Helper functions
โ”œโ”€โ”€ app.js # Express app setup
โ”œโ”€โ”€ index.js # Entry point with graceful shutdown
โ””โ”€โ”€ counter.txt # Counter storage
```

### Design Principles

- **Single Responsibility Principle**: Each module has a single, well-defined purpose
- **Open/Closed Principle**: Easy to extend without modifying existing code
- **Dependency Inversion**: High-level modules don't depend on low-level modules
- **Clean Code**: Meaningful names, small functions, comprehensive documentation
- **Express.js Best Practices**: Proper middleware chain, error handling, and logging

## ๐Ÿ› ๏ธ Requirements

- [Node.js](https://nodejs.org/en) (>= 20.x)
- [pnpm](https://pnpm.io/) (>= 8.x)

## ๐Ÿš€ Installation

1. **Clone the repository**:
```bash
git clone https://github.com/montasim/github-readme-counter.git
cd github-readme-counter
```

2. **Install dependencies**:
```bash
pnpm install
```

3. **Start the server**:
```bash
pnpm start
```

For development with auto-reload:
```bash
pnpm dev
```

The server will start on the port specified in your environment variables or default to port 3000.

## ๐Ÿ“ก API Endpoints

### Health Check

Check the service status and uptime:

```bash
GET /health
```

**Response**:
```json
{
"status": "ok",
"timestamp": "2026-02-23T05:00:00.000Z",
"uptime": 123.456
}
```

### Counter SVG

Get a dynamic SVG image with the visitor count:

```bash
GET /count.svg
```

**Query Parameters**:

| Parameter | Short | Description | Default |
|-----------|-------|-------------|---------|
| Background Color | `bg` | Hex code for background color | `000000` (black) |
| Text Color | `tc` | Hex code for text color | `EB008B` (magenta) |

> **Note**: For backward compatibility, the old parameter names (`backgroundColor` and `textColor`) are still supported. The new short names take precedence if both are provided.

**Examples**:

Default colors:
```bash
http://localhost:3000/count.svg
```

Custom colors (new parameters):
```bash
http://localhost:3000/count.svg?bg=FFFFFF&tc=0000FF
```

Custom colors (old parameters - still supported):
```bash
http://localhost:3000/count.svg?backgroundColor=FFFFFF&textColor=0000FF
```

Mixed parameters (new takes precedence):
```bash
http://localhost:3000/count.svg?bg=FFFFFF&textColor=0000FF
```

![Default Visitor Count](./default.png)
![Customized Visitor Count](./customized-example.png)

## ๐ŸŽจ Customization

### Color Guidelines

- Provide hex codes **without** the `#` symbol (e.g., `FFFFFF`, not `#FFFFFF`)
- Use 6-digit hex codes (e.g., `000000`, `FFFFFF`, `EB008B`)
- Invalid or unsupported color values will revert to defaults

### Color Examples

| Color | Hex Code | Preview |
|-------|----------|---------|
| Black | `000000` | โฌ› |
| White | `FFFFFF` | โฌœ |
| Red | `FF0000` | ๐ŸŸฅ |
| Green | `00FF00` | ๐ŸŸฉ |
| Blue | `0000FF` | ๐ŸŸฆ |
| Magenta | `EB008B` | ๐Ÿ’œ |
| Cyan | `00B8B5` | ๐Ÿ’  |

## ๐Ÿงช Testing

All features have been tested and verified:

โœ… **Health Check Endpoint**
- Returns 200 status
- Includes service status, timestamp, and uptime

โœ… **Counter Endpoint**
- Returns SVG image with incremented counter
- Default colors work correctly
- Custom colors apply properly

โœ… **Query Parameters**
- New short parameters (`bg`, `tc`) work
- Old long parameters (`backgroundColor`, `textColor`) work
- Mixed parameters work (new takes precedence)
- No parameters use defaults

โœ… **Error Handling**
- Invalid hex codes return 400 status
- Proper error messages displayed
- Error logging with stack traces

โœ… **404 Handler**
- Undefined routes return 404 status
- Consistent 404 response format

โœ… **Logging**
- All requests logged with timestamp and method
- All responses logged with status code and duration
- Color-coded status codes (green for 2xx, yellow for 4xx, red for 5xx)

## ๐Ÿ”ง Error Handling

The service implements comprehensive error handling:

- **400 Bad Request**: Invalid input (e.g., invalid hex color codes)
- **404 Not Found**: Route not found
- **500 Internal Server Error**: Unexpected server errors

All errors are logged with full stack traces for debugging.

## ๐Ÿ“Š Logging

The service provides detailed logging:

- **Request Logging**: Timestamp, HTTP method, and path
- **Response Logging**: Status code and response duration
- **Error Logging**: Full error messages and stack traces
- **Color-coded Output**: Green for success, yellow for warnings, red for errors

## ๐Ÿ”„ Graceful Shutdown

The service handles termination signals properly:

- **SIGTERM**: Graceful shutdown on termination signal
- **SIGINT**: Graceful shutdown on interrupt signal (Ctrl+C)
- **Timeout**: Forces shutdown after 10 seconds if graceful shutdown fails
- **Cleanup**: Properly closes server connections

## ๐Ÿค Contributing

We welcome contributions! Please read [CONTRIBUTION.md](./CONTRIBUTION.md) for details on:
- How to contribute
- Coding style guidelines
- Commit message conventions
- Code review process

## ๐Ÿ“– Author



Moon



๏ผญโ™ข๏ผฎ๏ผดฮ›๏ผณ๏ผฉ๏ผญ



## ๐Ÿ‘ฅ Contributors

[![Contributors Display](https://badges.pufler.dev/contributors/montasim/github-readme-counter?size=50&padding=5&perRow=10&bots=true)](https://badges.pufler.dev)

## ๐Ÿ“ License

This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.

## ๐Ÿ™ Acknowledgments

1. [sagar-viradiya](https://github.com/sagar-viradiya/sagar-viradiya)
2. [pufler.dev/badge-it/](https://pufler.dev/badge-it/)
3. [github-readme-counter](https://github.com/iamskok/github-readme-counter)

## ๐Ÿ“š Additional Documentation

- [IMPLEMENTATION_SUMMARY.md](./IMPLEMENTATION_SUMMARY.md) - Details on query parameter simplification
- [REFACTORING_SUMMARY.md](./REFACTORING_SUMMARY.md) - Complete refactoring documentation
- [SECURITY.md](./SECURITY.md) - Security policy and reporting
- [CONTRIBUTION.md](./CONTRIBUTION.md) - Contribution guidelines

## ๐ŸŒŸ Show Your Support

If you find this project helpful, please consider:
- โญ Starring the repository
- ๐Ÿด Forking and contributing
- ๐Ÿ“ข Sharing with others

---

Built with โค๏ธ using Express.js, following Clean Code principles and SOLID design patterns.