Go to file
kingecg 8fff0e8888 use interceptor in SHMux 2025-09-21 19:03:29 +08:00
doc 添加Anko脚本语法指南及完善拦截器文档 2025-09-19 21:35:00 +08:00
example 添加动态库示例代码及CLI测试程序 2025-09-19 21:06:03 +08:00
interceptor use interceptor in SHMux 2025-09-21 19:03:29 +08:00
.gitignore 添加动态库加载功能及测试用例 2025-09-19 18:57:22 +08:00
LICENSE add code 2025-09-17 08:46:39 +08:00
README.md add code 2025-09-17 08:46:39 +08:00
go.mod 实现模块注册功能及脚本执行优化 2025-09-20 16:35:33 +08:00
go.sum 添加动态库加载功能及测试用例 2025-09-19 18:57:22 +08:00
gosh.go use interceptor in SHMux 2025-09-21 19:03:29 +08:00
helper.go add code 2025-09-17 08:46:39 +08:00
require.md add code 2025-09-17 08:46:39 +08:00

README.md

Gosh

Gosh 是一个轻量级的 HTTP 服务器类库,允许用户通过注册 Anko 脚本和 Go 函数来快速构建 RESTful API。

特性

  • 简单易用的 HTTP 路由注册
  • 支持 GET、POST、PUT、DELETE、PATCH 等 HTTP 方法
  • 通过 Anko 脚本语言实现动态 API 处理
  • 内置请求和响应辅助工具
  • 支持文件系统、网络、进程和 Shell 命令操作
  • 跨平台支持Linux、Windows

安装

go get git.kingecg.top/kingecg/gosh

快速开始

创建一个简单的 HTTP 服务器:

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 参考

服务器创建

// 创建新的服务器实例
serverMux := gosh.NewSHMux()

路由注册

// 注册 HTTP 方法路由
serverMux.GET(pattern, script)
serverMux.POST(pattern, script)
serverMux.PUT(pattern, script)
serverMux.DELETE(pattern, script)
serverMux.PATCH(pattern, script)

函数注册

// 注册 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 请求

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)
`)

使用文件系统模块

serverMux.GET("/api/file", `
    fs = Require("fs")
    content = fs.ReadFile("/path/to/file.txt", "utf8")
    Res.WriteStr(content)
`)

使用进程模块

serverMux.GET("/api/env", `
    process = Require("process")
    env = process.GetAllEnv()
    Res.WriteJSON(env)
`)

使用 Shell 模块

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 文件。