gosh/README.md

185 lines
3.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Gosh
Gosh 是一个轻量级的 HTTP 服务器类库,允许用户通过注册 [Anko](https://github.com/mattn/anko) 脚本和 Go 函数来快速构建 RESTful API。
## 特性
- 简单易用的 HTTP 路由注册
- 支持 GET、POST、PUT、DELETE、PATCH 等 HTTP 方法
- 通过 Anko 脚本语言实现动态 API 处理
- 内置请求和响应辅助工具
- 支持文件系统、网络、进程和 Shell 命令操作
- 跨平台支持Linux、Windows
## 安装
```bash
go get git.kingecg.top/kingecg/gosh
```
## 快速开始
创建一个简单的 HTTP 服务器:
```go
package main
import (
"fmt"
"net/http"
"git.kingecg.top/kingecg/gosh"
)
func main() {
// 创建服务器实例
serverMux := gosh.NewSHMux()
// 注册 Go 函数,可在脚本中调用
serverMux.RegistFunction("hello", func(name string) string {
return fmt.Sprintf("Hello, %s", name)
})
// 注册 GET 路由,使用 Anko 脚本处理请求
serverMux.GET("/hello", `
name = Req.GetQuery("name")
Res.WriteStr(hello(name))
`)
// 启动服务器
http.ListenAndServe(":8089", serverMux)
}
```
## API 参考
### 服务器创建
```go
// 创建新的服务器实例
serverMux := gosh.NewSHMux()
```
### 路由注册
```go
// 注册 HTTP 方法路由
serverMux.GET(pattern, script)
serverMux.POST(pattern, script)
serverMux.PUT(pattern, script)
serverMux.DELETE(pattern, script)
serverMux.PATCH(pattern, script)
```
### 函数注册
```go
// 注册 Go 函数,可在脚本中调用
serverMux.RegistFunction(name, function)
```
### 请求处理
在 Anko 脚本中,可以使用以下对象和方法:
- `Req` - 请求对象
- `Req.GetQuery(key)` - 获取查询参数
- `Req.GetBodyStr()` - 获取请求体字符串
- `Req.GetHeader(key)` - 获取请求头
- `Req.GetBody()` - 获取解析后的请求体JSON 或表单)
- `Res` - 响应对象
- `Res.SetHeader(key, value)` - 设置响应头
- `Res.WriteStr(str)` - 写入字符串响应
- `Res.WriteJSON(value)` - 写入 JSON 响应
- `Res.Status(code)` - 设置状态码
- `Res.StatusOk()` - 设置 200 状态码
- `Res.StatusBadRequest()` - 设置 400 状态码
- `Res.StatusNotFound()` - 设置 404 状态码
- `Res.StatusInternalServerError()` - 设置 500 状态码
- `Res.StatusForbidden()` - 设置 403 状态码
### 内置模块
在 Anko 脚本中,可以使用 `Require` 函数导入以下模块:
- `fs` - 文件系统模块
- 提供文件读写操作
- `net` - 网络模块
- 提供网络相关操作,如端口检测
- `process` - 进程模块
- 提供进程信息和环境变量访问
- 获取和设置环境变量
- 获取进程 ID 和主机名等信息
- `shell` - Shell 命令模块
- 提供执行 Shell 命令的功能
- 支持 Linux bash、Windows cmd 和 PowerShell
- 命令输出解析功能
## 示例
### 处理 JSON 请求
```go
serverMux.POST("/api/user", `
body = Req.GetBody()
name = body["name"]
age = body["age"]
response = {
"message": "User created",
"user": {
"name": name,
"age": age
}
}
Res.WriteJSON(response)
`)
```
### 使用文件系统模块
```go
serverMux.GET("/api/file", `
fs = Require("fs")
content = fs.ReadFile("/path/to/file.txt", "utf8")
Res.WriteStr(content)
`)
```
### 使用进程模块
```go
serverMux.GET("/api/env", `
process = Require("process")
env = process.GetAllEnv()
Res.WriteJSON(env)
`)
```
### 使用 Shell 模块
```go
serverMux.GET("/api/ls", `
shell = Require("shell")
result, err = shell.Exec("ls -la")
if err == nil {
Res.WriteStr(result.Stdout)
} else {
Res.StatusInternalServerError()
Res.WriteStr("Error executing command")
}
`)
```
## 贡献
欢迎提交问题和拉取请求!
## 许可证
本项目采用 MIT 许可证 - 详见 [LICENSE](LICENSE) 文件。