Go to file
程广 56210416a6 docs(README): 更新项目文档
- 新增项目概述、核心功能、系统架构等章节
- 补充技术选型、安装指南等内容
- 优化现有章节的结构和表述
2025-06-13 17:56:27 +08:00
.vscode add mime 2025-04-14 17:35:12 +08:00
admin change and correct 2025-05-30 18:42:53 +08:00
adminui fix normalize bug 2023-12-13 10:59:47 +08:00
example change and correct 2025-05-30 18:42:53 +08:00
handler change and correct 2025-05-30 18:42:53 +08:00
healthcheck 实现健康检查功能并优化代码结构 2025-05-29 16:53:30 +08:00
model feat(server): 添加自动 SSL 证书管理功能 2025-06-13 17:49:04 +08:00
server feat(server): 添加自动 SSL 证书管理功能 2025-06-13 17:49:04 +08:00
utils feat(server): 添加自动 SSL 证书管理功能 2025-06-13 17:49:04 +08:00
.gitignore chore: 添加日志文件到 .gitignore 2025-06-13 15:10:05 +08:00
.hintrc change and correct 2025-05-30 18:42:53 +08:00
.nvmrc fix 2025-02-18 13:46:59 +08:00
LICENSE Initial commit 2023-12-06 20:58:08 +08:00
Makefile build(Makefile): 更新构建架构并调整 go sum 文件 2025-06-13 15:04:41 +08:00
README.md docs(README): 更新项目文档 2025-06-13 17:56:27 +08:00
config.json change and correct 2025-05-30 18:42:53 +08:00
config.jsonbak change and correct 2025-05-30 18:42:53 +08:00
go.mod feat(server): 添加自动 SSL 证书管理功能 2025-06-13 17:49:04 +08:00
go.sum feat(server): 添加自动 SSL 证书管理功能 2025-06-13 17:49:04 +08:00
gohttp.go feat(server): 添加自动 SSL 证书管理功能 2025-06-13 17:49:04 +08:00
gohttpd.service add install script 2023-12-27 17:22:18 +08:00
install.sh add install script 2023-12-27 17:22:18 +08:00
main.go refactor and add static 2025-02-18 00:54:50 +08:00
nginx.conf add config and add log message 2023-12-20 17:49:02 +08:00

README.md

gohttp

项目概述

项目背景和价值

  • 背景: 实现一个类似Nginx功能的HTTP服务器提供静态文件服务、反向代理、路径重写、TLS支持等功能。
  • 价值: 提供轻量级、高性能的HTTP服务解决方案适用于中小型应用或特定场景下的负载均衡和反向代理需求。

目标

实现一个类似nginx功能的http服务器

核心功能

  • 支持静态文件服务
  • 反向代理Proxy
  • 路径重写Rewrite
  • TLS加密支持
  • 端口复用
  • 健康检查
  • 日志管理支持Console和File两种Appender

配置

{
   "logging" :{
        "appenders": {
            "out" :{
                "type": "file",
                "options":{
                    "file": "gohttpd.log"
                }
            }
        },
        "categories": {
            "default": {
                "appenders": [ "out" ],
                "level": "debug"
            }
        }
    },
    "admin" : {
        "name": "admin",
        "port" : 8088,
        "username": "admin",
        "password": "admin",
        "directives":[
            "Set-Header Access-Control-Allow-Origin *"
        ],
        "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"],
                "directives":[
                    "HostSchemas $target",
                    "HeaderOrigin",
                    "Path /ws /",
                    "RemoveCookie token"
                ]
            }
        ]
    }] 
}
  • logging 日志配置
  • admin 管理后台配置
  • servers 服务器配置

日志采用自己实现的类log4j库目前只支持console 和file两种appeder

servers 配置

  • port 端口
  • name 服务器名称
  • paths 路径配置
  • certfile 证书文件
  • keyfile 证书密钥文件
  • directives 指令 针对response的指令目前只实现了set-header

paths 配置

  • path 路径
  • root 根目录
  • default 默认文件
  • upstreams 代理地址
  • directives 这里指令针对的是代理请求,有以下几种:
    • HostSchemas [$target] 代理地址
    • HeaderOrigin 代理请求时添加Origin头
    • Path [/ws] [/] 代理请求时重写URL路径用第二个参数替换url中的第一个部分
    • RemoveCookie [token] 代理请求时删除cookie中的某些字段

健康检查配置(新)

健康检查功能允许系统定期检查上游服务器的健康状态,并在请求时自动跳过不健康的服务器。

配置选项

  • health_check - 在server配置中的健康检查参数
    • interval - 检查间隔时间(如"10s"
    • timeout - 单次检查超时时间(如"5s"
    • retries - 健康检查失败重试次数

示例配置

{
    "servers":[{
        "port" : 8080,
        "name":"test",
        
        "paths":[{
            "path": "/",
            "root": "/home/kingecg/code/gohttp/public/",
            "default": "index.html"
        },{
            "path": "/ws",
            "upstreams":["http://localhost:3000"],
            "directives":[
                "HostSchemas $target",
                "HeaderOrigin",
                "Path /ws /",
                "RemoveCookie token"
            ],
            "health_check": {
                "interval": "10s",  // 每10秒检查一次
                "timeout": "5s",   // 每次检查5秒超时
                "retries": 3         // 失败3次才认为是不健康
            },
        }]
    }]
}

特性

  • 自动检测上游服务器的健康状态
  • 请求失败时自动重试其他可用服务器
  • 定期后台检查服务器状态
  • 详细的日志记录便于监控和调试

指令系统

指令系统采用了nginx的指令系统指令的格式为

指令名 参数1 参数2 ... 参数n

指令系统用来对特定的request/response进行处理。目前只支持

  • 返回的response中设置header
  • 反向代理时,修改发送到上游服务器的请求

命名规则: 模块支持的指令以模块名作为前缀Proxy_Path 是反向代理模块的Path指令。 没有前缀的是全局指令Set-Header 是设置header的指令。

系统架构

整体架构

  • 架构模式: 单体架构,模块化设计
  • 关键组件:
    • Server: Restful API注册、路由解析、中间件管理
    • Handler: 文件和代理处理器
    • HealthCheck: 上游服务器健康检测
    • Admin: 管理后台API

技术选型

  • 编程语言: Go (v1.23.0+)
  • 依赖库:
    • git.pyer.club/kingecg/cmux: 多路复用支持
    • git.pyer.club/kingecg/godaemon: 守护进程支持
    • git.pyer.club/kingecg/gologger: 自定义日志库
    • github.com/golang-jwt/jwt/v5: JWT支持
    • github.com/nanmu42/gzip: Gzip压缩支持
    • github.com/samber/lo: 工具库

安装指南

开发环境搭建

  1. 安装Go语言环境版本>=1.23.0
  2. 克隆项目代码到本地:
    git clone <repository-url>
    cd /persistent/home/kingecg/work/code/gohttp
    
  3. 安装依赖:
    go mod tidy
    

构建与运行

  • 构建命令:
    make clean && make build
    
  • 运行命令:
    ./target/<platform>/gohttpd
    
  • 部署: 使用install.sh脚本完成安装

构建

make clean && make build

在target目录下生成可执行文件

运行

./gohttpd