# tcp_server_c **Repository Path**: zhaopei/tcp_server_c ## Basic Information - **Project Name**: tcp_server_c - **Description**: 原仓库地址 https://github.com/dexter-xD/tcp_server_c.git - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-10-13 - **Last Updated**: 2025-10-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Blink: Lightweight Web Server with Advanced Templating Blink is a lightweight, powerful web server written in C that features a comprehensive templating system with support for dynamic content, conditional logic, loops, and SQLite database integration. It's designed to be fast, easy to use, and perfect for both development and small-scale deployments. [![Buy Me A Coffee](https://img.shields.io/badge/Buy%20Me%20A%20Coffee-Support-yellow.svg)](https://buymeacoffee.com/trish07) ## Features - **Lightweight HTTP Server**: Fast and efficient C-based HTTP server with minimal dependencies - **Hot Reloading**: Automatic browser refresh when HTML files are modified - **WebSocket Support**: Real-time bidirectional communication - **Comprehensive Templating System**: - Variable replacement - Conditional logic (if/else blocks) - Loops with item iteration - Conditional loops with filtering - Nested template structures - **SQLite Integration**: - Execute SQL queries directly in templates - Display query results as formatted HTML tables - Form-based database operations (Create, Read, Update, Delete) - Placeholder substitution for safe user input handling - **Flexible Configuration**: - Customizable port settings - Custom HTML file serving - Database path configuration - Template processing toggles ## Project Structure ``` blink/ ├── CMakeLists.txt # Build configuration ├── LICENSE # Project license ├── README.md # Project documentation ├── .gitignore # Git ignore file │ ├── include/ # Header files │ ├── blink_orm.h # ORM functionality for SQLite │ ├── debug.h # Debugging utilities │ ├── file_watcher.h # File watching for hot reload │ ├── html_serve.h # HTML serving functionality │ ├── request_handler.h # HTTP request handler │ ├── server.h # Main server header │ ├── socket_utils.h # Socket utilities │ ├── sqlite_handler.h # SQLite database integration │ ├── template.h # Template processing │ └── websocket.h # WebSocket protocol support │ ├── src/ # Source code files │ ├── file_watcher.c # Implementation of file watcher │ ├── handle_client.c # Client connection handler │ ├── html_serve.c # HTML content serving │ ├── request_handler.c # HTTP request processing │ ├── server.c # Main server implementation │ ├── socket_utils.c # Socket utility functions │ ├── sqlite_handler.c # SQLite database functions │ ├── template.c # Template engine implementation │ └── websocket.c # WebSocket implementation │ └── build/ # Build directory (generated) └── bin/ # Compiled binaries └── blink # Main executable ``` ## Quick Start ### Prerequisites - **CMake** (version 3.10 or higher) - **GCC** or another compatible C compiler - **OpenSSL** development libraries - **SQLite3** development libraries - **Linux** or **WSL** (Windows Subsystem for Linux) recommended ### Installation ```bash # Install dependencies (Debian/Ubuntu) sudo apt update sudo apt install build-essential cmake libssl-dev libsqlite3-dev # Clone the repository git clone https://github.com/dexter-xD/blink.git cd blink # Build the project mkdir build && cd build cmake .. make # Run the server ./bin/blink ``` ### Command-Line Options ``` Options: -p, --port PORT Specify port number (default: 8080) -s, --serve FILE Specify a custom HTML file to serve -db, --database FILE Specify SQLite database path -n, --no-templates Disable template processing -h, --help Display help message ``` Example usage: ```bash # Run with a custom HTML file and SQLite database ./bin/blink --serve myapp.html --database mydata.db --port 9000 ``` ## Template Engine Guide The Blink template engine allows dynamic HTML generation with various powerful features. Here's an overview of the main capabilities: ### 1. Variable Replacement Define variables using HTML comments and reference them with double curly braces: ```html

Welcome, {{username}}!

You are logged into {{company}} systems.

``` ### 2. Conditional Logic Use if-else blocks to display content conditionally: ```html {% if is_admin %}

Admin Controls

{% else %}

You don't have admin privileges.

{% endif %} ``` ### 3. Loops Iterate over items using for loops: ```html ``` ### 4. Multi-part Items Use pipe-delimited values for structured data: ```html {% for item in items %} {% endfor %}
ProductCategoryPrice
{{item.0}} {{item.1}} ${{item.2}}
``` ### 5. Conditional Loops Filter items in loops using conditions: ```html

Fruits Only:

``` ### 6. SQLite Integration Execute SQL queries directly in your templates: ```html

User List

{% query "SELECT id, name, email FROM users ORDER BY name LIMIT 10" %}

Item Statistics

{% query "SELECT category, COUNT(*) as count, AVG(price) as avg_price FROM products GROUP BY category" %} ``` ### 7. Form-Based Database Operations Create forms that perform database operations: ```html
``` For detailed documentation on all template features, see the [Template Documentation](./docs/Template.md). ## Example Applications ### 1. Simple Dynamic Page ```html {{page_title}}

Hello, {{username}}!

Welcome to our website.

``` ### 2. Data Dashboard with SQLite ```html Sales Dashboard

Sales Dashboard

Recent Orders

{% query "SELECT id, customer_name, amount, date FROM orders ORDER BY date DESC LIMIT 5" %}

Sales by Category

{% query "SELECT category, SUM(amount) as total FROM orders GROUP BY category ORDER BY total DESC" %} ``` ## Hot Reload Feature Blink's hot reload feature automatically refreshes connected browsers when HTML files are modified: 1. Start the server with default options 2. Edit any HTML file in your project directory 3. The browser will automatically refresh to show your changes This feature works by: - Watching file system events in the HTML directory - Using WebSockets to notify connected clients - Injecting a small JavaScript snippet into served HTML pages ## WebSocket Support Blink includes WebSocket support for real-time bidirectional communication: 1. Access WebSocket functionality at `/ws` endpoint 2. Establish a WebSocket connection from your client-side JavaScript 3. Exchange messages between client and server in real time ## SQLite Database Integration To use SQLite features: 1. Start Blink with a database: `./bin/blink --database mydata.db` 2. Use `{% query "SQL_STATEMENT" %}` tags in your HTML templates 3. Create forms with action="/sql" to perform database operations ## Contributing Contributions are welcome! Please feel free to submit a Pull Request. ## Support Development If you find Blink useful, consider supporting its development: [![Buy Me A Coffee](https://img.shields.io/badge/Buy%20Me%20A%20Coffee-Support-yellow.svg)](https://www.buymeacoffee.com/yourusername) ## License This project is licensed under the MIT License - see the LICENSE file for details. --- ## Template Engine Documentation For full documentation on the template engine, see [docs/Template.md](./docs/Template.md).