feat: docs api design

This commit is contained in:
neo-f
2023-03-22 22:45:17 +08:00
commit 084d0de8bc
52 changed files with 3420 additions and 0 deletions

23
internal/router/base.go Normal file
View File

@@ -0,0 +1,23 @@
package router
import (
"github.com/casdoor/casdoor-go-sdk/casdoorsdk"
"github.com/gofiber/fiber/v2"
"github.com/rs/zerolog/log"
)
var userClaimsKey = struct{}{}
func JWTRequired(c *fiber.Ctx) error {
jwt := c.Get("Authorization")
if jwt == "" {
return c.Status(401).JSON(fiber.Map{"msg": "Unauthorized"})
}
claims, err := casdoorsdk.ParseJwtToken(jwt)
if err != nil {
log.Ctx(c.UserContext()).Error().Err(err).Msg("Unauthorized user")
return c.Status(401).JSON(fiber.Map{"msg": "Unauthorized"})
}
c.Locals(userClaimsKey, claims)
return nil
}

82
internal/router/debug.go Normal file
View File

@@ -0,0 +1,82 @@
package router
import (
"context"
"fmt"
"octopus/statics"
"sort"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/basicauth"
"github.com/neo-f/soda"
)
func RegisterDebuggerRouter(app *soda.Soda) {
app.App.Get("/debugger", basicAuth, func(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
return c.SendString(statics.DebuggerHTML)
})
app.App.Get("/debugger/functions", basicAuth, func(c *fiber.Ctx) error {
sort.Slice(debugFunctions, func(i, j int) bool {
return debugFunctions[i].Title < debugFunctions[j].Title
})
return c.JSON(debugFunctions)
})
app.App.Post("/debugger/execute", basicAuth, func(c *fiber.Ctx) error {
var params struct {
Func string `json:"func"`
Params map[string]string `json:"params"`
}
if err := c.BodyParser(&params); err != nil {
return err
}
var fn *DebugFunction
for _, f := range debugFunctions {
if f.Name == params.Func {
fn = &f
break
}
}
if fn == nil {
return fmt.Errorf("function not found")
}
resp, err := fn.Do(c.UserContext(), params.Params)
if err != nil {
return err
}
return c.JSON(resp)
})
}
var basicAuth = basicauth.New(basicauth.Config{
Users: map[string]string{"neo": "whosyourdaddy"},
})
type DebugFunctionParameter struct {
Name string `json:"name"`
Description string `json:"description"`
Required bool `json:"required"`
}
type DebugFunction struct {
Name string `json:"name"`
Title string `json:"title"`
Description string `json:"description"`
Parameters []DebugFunctionParameter `json:"parameters"`
Do func(context.Context, map[string]string) (interface{}, error) `json:"-"`
}
var debugFunctions = []DebugFunction{
{
Name: "echo",
Title: "demo debugger",
Description: "echo the parameters",
Parameters: []DebugFunctionParameter{
{Name: "param-a", Description: "param-a", Required: true},
{Name: "param-b", Description: "param-b", Required: true},
},
Do: func(ctx context.Context, params map[string]string) (interface{}, error) {
return params, nil
},
},
}

81
internal/router/doc.go Normal file
View File

@@ -0,0 +1,81 @@
package router
import (
"octopus/internal/schema"
"github.com/neo-f/soda"
)
func RegisterDocRouter(app *soda.Soda) {
app.Get("/docs", nil).
AddTags("文档管理").
SetSummary("获取文档列表").
AddJWTSecurity(JWTRequired).
SetParameters(schema.ListDocQuery{}).
AddJSONResponse(200, schema.DocList{}).OK()
app.Post("/docs", nil).
AddTags("文档管理").
SetSummary("新建文档").
AddJWTSecurity(JWTRequired).
SetJSONRequestBody(schema.CreateDoc{}).
AddJSONResponse(200, schema.Doc{}).OK()
app.Put("/docs/:id", nil).
AddTags("文档管理").
SetSummary("更新文档").
AddJWTSecurity(JWTRequired).
SetParameters(schema.DocID{}).
SetJSONRequestBody(schema.ListDocQuery{}).
AddJSONResponse(200, schema.Doc{}).OK()
app.Delete("/docs/:id", nil).
AddTags("文档管理").
SetSummary("获取文档列表").
AddJWTSecurity(JWTRequired).
SetParameters(schema.DocID{}).
AddJSONResponse(200, nil).OK()
app.Post("/docs/batch/delete", nil).
AddTags("文档管理").
SetSummary("批量-文件删除").
AddJWTSecurity(JWTRequired).
SetJSONRequestBody(schema.DocsBatchDelete{}).
AddJSONResponse(200, schema.DocsBatchResults{}).OK()
app.Post("/docs/batch/update", nil).
AddTags("文档管理").
SetSummary("批量-文件删除").
AddJWTSecurity(JWTRequired).
SetJSONRequestBody(schema.DocsBatchUpdate{}).
AddJSONResponse(200, schema.DocsBatchResults{}).OK()
app.Get("/doc-folders", nil).
AddTags("文档管理").
SetSummary("获取文件夹树").
AddJWTSecurity(JWTRequired).
SetParameters(schema.GetDocFolderTree{}).
AddJSONResponse(200, schema.DocFolderWithChildren{}).OK()
app.Post("/doc-folders", nil).
AddTags("文档管理").
SetSummary("新建文件夹").
AddJWTSecurity(JWTRequired).
SetJSONRequestBody(schema.CreateDocFolder{}).
AddJSONResponse(200, schema.DocFolder{}).OK()
app.Put("/doc-folders/:id", nil).
AddTags("文档管理").
SetSummary("更新文件夹").
AddJWTSecurity(JWTRequired).
SetParameters(schema.DocFolderID{}).
SetJSONRequestBody(schema.UpdateDocFolder{}).
AddJSONResponse(200, schema.DocFolder{}).OK()
app.Delete("/doc-folders/:id", nil).
AddTags("文档管理").
SetSummary("删除文件夹").
AddJWTSecurity(JWTRequired).
SetParameters(schema.DocFolderID{}).
AddJSONResponse(200, nil).OK()
}