golang静态HTTP服务器快速部署插件serve的使用
Golang静态HTTP服务器快速部署插件serve的使用
serve
是一个简单的静态HTTP服务器,可以在任何你需要的地方快速部署。
功能特性
- HTTPS (TLS)支持
- CORS支持
- 请求日志记录
- 兼容
net/http
- 通过
users.json
支持基本认证(BasicAuth)
安装方法
macOS上使用Homebrew安装
brew install syntaqx/tap/serve
使用Docker
官方镜像syntaqx/serve
可在Docker Hub获取。
启动一个容器并挂载当前目录:
docker run -v .:/var/www:ro -d syntaqx/serve
或者创建一个自定义Docker镜像:
FROM syntaqx/serve
COPY . /var/www
构建并运行:
docker build -t some-content-serve .
docker run --name some-serve -d some-content-serve
暴露外部端口:
docker run --name some-serve -d -p 8080:8080 some-content-serve
下载二进制文件
快速下载安装最新版本:
curl -sfL https://install.goreleaser.com/github.com/syntaqx/serve.sh | sh
或者手动从发布页面下载适合你系统的二进制文件。
从源码安装
git clone git@github.com:syntaqx/serve.git && cd $(basename $_ .git)
make install
使用方法
基本命令格式:
serve [options] [path]
[path]
默认为当前目录(.)
然后只需在浏览器中打开http://localhost:8080
即可查看你的服务器。
配置选项
--host
绑定主机地址(默认为0.0.0.0
)--port
监听端口(默认为8080
)--ssl
启用HTTPS(默认为false
)--cert
SSL证书文件路径(默认为cert.pem
)--key
SSL密钥文件路径(默认为key.pem
)--dir
要服务的目录路径(默认为.
,也可以通过arg[0]
配置)--users
用户文件路径(默认为users.dat
);文件应包含明文格式的username:password行
在代码中使用
除了使用二进制运行serve
,你还可以在自己的Go程序中嵌入serve.FileServer
:
package main
import (
"log"
"net/http"
"github.com/syntaqx/serve"
)
func main() {
fs := serve.NewFileServer()
log.Fatal(http.ListenAndServe(":8080", fs))
}
完整示例
基本使用
# 在当前目录启动服务器
serve
# 指定目录启动服务器
serve /path/to/your/files
# 指定端口启动
serve --port 3000
启用HTTPS
serve --ssl --cert /path/to/cert.pem --key /path/to/key.pem
使用Docker Compose
version: '3'
services:
web:
image: syntaqx/serve
volumes:
- ./static:/var/www
- ./fixtures:/etc/ssl
environment:
- PORT=1234
ports:
- 1234
command: serve -ssl -cert=/etc/ssl/cert.pem -key=/etc/ssl/key.pem -dir=/var/www
许可证
serve
是根据MIT许可证发布的开源软件。
更多关于golang静态HTTP服务器快速部署插件serve的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
更多关于golang静态HTTP服务器快速部署插件serve的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
Golang 静态HTTP服务器快速部署插件serve的使用
在Golang中,我们可以使用serve
库快速搭建一个静态HTTP服务器,用于本地开发测试或简单的文件共享。下面我将详细介绍如何使用这个工具。
安装serve
首先,我们需要安装serve
库:
go get github.com/syntaqx/serve
基本使用方法
1. 最简单的方式
在项目目录下直接运行:
serve
这会启动一个服务器,默认监听0.0.0.0:3000
,并将当前目录作为根目录。
2. 自定义端口和目录
serve -p 8080 -d ./public
-p
指定端口号-d
指定要服务的目录
3. 使用Golang代码启动
你也可以直接在Go代码中使用这个库:
package main
import (
"log"
"github.com/syntaqx/serve"
)
func main() {
// 创建服务器配置
config := serve.Config{
Port: 8080,
Dir: "./public",
Verbose: true,
}
// 创建服务器实例
server := serve.NewServer(config)
// 启动服务器
log.Printf("Server starting on :%d", config.Port)
if err := server.ListenAndServe(); err != nil {
log.Fatalf("Server failed: %v", err)
}
}
高级功能
1. 启用HTTPS
config := serve.Config{
Port: 443,
Dir: "./public",
CertFile: "cert.pem",
KeyFile: "key.pem",
}
2. 自定义路由
config := serve.Config{
Port: 8080,
Dir: "./public",
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/custom" {
w.Write([]byte("Custom route"))
return
}
http.FileServer(http.Dir(config.Dir)).ServeHTTP(w, r)
}),
}
3. 启用CORS
config := serve.Config{
Port: 8080,
Dir: "./public",
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
if r.Method == "OPTIONS" {
return
}
http.FileServer(http.Dir(config.Dir)).ServeHTTP(w, r)
}),
}
命令行参数完整列表
参数 | 简写 | 描述 | 默认值 |
---|---|---|---|
–port | -p | 监听端口 | 3000 |
–dir | -d | 要服务的目录 | 当前目录 |
–host | -h | 监听主机 | 0.0.0.0 |
–cert | -c | TLS证书文件 | “” |
–key | -k | TLS私钥文件 | “” |
–verbose | -v | 详细日志输出 | false |
–no-log | -q | 禁用日志输出 | false |
–help | -help | 显示帮助信息 | - |
实际应用示例
1. 开发环境快速启动
# 在项目目录下
serve -p 3000 -d ./dist
2. 作为本地API模拟服务器
创建一个api
目录,里面放上JSON文件,然后:
serve -p 3001 -d ./api
3. 在Go项目中集成
package main
import (
"log"
"net/http"
"os"
"github.com/syntaqx/serve"
)
func main() {
// 检查环境变量
port := "3000"
if p := os.Getenv("PORT"); p != "" {
port = p
}
// 配置服务器
config := serve.Config{
Port: port,
Dir: "./public",
Verbose: true,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// 添加自定义中间件
log.Printf("%s %s", r.Method, r.URL.Path)
http.FileServer(http.Dir(config.Dir)).ServeHTTP(w, r)
}),
}
// 启动服务器
server := serve.NewServer(config)
log.Printf("Starting server on :%s", port)
if err := server.ListenAndServe(); err != nil {
log.Fatalf("Server error: %v", err)
}
}
总结
serve
是一个轻量级的Golang静态文件服务器工具,非常适合:
- 本地开发测试
- 快速共享文件
- 构建简单的Web服务原型
- 作为微服务架构中的静态资源服务器
它的优势在于简单易用,既可以作为命令行工具快速启动,也可以集成到Go项目中作为库使用。