chore: 初始化项目骨架(README + Makefile + .gitignore)

This commit is contained in:
xfy911 2026-06-03 16:42:10 +08:00
commit 84b415470b
3 changed files with 137 additions and 0 deletions

12
.gitignore vendored Normal file
View File

@ -0,0 +1,12 @@
# Cocoon
/build/
*.o
*.a
*.so
cocoon
.DS_Store
*.swp
*.swo
*~
.vscode/
.idea/

49
Makefile Normal file
View File

@ -0,0 +1,49 @@
# Cocoon - 基于 coco 协程库的静态资源 Web 服务器
CC = gcc
CFLAGS = -Wall -Wextra -O2 -std=c11 -I../coco/include
LDFLAGS = -L../coco/build -lcoco -lpthread -lm
# 调试模式
DEBUG ?= 0
ifeq ($(DEBUG),1)
CFLAGS += -g -O0 -DCOCOON_DEBUG
endif
# 安装路径
PREFIX ?= /usr/local
BINDIR = $(PREFIX)/bin
# 源文件
SRCS = main.c server.c http.c static.c
OBJS = $(SRCS:.c=.o)
TARGET = cocoon
# 默认目标
all: $(TARGET)
# 链接
$(TARGET): $(OBJS)
$(CC) $(OBJS) -o $@ $(LDFLAGS)
# 编译规则
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
# 安装
install: $(TARGET)
install -d $(BINDIR)
install -m 755 $(TARGET) $(BINDIR)/
# 卸载
uninstall:
rm -f $(BINDIR)/$(TARGET)
# 清理
clean:
rm -f $(OBJS) $(TARGET)
# 重新构建
rebuild: clean all
.PHONY: all clean install uninstall rebuild

76
README.md Normal file
View File

@ -0,0 +1,76 @@
# Cocoon
一个基于 [coco](https://github.com/DefectingCat/coco) 协程库的轻量级静态资源 Web 服务器。
**Cocoon**(茧)—— 用协程的轻盈,包裹静态资源的安稳。
## 特性
- 🚀 **协程驱动并发** — 基于 coco 的有栈协程,每个连接一个协程,告别回调地狱
- 📁 **静态文件托管** — 支持目录列表、MIME 类型自动识别、范围请求Range
- 🧵 **多线程调度** — 利用 coco 的 M:N 调度器,自动扩展至多核
- ⚡ **零拷贝发送** — 支持 sendfile减少用户态/内核态拷贝
- 🔧 **极简配置** — 命令行参数即可启动,无需配置文件
## 快速开始
### 构建
```bash
# 克隆仓库
git clone https://github.com/xfy911/cocoon.git
cd cocoon
# 构建(需要 coco 库和头文件)
make
# 运行
./cocoon -r ./examples/www -p 8080
```
### 依赖
- [coco](https://github.com/DefectingCat/coco) — 协程库
- Linux / macOS支持 epoll / kqueue
## 使用
```bash
# 基本用法
./cocoon -r /var/www/html -p 8080
# 多线程模式(自动检测 CPU 核心数)
./cocoon -r ./examples/www -p 8080 -t
# 指定工作线程数
./cocoon -r ./examples/www -p 8080 -w 8
```
## 架构
```
┌─────────────┐ ┌─────────────────┐ ┌──────────────┐
│ main.c │────▶│ server.c │────▶│ static.c │
│ (入口) │ │ (TCP 服务器) │ │ (静态文件) │
└─────────────┘ └─────────────────┘ └──────────────┘
┌─────────────────┐
│ http.c │
│ (HTTP 解析) │
└─────────────────┘
┌─────────────────┐
│ coco 协程库 │
│ (并发 + I/O) │
└─────────────────┘
```
## 性能
基于 coco 协程的高性能上下文切换(< 100nsCocoon 能够轻松处理数万并发连接
## 许可证
MIT