Go to file
kingecg de1bbbfb7b chore: update module path to git.kingecg.top 2025-06-25 23:54:32 +08:00
.vscode add mime 2025-04-14 17:35:12 +08:00
admin chore: update module path to git.kingecg.top 2025-06-25 23:54:32 +08:00
adminui feat(admin): 添加服务器配置列表接口并优化前端展示 2025-06-15 22:15:45 +08:00
certs feat(ssl): 实现自定义证书管理器并更新服务器配置 2025-06-24 07:12:11 +08:00
example change and correct 2025-05-30 18:42:53 +08:00
handler chore: update module path to git.kingecg.top 2025-06-25 23:54:32 +08:00
healthcheck chore: update module path to git.kingecg.top 2025-06-25 22:53:18 +08:00
include "feat(ssl): 为ahttp服务启用SSL支持" 2025-06-24 19:06:00 +08:00
model chore: update module path to git.kingecg.top 2025-06-25 23:54:32 +08:00
server chore: update module path to git.kingecg.top 2025-06-25 23:54:32 +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 中移除冗余项 2025-06-23 23:46:48 +08:00
README.md chore: update module path to git.kingecg.top 2025-06-25 23:54:32 +08:00
config.json refactor(config): 重构配置处理逻辑 2025-06-23 23:12:03 +08:00
config.jsonbak change and correct 2025-05-30 18:42:53 +08:00
go.mod chore: update module path to git.kingecg.top 2025-06-25 23:54:32 +08:00
go.sum chore: update module path to git.kingecg.top 2025-06-25 23:54:32 +08:00
gohttp.go chore: update module path to git.kingecg.top 2025-06-25 23:54:32 +08:00
gohttpd.service add install script 2023-12-27 17:22:18 +08:00
install.sh fix(install): 修改安装脚本以创建必要的目录结构 2025-06-23 23:17:45 +08:00
main.go chore: update module path to git.kingecg.top 2025-06-25 23:54:32 +08:00
nginx.conf add config and add log message 2023-12-20 17:49:02 +08:00

README.md

GoHTTP Server

一个基于Go语言开发的高性能HTTP服务器支持多域名、SSL/TLS、静态文件服务等特性。类似Nginx功能的轻量级HTTP服务器实现提供静态文件服务、反向代理、路径重写、TLS支持等功能。

主要特性

  • 多服务器配置支持
  • SSL/TLS 支持
  • 静态文件服务
  • 反向代理Proxy
  • 路径重写Rewrite
  • 健康检查
  • 访问控制IP白名单/黑名单)
  • JWT认证
  • 访问统计
  • 管理接口
  • 配置热重载
  • 端口复用
  • 日志管理支持Console和File两种Appender
  • Gzip压缩支持

快速开始

系统要求

  • Go 1.23.0+

安装

git clone [repository-url]
cd gohttp
go mod tidy
make build

配置

  1. 主配置文件 (config.json)
  2. 域名配置文件 (include/*.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",
        "directives": [
            "Set-Header Access-Control-Allow-Origin *"
        ],
        "paths": [
            {
                "path": "/",
                "root": "./adminui",
                "default": "index.html"
            }
        ]
    },
    "servers": [{
        "port": 8080,
        "name": "test",
        "paths": [
            {
                "path": "/",
                "root": "/path/to/public/",
                "default": "index.html"
            },
            {
                "path": "/ws",
                "upstreams": ["http://localhost:3000"],
                "directives": [
                    "HostSchemas $target",
                    "HeaderOrigin",
                    "Path /ws /",
                    "RemoveCookie token"
                ],
                "health_check": {
                    "interval": "10s",
                    "timeout": "5s",
                    "retries": 3
                }
            }
        ]
    }]
}

配置说明

主配置文件

  • logging: 日志配置
    • appenders: 日志输出配置支持console和file
    • categories: 日志类别配置
  • admin: 管理后台配置
    • port: 管理接口端口
    • username: 管理员用户名
    • password: 管理员密码
    • directives: 管理接口指令
  • servers: 服务器配置列表

服务器配置

  • name: 服务名称
  • port: 监听端口
  • certfile: SSL证书文件路径
  • keyfile: SSL密钥文件路径
  • paths: 路径配置列表
  • directives: 全局指令

路径配置

  • path: URL路径
  • root: 本地文件系统路径
  • default: 默认文件
  • upstreams: 代理服务器地址列表
  • directives: 路径特定指令
  • health_check: 健康检查配置
    • interval: 检查间隔时间
    • timeout: 检查超时时间
    • retries: 失败重试次数

指令系统

指令系统采用类似Nginx的指令系统格式为

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

支持的指令

  • Set-Header: 设置响应头
  • HostSchemas: 设置代理目标地址
  • HeaderOrigin: 添加Origin头
  • Path: 重写URL路径
  • RemoveCookie: 删除指定Cookie

管理接口

  • /admin/reload: 重新加载配置
  • /admin/stats: 查看统计信息
  • /admin/health: 健康检查

构建和部署

构建

make clean && make build

构建后的可执行文件位于target目录。

运行

./target/<platform>/gohttpd

系统服务安装

安装脚本会执行以下操作:

  1. 检查是否已安装gohttpd
  2. 创建目录 /usr/local/gohttpd
  3. 复制所有必要文件到安装目录
  4. 创建软链接到 /usr/local/bin/gohttpd
  5. 安装并启用系统服务

安装命令:

sudo ./install.sh

安装完成后,可以通过以下命令管理服务:

# 启动服务
sudo systemctl start gohttpd.service

# 停止服务
sudo systemctl stop gohttpd.service

# 重启服务
sudo systemctl restart gohttpd.service

# 查看服务状态
sudo systemctl status gohttpd.service

# 查看服务日志
sudo journalctl -u gohttpd.service

默认安装路径:

  • 程序安装目录:/usr/local/gohttpd/
  • 可执行文件链接:/usr/local/bin/gohttpd
  • 服务配置文件:/etc/systemd/system/gohttpd.service

技术架构

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

依赖库

  • module git.kingecg.top/kingecg/cmux: 多路复用支持
  • git.kingecg.top/kingecg/godaemon: 守护进程支持
  • git.kingecg.top/kingecg/gologger: 自定义日志库
  • github.com/golang-jwt/jwt/v5: JWT支持
  • github.com/nanmu42/gzip: Gzip压缩支持
  • github.com/samber/lo: 工具库

许可证

本项目采用 MIT 许可证。查看 LICENSE 文件了解更多详细信息。

MIT License

Copyright (c) [year] [copyright holders]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

注意:请确保更新 LICENSE 文件中的 [year] 和 [copyright holders] 信息。