Skip to content

lazyfury/serv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

serv — 静态网站与路径代理服务 CLI

一个用 Go 编写的轻量级 HTTP 服务:

  • 从指定目录提供静态网站(支持 HTML/JS/CSS 等)
  • 在没有 index.html 时自动列出目录文件
  • 按指定的 URL 前缀将请求反向代理到目标 URL(前缀会被移除)

代码入口:main.go

功能特性

  • 静态文件托管:使用 http.FileServer 提供目录中的所有静态资源
  • 目录列表:当目录中没有 index.html 时显示文件列表,便于浏览
  • 路径前缀代理:--proxy-path 指定前缀,匹配到的请求会被转发到 --proxy-target-url
    • 代理时会移除前缀并保留剩余路径,例如:/api/users -> 目标的 /users
    • 将请求的 Host 头设置为目标主机,避免后端依赖 Host 失败
  • 简单访问日志:打印每个请求的 METHOD PATH

环境要求

  • Go 1.25 及以上

安装与编译

  • 直接运行(开发模式):
    • go run . -d . -p 8080
  • 编译为二进制:
    • go build -o out/serv .
    • Windows 可执行文件示例:out/serv.exe

使用方式

命令:serv(或 out/serv / out/serv.exe

支持的参数:

  • -d, --dir 静态文件根目录(默认 .
  • -p, --port 服务监听端口(默认 8080
  • -c, --config 配置文件路径(支持 json/yaml/yml/toml,可选)
  • -r, --route 路由规则 <path>=<target>,可重复提供多条(例如:-r /api=:9001
  • --proxy-path--proxy-target-url(用于单条路由,向后兼容,可选)

说明:配置文件是可选的,若未提供则仅根据命令行参数运行;当同时存在时会合并路由配置。

示例:仅静态网站

go run . -d ./public -p 8080

访问:http://localhost:8080/

示例:静态 + 单条路径前缀代理(向后兼容参数)

/api/* 代理到 https://httpbin.org/*,并移除 /api 前缀:

go run . -d . -p 8080 --proxy-path /api --proxy-target-url https://httpbin.org

请求示例:

  • http://localhost:8080/api/get -> 代理到 https://httpbin.org/get
  • http://localhost:8080/api/anything/foo -> 代理到 https://httpbin.org/anything/foo

Windows 二进制示例

编译后使用:

out/serv.exe -d . -p 8080 --proxy-path /api --proxy-target-url https://httpbin.org

示例:多路由(重复 --route

out/serv.exe -p 8080 -d . \
  -r /=:8080 \
  -r /api=:9001 \
  -r /admin=:9002

示例:使用配置文件(可选)

在当前目录中提供 serv.json|serv.yaml|serv.yml|serv.toml 之一,或通过 -c 指定路径:

out/serv.exe -c ./serv.yaml

配置合并顺序:配置文件 → 旧参数(--proxy-path/--proxy-target-url)→ 多个 --route

工作原理

  • 静态服务:http.FileServer(http.Dir(dir)) 负责在根路径 / 下提供目录内容;当目录无 index.html 时显示目录列表。
  • 反向代理:使用 httputil.NewSingleHostReverseProxy(target),并自定义 Director
    • 如果路径以 proxyPath 前缀开头,移除该前缀,并确保以 / 开头
    • 设置 req.Host = targetURL.Host,提高与目标服务兼容性
    • 同时对精确匹配与前缀匹配进行处理(例如 /api/api/
  • 多路由:注册多条前缀路由,Go ServeMux 采用最长匹配优先;同一路径只注册一次以避免冲突。
  • 根路径代理:当存在 path="/" 的路由时,静态目录不再挂载,服务充当反向代理网关。
  • 目标地址规范化:`
    • :PORT 开头的目标自动扩展为 http://localhost:PORT
    • 缺少协议的目标默认补全为 http://

注意事项

  • 安全:开发用途为主,未内置访问控制与限流;生产环境请置于反向代理(如 Nginx)或网关之后。
  • CORS:是否允许跨域由目标服务决定;如需自定义跨域策略,可在本服务上增加相应中间件。
  • 目录列表:若希望隐藏目录结构,请在目录中提供 index.html 并进行前端路由控制。

配置文件示例

JSON(serv.json):

{
  "dir": ".",
  "port": 8080,
  "routes": [
    { "path": "/", "target": ":8080" },
    { "path": "/api", "target": ":9001" },
    { "path": "/admin", "target": ":9002" }
  ]
}

YAML(serv.yaml):

dir: .
port: 8080
routes:
  - path: /
    target: :8080
  - path: /api
    target: :9001
  - path: /admin
    target: :9002

TOML(serv.toml):

dir = "."
port = 8080

[[routes]]
path = "/"
target = ":8080"

[[routes]]
path = "/api"
target = ":9001"

[[routes]]
path = "/admin"
target = ":9002"

开发

  • 主要代码文件:main.go
  • 依赖管理:go.mod / go.sum
  • 常用命令:
    • go run . -d . -p 8080
    • go build -o out/serv .

许可

未指定许可证。如需开源发布,请补充合适的 LICENSE 文件。

About

serv — 静态网站与路径代理服务 CLI

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages