2023-12-06 20:58:08 +08:00
|
|
|
|
# gohttp
|
|
|
|
|
|
2023-12-07 00:22:53 +08:00
|
|
|
|
## 目标
|
|
|
|
|
|
|
|
|
|
实现一个类似nginx功能的http服务器
|
|
|
|
|
## 功能
|
|
|
|
|
|
|
|
|
|
- 支持静态文件
|
|
|
|
|
- Proxy
|
|
|
|
|
- 支持rewrite
|
2023-12-13 17:59:14 +08:00
|
|
|
|
- 支持TLS
|
|
|
|
|
- 支持端口复用
|
|
|
|
|
|
|
|
|
|
## 配置
|
|
|
|
|
```json
|
|
|
|
|
{
|
|
|
|
|
"logging" :{
|
|
|
|
|
"appenders": {
|
|
|
|
|
"out" :{
|
|
|
|
|
"type": "file",
|
|
|
|
|
"options":{
|
|
|
|
|
"file": "gohttpd.log"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"categories": {
|
|
|
|
|
"default": {
|
|
|
|
|
"appenders": [ "out" ],
|
|
|
|
|
"level": "debug"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"admin" : {
|
|
|
|
|
"name": "admin",
|
|
|
|
|
"port" : 8088,
|
|
|
|
|
"username": "admin",
|
|
|
|
|
"password": "admin",
|
2023-12-15 22:40:12 +08:00
|
|
|
|
"directives":[
|
|
|
|
|
"Set-Header Access-Control-Allow-Origin *"
|
|
|
|
|
],
|
2023-12-13 17:59:14 +08:00
|
|
|
|
"paths": [
|
|
|
|
|
{
|
|
|
|
|
"path": "/",
|
|
|
|
|
"root": "./adminui",
|
|
|
|
|
"default": "index.html"
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
"servers":[{
|
|
|
|
|
"port" : 8080,
|
|
|
|
|
"name":"test",
|
|
|
|
|
"paths":[
|
|
|
|
|
{
|
|
|
|
|
"path": "/",
|
|
|
|
|
"root": "/home/kingecg/code/gohttp/public/",
|
|
|
|
|
"default": "index.html"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"path": "/ws",
|
|
|
|
|
"upstreams":["http://localhost:3000"],
|
2023-12-15 22:40:12 +08:00
|
|
|
|
"directives":[
|
|
|
|
|
"HostSchemas $target",
|
|
|
|
|
"HeaderOrigin",
|
|
|
|
|
"Path /ws /",
|
|
|
|
|
"RemoveCookie token"
|
|
|
|
|
]
|
2023-12-13 17:59:14 +08:00
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}]
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
- logging 日志配置
|
|
|
|
|
- admin 管理后台配置
|
|
|
|
|
- servers 服务器配置
|
|
|
|
|
|
|
|
|
|
日志采用自己实现的类log4j库,目前只支持console 和file两种appeder
|
|
|
|
|
|
|
|
|
|
servers 配置
|
|
|
|
|
|
|
|
|
|
- port 端口
|
|
|
|
|
- name 服务器名称
|
|
|
|
|
- paths 路径配置
|
|
|
|
|
- certfile 证书文件
|
|
|
|
|
- keyfile 证书密钥文件
|
2023-12-15 22:40:12 +08:00
|
|
|
|
- directives 指令 针对response的指令,目前只实现了set-header
|
|
|
|
|
|
2023-12-13 17:59:14 +08:00
|
|
|
|
|
|
|
|
|
paths 配置
|
|
|
|
|
|
|
|
|
|
- path 路径
|
|
|
|
|
- root 根目录
|
|
|
|
|
- default 默认文件
|
|
|
|
|
- upstreams 代理地址
|
2023-12-15 22:40:12 +08:00
|
|
|
|
- directives 这里指令针对的是代理请求,有以下几种:
|
|
|
|
|
- HostSchemas [$target] 代理地址
|
|
|
|
|
- HeaderOrigin 代理请求时,添加Origin头
|
|
|
|
|
- Path [/ws] [/] 代理请求时,重写URL路径,用第二个参数替换url中的第一个部分
|
|
|
|
|
- RemoveCookie [token] 代理请求时,删除cookie中的某些字段
|
|
|
|
|
|
|
|
|
|
### 指令系统
|
2023-12-13 17:59:14 +08:00
|
|
|
|
|
2023-12-15 22:40:12 +08:00
|
|
|
|
指令系统采用了nginx的指令系统,指令的格式为:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
指令名 参数1 参数2 ... 参数n
|
|
|
|
|
```
|
|
|
|
|
指令系统用来对特定的request/response进行处理。目前只支持:
|
2023-12-13 17:59:14 +08:00
|
|
|
|
|
2023-12-15 22:40:12 +08:00
|
|
|
|
- 返回的response中设置header
|
|
|
|
|
- 反向代理时,修改发送到上游服务器的请求
|
2023-12-07 22:42:27 +08:00
|
|
|
|
|
|
|
|
|
## Packages
|
|
|
|
|
|
|
|
|
|
### Server
|
|
|
|
|
|
2023-12-13 17:59:14 +08:00
|
|
|
|
RestMux 提供
|
|
|
|
|
- Restful API注册功能的 ServerMux
|
|
|
|
|
- Route
|
|
|
|
|
- Url路径参数解析(形如:/user/:id)
|
|
|
|
|
- 中间件
|
|
|
|
|
- server管理
|
|
|
|
|
|
|
|
|
|
### model
|
|
|
|
|
提供模型定义
|
|
|
|
|
|
|
|
|
|
### admin
|
|
|
|
|
管理后台api
|
|
|
|
|
|
|
|
|
|
### handler
|
|
|
|
|
|
2023-12-17 10:33:29 +08:00
|
|
|
|
目录 handler
|
2023-12-13 17:59:14 +08:00
|
|
|
|
|
|
|
|
|
提供文件和代理两种handler
|
|
|
|
|
其中proxy handler 提供简单的负载均衡和会话粘滞功能
|
|
|
|
|
|
|
|
|
|
## 构建
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
make clean && make build
|
|
|
|
|
```
|
|
|
|
|
在target目录下生成可执行文件
|
|
|
|
|
|
|
|
|
|
## 运行
|
2023-12-07 22:42:27 +08:00
|
|
|
|
|
2023-12-13 17:59:14 +08:00
|
|
|
|
```bash
|
|
|
|
|
./gohttpd
|
|
|
|
|
```
|