feat: 添加环境变量管理工具和部署配置改版
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 5m33s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 5m33s
主要改动: - 新增交互式环境配置脚本 (scripts/setup-env.sh) - 新增本地启动快捷脚本 (scripts/run-local.sh) - 新增环境变量模板文件 (.env.example) - 部署模式改版:使用嵌入式配置 + 环境变量覆盖 - 添加对象存储功能支持 - 改进 IoT 卡片导入任务 - 优化 OpenAPI 文档生成 - 删除旧的配置文件,改用嵌入式默认配置
This commit is contained in:
@@ -4,6 +4,9 @@ import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/swaggest/openapi-go/openapi3"
|
||||
"gopkg.in/yaml.v3"
|
||||
@@ -85,26 +88,37 @@ func (g *Generator) addErrorResponseSchema() {
|
||||
g.Reflector.Spec.ComponentsEns().SchemasEns().WithMapOfSchemaOrRefValuesItem("ErrorResponse", errorSchema)
|
||||
}
|
||||
|
||||
// ptrString 返回字符串指针
|
||||
func ptrString(s string) *string {
|
||||
return &s
|
||||
}
|
||||
|
||||
// FileUploadField 定义文件上传字段
|
||||
type FileUploadField struct {
|
||||
Name string
|
||||
Description string
|
||||
Required bool
|
||||
}
|
||||
|
||||
// AddOperation 向 OpenAPI 规范中添加一个操作
|
||||
// 参数:
|
||||
// - method: HTTP 方法(GET, POST, PUT, DELETE 等)
|
||||
// - path: API 路径
|
||||
// - summary: 操作摘要
|
||||
// - description: 详细说明,支持 Markdown 语法(可为空)
|
||||
// - input: 请求参数结构体(可为 nil)
|
||||
// - output: 响应结构体(可为 nil)
|
||||
// - tags: 标签列表
|
||||
// - requiresAuth: 是否需要认证
|
||||
func (g *Generator) AddOperation(method, path, summary string, input interface{}, output interface{}, requiresAuth bool, tags ...string) {
|
||||
func (g *Generator) AddOperation(method, path, summary, description string, input interface{}, output interface{}, requiresAuth bool, tags ...string) {
|
||||
op := openapi3.Operation{
|
||||
Summary: &summary,
|
||||
Tags: tags,
|
||||
}
|
||||
|
||||
if description != "" {
|
||||
op.Description = &description
|
||||
}
|
||||
|
||||
// 反射输入 (请求参数/Body)
|
||||
if input != nil {
|
||||
// SetRequest 根据结构体标签自动检测 Body、Query 或 Path 参数
|
||||
@@ -134,6 +148,166 @@ func (g *Generator) AddOperation(method, path, summary string, input interface{}
|
||||
}
|
||||
}
|
||||
|
||||
// AddMultipartOperation 添加支持文件上传的 multipart/form-data 操作
|
||||
func (g *Generator) AddMultipartOperation(method, path, summary, description string, input interface{}, output interface{}, requiresAuth bool, fileFields []FileUploadField, tags ...string) {
|
||||
op := openapi3.Operation{
|
||||
Summary: &summary,
|
||||
Tags: tags,
|
||||
}
|
||||
|
||||
if description != "" {
|
||||
op.Description = &description
|
||||
}
|
||||
|
||||
objectType := openapi3.SchemaType("object")
|
||||
stringType := openapi3.SchemaType("string")
|
||||
integerType := openapi3.SchemaType("integer")
|
||||
binaryFormat := "binary"
|
||||
|
||||
properties := make(map[string]openapi3.SchemaOrRef)
|
||||
var requiredFields []string
|
||||
|
||||
for _, f := range fileFields {
|
||||
properties[f.Name] = openapi3.SchemaOrRef{
|
||||
Schema: &openapi3.Schema{
|
||||
Type: &stringType,
|
||||
Format: &binaryFormat,
|
||||
Description: ptrString(f.Description),
|
||||
},
|
||||
}
|
||||
if f.Required {
|
||||
requiredFields = append(requiredFields, f.Name)
|
||||
}
|
||||
}
|
||||
|
||||
if input != nil {
|
||||
formFields := parseFormFields(input)
|
||||
for _, field := range formFields {
|
||||
var schemaType *openapi3.SchemaType
|
||||
switch field.Type {
|
||||
case "integer":
|
||||
schemaType = &integerType
|
||||
default:
|
||||
schemaType = &stringType
|
||||
}
|
||||
schema := &openapi3.Schema{
|
||||
Type: schemaType,
|
||||
Description: ptrString(field.Description),
|
||||
}
|
||||
if field.Min != nil {
|
||||
schema.Minimum = field.Min
|
||||
}
|
||||
if field.MaxLength != nil {
|
||||
schema.MaxLength = field.MaxLength
|
||||
}
|
||||
properties[field.Name] = openapi3.SchemaOrRef{Schema: schema}
|
||||
if field.Required {
|
||||
requiredFields = append(requiredFields, field.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
op.RequestBody = &openapi3.RequestBodyOrRef{
|
||||
RequestBody: &openapi3.RequestBody{
|
||||
Required: ptrBool(true),
|
||||
Content: map[string]openapi3.MediaType{
|
||||
"multipart/form-data": {
|
||||
Schema: &openapi3.SchemaOrRef{
|
||||
Schema: &openapi3.Schema{
|
||||
Type: &objectType,
|
||||
Properties: properties,
|
||||
Required: requiredFields,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if output != nil {
|
||||
if err := g.Reflector.SetJSONResponse(&op, output, 200); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
if requiresAuth {
|
||||
g.addSecurityRequirement(&op)
|
||||
}
|
||||
|
||||
g.addStandardErrorResponses(&op, requiresAuth)
|
||||
|
||||
if err := g.Reflector.Spec.AddOperation(method, path, op); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func ptrBool(b bool) *bool {
|
||||
return &b
|
||||
}
|
||||
|
||||
type formFieldInfo struct {
|
||||
Name string
|
||||
Type string
|
||||
Description string
|
||||
Required bool
|
||||
Min *float64
|
||||
MaxLength *int64
|
||||
}
|
||||
|
||||
func parseFormFields(input interface{}) []formFieldInfo {
|
||||
var fields []formFieldInfo
|
||||
t := reflect.TypeOf(input)
|
||||
if t.Kind() == reflect.Ptr {
|
||||
t = t.Elem()
|
||||
}
|
||||
if t.Kind() != reflect.Struct {
|
||||
return fields
|
||||
}
|
||||
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
field := t.Field(i)
|
||||
|
||||
formTag := field.Tag.Get("form")
|
||||
if formTag == "" || formTag == "-" {
|
||||
continue
|
||||
}
|
||||
|
||||
info := formFieldInfo{
|
||||
Name: formTag,
|
||||
Description: field.Tag.Get("description"),
|
||||
}
|
||||
|
||||
switch field.Type.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
info.Type = "integer"
|
||||
default:
|
||||
info.Type = "string"
|
||||
}
|
||||
|
||||
validateTag := field.Tag.Get("validate")
|
||||
if strings.Contains(validateTag, "required") {
|
||||
info.Required = true
|
||||
}
|
||||
|
||||
if minStr := field.Tag.Get("minimum"); minStr != "" {
|
||||
if min, err := strconv.ParseFloat(minStr, 64); err == nil {
|
||||
info.Min = &min
|
||||
}
|
||||
}
|
||||
|
||||
if maxLenStr := field.Tag.Get("maxLength"); maxLenStr != "" {
|
||||
if maxLen, err := strconv.ParseInt(maxLenStr, 10, 64); err == nil {
|
||||
info.MaxLength = &maxLen
|
||||
}
|
||||
}
|
||||
|
||||
fields = append(fields, info)
|
||||
}
|
||||
|
||||
return fields
|
||||
}
|
||||
|
||||
// addSecurityRequirement 为操作添加认证要求
|
||||
func (g *Generator) addSecurityRequirement(op *openapi3.Operation) {
|
||||
op.Security = []map[string][]string{
|
||||
|
||||
Reference in New Issue
Block a user