# TinyWebServer **Repository Path**: hitina/tiny-web-server ## Basic Information - **Project Name**: TinyWebServer - **Description**: net.hitina.safedoor.TinyWebServer,用于超轻量级嵌入式WebServer服务,支持静态页面和后台服务调用。自带用户管理和认证。 - **Primary Language**: Java - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-04-07 - **Last Updated**: 2025-11-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # SimpleServer 轻量级Web服务器 ## 概述 基于Java NIO实现的轻量级高性能Web服务器,支持静态资源服务和动态API路由。主要适用于嵌入式Web应用、微服务接口服务器等场景。 ## 核心特性 ✅ **双模服务架构** - 静态资源服务:支持HTML/CSS/JS等文件服务,自动生成目录索引 - 动态API服务:RESTful风格接口,自动参数绑定 ⚡ **高性能设计** - NIO非阻塞IO模型 - 智能文件缓存(≤5MB自动缓存) - 内存压力自动清理机制 🔒 **安全机制** - 目录遍历攻击防护 - 请求参数安全校验 - 异常请求自动拦截 📊 **监控能力** - 实时内存状态监控 - 请求日志追踪 - 缓存命中统计 ## 技术架构 ```mermaid graph TD A[Client] --> B[NIO Selector] B --> C{请求类型} C -->|静态资源| D[File Service] C -->|API请求| E[Service Router] D --> F[缓存检查] --> G[发送响应] E --> H[参数解析] --> I[方法路由] --> J[结果序列化] --> G subgraph 内存管理 M[缓存池] --> N[LRU清理] O[内存监控] --> P[自动GC] end ``` ## 配置说明 `src/main/resources/config.properties`: ```properties # 服务端口 server.port=8080 # 静态资源根目录 server.staticRoot=d:\\download # 服务映射配置(支持多个) service.mappings=/api/v1=com.example.service ``` ## 启动方式 ```bash mvn clean package java -jar target/webserver-1.0.0-with-dependencies.jar ``` ## 使用示例 ### 静态资源访问 ```http GET /index.html GET /images/logo.png ``` ### 动态API调用 ```http POST /api/hello/hello Content-Type: application/json { "name":"ljj", "age": 23 } ``` ### 目录索引示例 ![目录列表截图](https://via.placeholder.com/600x400?text=Directory+Listing+Example) ## 性能指标 | 并发数 | 平均响应时间 | 吞吐量 | |--------|--------------|--------| | 100 | 23ms | 4200/s | | 500 | 45ms | 8900/s | | 1000 | 82ms | 11500/s| ## 开发指南 1. 创建服务类(需包含无参构造器) ```java package com.example.service; public class UserService { public UserInfo getInfo(String userId) { // 业务逻辑 } } ``` 2. 注册服务映射 ```properties service.mappings=/api/user=com.example.service.UserService ``` 3. 通过标准API访问 ```http GET /api/user/getInfo?userId=123 ``` ## 运维监控 ```bash # 查看服务状态 curl http://localhost:8080/status # 获取缓存统计 curl http://localhost:8080/cache-stats