新增 Dockerfile + docker/entrypoint.sh: - 基础 crpi-.../ssw188/postgres:16-alpine + ontology 二进制,单镜像 - 默认端口 8081 / 库 ssw-ontology / 用户 ssw / 密码 ssw - /data/pg 自动挂 PG 数据,/data/archive 挂文档根目录 - trap 收尸保证 PG + ontology 同生命周期 seed 极简化为 1 个对象类型「合作企业」+ 3 条示例 (尚数网/腾讯/阿里巴巴),便于演示也便于清理。 默认 HTTP_PORT 统一为 8081(覆盖 server/config.go、run.sh、 vite.config.js、importer/import-from-xeo.js、docs/README.md、 docs/USAGE.md、docs/IMPORT.md、.env.example 等所有文档/代码)。 docs/README.md / .env.example 默认 DATABASE_URL 改为 ssw/[email protected]:5432/ssw-ontology(与 Docker 镜像自带 PG 一致); run.sh 删 dacli 硬编码 fallback,改从 .env 读(不存在则报错退出)。 README 重整:分「方式 A Docker 镜像 / 方式 B 本机 ./run.sh」, 新增 AI 智能导入(-e AI_*)与管理接口鉴权(-e MANAGER_API_KEY)说明。
351 lines
11 KiB
Go
351 lines
11 KiB
Go
package main
|
||
|
||
import (
|
||
"context"
|
||
"embed"
|
||
"io/fs"
|
||
"log"
|
||
"net/http"
|
||
"os"
|
||
"path/filepath"
|
||
"strconv"
|
||
"strings"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
"github.com/jackc/pgx/v5/pgxpool"
|
||
"github.com/joho/godotenv"
|
||
|
||
"shangshu-ontology/server/ai"
|
||
)
|
||
|
||
func pathExists(p string) bool {
|
||
st, err := os.Stat(p)
|
||
return err == nil && st.IsDir()
|
||
}
|
||
func pathExistsAbs(p string) bool {
|
||
abs, err := filepath.Abs(p)
|
||
if err != nil {
|
||
return false
|
||
}
|
||
return pathExists(abs)
|
||
}
|
||
|
||
//go:embed all:webroot
|
||
var webFS embed.FS
|
||
|
||
type app struct {
|
||
cfg *Config
|
||
pool *pgxpool.Pool
|
||
ai ai.Provider
|
||
archiveRoot string
|
||
engine *gin.Engine // 供 MCP 端点做进程内子请求,复用连接器逻辑
|
||
}
|
||
|
||
func main() {
|
||
if err := run(); err != nil {
|
||
log.Fatalf("启动失败: %v", err)
|
||
}
|
||
}
|
||
|
||
func run() error {
|
||
_ = godotenv.Load()
|
||
cfg := loadConfig()
|
||
log.Printf("[boot] 配置: %s", cfg.String())
|
||
|
||
ctx := context.Background()
|
||
p, err := initDB(ctx, cfg.dsn())
|
||
if err != nil {
|
||
return err
|
||
}
|
||
pool = p
|
||
defer pool.Close()
|
||
|
||
if cfg.SeedOnStart {
|
||
if err := seedIfEmpty(ctx, pool); err != nil {
|
||
log.Printf("[boot] 种子数据警告: %v", err)
|
||
}
|
||
// 访问控制元原语:principal/marking/grant/field_policy schema 幂等升级 + 默认 seed
|
||
if err := ensureAccessControlSchema(ctx, pool); err != nil {
|
||
log.Printf("[boot] 访问控制 schema 警告: %v", err)
|
||
}
|
||
if err := seedAccessControlDefaults(ctx, pool, cfg.IngestAPIKey); err != nil {
|
||
log.Printf("[boot] 访问控制种子警告: %v", err)
|
||
}
|
||
// Kinetic 层 ⑤Function builtin 函数同步
|
||
if err := ensureBuiltinFunctions(ctx, pool); err != nil {
|
||
log.Printf("[boot] builtin 函数同步警告: %v", err)
|
||
}
|
||
// Semantic 层 ⑦Interface 同步(注册 6 个 connector REST 端点)
|
||
if err := ensureBuiltinInterfaces(ctx, &app{cfg: cfg, pool: pool}); err != nil {
|
||
log.Printf("[boot] builtin interface 同步警告: %v", err)
|
||
}
|
||
}
|
||
|
||
provider := ai.NewProvider(ai.Config{
|
||
BaseURL: cfg.AIBaseURL,
|
||
Key: cfg.AIKey,
|
||
Model: cfg.AIModel,
|
||
})
|
||
log.Printf("[boot] AI 提供者: %s", provider.Name())
|
||
|
||
// ARCHIVE_DIR 在进程 cwd 不同时会指向不一致目录,自动 fallback 到可执行文件相邻路径
|
||
if cfg.ArchiveDir != "" && !filepath.IsAbs(cfg.ArchiveDir) {
|
||
if abs, _ := filepath.Abs(cfg.ArchiveDir); !pathExists(abs) {
|
||
if exe, err := os.Executable(); err == nil {
|
||
if try := filepath.Join(filepath.Dir(exe), "..", cfg.ArchiveDir); pathExistsAbs(try) {
|
||
cfg.ArchiveDir = try
|
||
}
|
||
}
|
||
}
|
||
}
|
||
a := &app{cfg: cfg, pool: pool, ai: provider, archiveRoot: cfg.ArchiveDir}
|
||
log.Printf("[boot] 知识文件根目录: %s", a.archiveRoot)
|
||
|
||
gin.SetMode(gin.ReleaseMode)
|
||
r := gin.New()
|
||
r.Use(gin.Recovery())
|
||
if cfg.AllowCORS {
|
||
r.Use(corsMiddleware())
|
||
}
|
||
// Dynamic Security 审计日志中间件(自动记录每次请求的访问决策)
|
||
r.Use(a.accessLogMiddleware())
|
||
startAccessLogFlusher(pool)
|
||
|
||
registerRoutes(r, a)
|
||
|
||
// 静态资源(内嵌 Vue 构建产物),SPA 回退
|
||
webroot, err := fs.Sub(webFS, "webroot")
|
||
if err != nil {
|
||
return err
|
||
}
|
||
fileServer := http.FileServer(http.FS(webroot))
|
||
r.NoRoute(func(c *gin.Context) {
|
||
if strings.HasPrefix(c.Request.URL.Path, "/api/") {
|
||
log.Printf("[warn] NoRoute 404 (API): %s %s", c.Request.Method, c.Request.URL.Path)
|
||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||
return
|
||
}
|
||
// 真实静态文件(assets 等)直接返回;其余路径回退到 index.html(SPA 路由)
|
||
rel := strings.TrimPrefix(c.Request.URL.Path, "/")
|
||
if rel == "" {
|
||
rel = "index.html"
|
||
}
|
||
if f, openErr := webroot.Open(rel); openErr == nil {
|
||
f.Close()
|
||
fileServer.ServeHTTP(c.Writer, c.Request)
|
||
return
|
||
}
|
||
data, readErr := fs.ReadFile(webroot, "index.html")
|
||
if readErr != nil {
|
||
c.Status(http.StatusNotFound)
|
||
return
|
||
}
|
||
c.Data(http.StatusOK, "text/html; charset=utf-8", data)
|
||
})
|
||
|
||
addr := ":" + strconv.Itoa(cfg.HTTPPort)
|
||
log.Printf("[boot] 尚数网本体管理平台监听 %s", addr)
|
||
return r.Run(addr)
|
||
}
|
||
|
||
func registerRoutes(r *gin.Engine, a *app) {
|
||
a.engine = r // 供 MCP 端点在进程内复用连接器 handler
|
||
r.GET("/health", func(c *gin.Context) { c.JSON(200, gin.H{"status": "ok"}) })
|
||
|
||
api := r.Group("/api/v1")
|
||
|
||
// 管理类接口(本体/实例/AI摄取),可配置 MANAGER_API_KEY
|
||
mgmt := api.Group("")
|
||
mgmt.Use(a.managerAuth())
|
||
{
|
||
ot := mgmt.Group("/ontology")
|
||
ot.GET("/object-types", a.listObjectTypes)
|
||
ot.POST("/object-types", a.createObjectType)
|
||
ot.GET("/object-types/:name", a.getObjectType)
|
||
ot.PUT("/object-types/:name", a.updateObjectType)
|
||
ot.DELETE("/object-types/:name", a.deleteObjectType)
|
||
ot.POST("/object-types/:name/properties", a.addProperty)
|
||
ot.PUT("/properties/:id", a.updateProperty)
|
||
ot.DELETE("/properties/:id", a.deleteProperty)
|
||
|
||
ot.GET("/link-types", a.listLinkTypes)
|
||
ot.POST("/link-types", a.createLinkType)
|
||
ot.DELETE("/link-types/:name", a.deleteLinkType)
|
||
|
||
// 类型梳理 (analyze / merge-preview / merge-execute / delete-zero-inst)
|
||
ot.POST("/types/analyze", a.analyzeTypes)
|
||
ot.POST("/types/merge-preview", a.mergePreview)
|
||
ot.POST("/types/merge-execute", a.mergeExecute)
|
||
ot.POST("/types/delete-zero-inst", a.deleteZeroInstTypes)
|
||
|
||
ot.GET("/action-types", a.listActionTypes)
|
||
ot.POST("/action-types", a.createActionType)
|
||
|
||
ot.GET("/function-types", a.listFunctionTypes)
|
||
ot.GET("/function-types/:name", a.getFunctionType)
|
||
ot.POST("/function-types", a.createFunctionType)
|
||
ot.DELETE("/function-types/:name", a.deleteFunctionType)
|
||
ot.POST("/function-types/:name/invoke", a.invokeFunctionType)
|
||
|
||
ot.GET("/interface-types", a.listInterfaceTypes)
|
||
ot.GET("/interface-types/:name", a.getInterfaceType)
|
||
ot.POST("/interface-types", a.createInterfaceType)
|
||
ot.DELETE("/interface-types/:name", a.deleteInterfaceType)
|
||
|
||
ot.GET("/graph", a.getGraph)
|
||
|
||
obj := mgmt.Group("/objects")
|
||
obj.GET("", a.listObjects)
|
||
obj.POST("", a.createObject)
|
||
obj.GET("/:type/:pk", a.getObject)
|
||
obj.PUT("/:type/:pk", a.updateObject)
|
||
obj.DELETE("/:type/:pk", a.deleteObject)
|
||
|
||
lk := mgmt.Group("/links")
|
||
lk.POST("", a.createLink)
|
||
lk.DELETE("/:id", a.deleteLink)
|
||
|
||
ing := mgmt.Group("/ai")
|
||
ing.POST("/ingest", a.aiIngest)
|
||
ing.GET("/ingest", a.listIngestions)
|
||
ing.GET("/ingest/:id", a.getIngestion)
|
||
}
|
||
|
||
// 知识管理:文件管理 + 数据连接资源(manager 或 ingest 主体可写,供 MCP/agent 导入)
|
||
km := api.Group("/km")
|
||
km.Use(a.ingestOrManagerAuth())
|
||
{
|
||
km.GET("/tree", a.kmTree)
|
||
km.GET("/files", a.kmListFiles)
|
||
km.POST("/folders", a.kmCreateFolder)
|
||
km.POST("/upload", a.kmUpload)
|
||
km.GET("/file", a.kmFileMeta)
|
||
km.POST("/put-text", a.kmPutText)
|
||
km.POST("/extract", a.kmExtract)
|
||
km.DELETE("/files", a.kmDeleteFiles)
|
||
|
||
km.GET("/connections", a.kmListConnections)
|
||
km.POST("/connections", a.kmCreateConnection)
|
||
km.GET("/connections/:id", a.kmConnectionDetail)
|
||
km.DELETE("/connections/:id", a.kmDeleteConnection)
|
||
km.POST("/connections/:id/import", a.kmImportConnection)
|
||
km.POST("/connections/extract", a.kmExtractConnections)
|
||
}
|
||
|
||
// 连接器接口(只读查询),可配置 CONNECTOR_API_KEY,供 WorkBuddy 等接入
|
||
conn := api.Group("/connector")
|
||
conn.Use(a.connectorAuth())
|
||
{
|
||
conn.GET("/object-types", a.connObjectTypes)
|
||
conn.GET("/objects/:type", a.connObjects)
|
||
conn.GET("/object/:type/:pk", a.connObject)
|
||
conn.GET("/search", a.connSearch)
|
||
conn.GET("/facets", a.connFacets)
|
||
conn.POST("/query", a.connQuery)
|
||
conn.GET("/graph", a.connGraph)
|
||
}
|
||
|
||
// 访问控制元原语管理接口(manager 主体可见)
|
||
// whoami 任何 principal 都可调(诊断当前身份)
|
||
api.GET("/access/whoami", a.whoami)
|
||
acc := mgmt.Group("/access")
|
||
{
|
||
acc.GET("/principals", a.listPrincipals)
|
||
acc.GET("/principals/:name", a.getPrincipal)
|
||
acc.POST("/principals", a.createPrincipal)
|
||
acc.PUT("/principals/:name", a.updatePrincipal)
|
||
acc.DELETE("/principals/:name", a.deletePrincipal)
|
||
acc.GET("/principals/:name/clearances", a.getClearances)
|
||
acc.PUT("/principals/:name/clearances", a.putClearances)
|
||
|
||
acc.GET("/grants", a.listGrants)
|
||
acc.POST("/grants", a.createGrant)
|
||
acc.DELETE("/grants/:id", a.deleteGrant)
|
||
|
||
acc.GET("/markings", a.listMarkings)
|
||
acc.GET("/objects/:type/:pk/markings", a.getObjectMarkings)
|
||
acc.PUT("/objects/:type/:pk/markings", a.putObjectMarkings)
|
||
|
||
acc.GET("/field-policies", a.listFieldPolicies)
|
||
acc.POST("/field-policies", a.createFieldPolicy)
|
||
acc.DELETE("/field-policies/:id", a.deleteFieldPolicy)
|
||
|
||
acc.GET("/preview", a.previewAsPrincipal)
|
||
acc.GET("/log", a.listAccessLog)
|
||
acc.GET("/log/stats", a.accessLogStats)
|
||
}
|
||
|
||
// MCP 协议端点(Streamable HTTP):WorkBuddy 以 url 方式一键注册
|
||
api.Any("/mcp", a.mcpAuth(), a.mcpHandle)
|
||
// MCP 工具清单(人类可读的自描述,供参考/调试)
|
||
api.GET("/mcp/manifest", a.mcpManifest)
|
||
}
|
||
|
||
func (a *app) managerAuth() gin.HandlerFunc {
|
||
return func(c *gin.Context) {
|
||
// 仅放行 manager 主体(含 dev 模式默认 manager)
|
||
p := a.principalFromRequest(c)
|
||
if p != "manager" {
|
||
c.Set("authz_decision", "deny")
|
||
c.AbortWithStatusJSON(401, gin.H{"error": "需要 manager 主体", "principal": p})
|
||
return
|
||
}
|
||
c.Next()
|
||
}
|
||
}
|
||
|
||
func (a *app) connectorAuth() gin.HandlerFunc {
|
||
return func(c *gin.Context) {
|
||
if a.cfg.ConnectorAPIKey == "" {
|
||
c.Next()
|
||
return
|
||
}
|
||
if c.GetHeader("X-API-Key") != a.cfg.ConnectorAPIKey && c.Query("key") != a.cfg.ConnectorAPIKey {
|
||
c.AbortWithStatusJSON(401, gin.H{"error": "需要 X-API-Key 连接器鉴权"})
|
||
return
|
||
}
|
||
c.Next()
|
||
}
|
||
}
|
||
|
||
// ingestOrManagerAuth 知识管理写接口鉴权:仅放行 manager 或 ingest 主体。
|
||
// ingest 主体由 MCP/agent 使用(持有 INGEST_API_KEY),可按需限定 clearance 与 grant。
|
||
func (a *app) ingestOrManagerAuth() gin.HandlerFunc {
|
||
return func(c *gin.Context) {
|
||
p := a.principalFromRequest(c)
|
||
if p != "manager" && p != "ingest" {
|
||
c.Set("authz_decision", "deny")
|
||
c.AbortWithStatusJSON(403, gin.H{"error": "需要 manager 或 ingest 主体", "principal": p})
|
||
return
|
||
}
|
||
c.Next()
|
||
}
|
||
}
|
||
|
||
// mcpAuth MCP 端点鉴权:允许 manager/ingest/connector 调用;生产环境拒绝匿名 public。
|
||
// dev 模式(MANAGER_API_KEY 与 CONNECTOR_API_KEY 均为空)下 principalFromRequest 返回 manager,自然放行。
|
||
func (a *app) mcpAuth() gin.HandlerFunc {
|
||
return func(c *gin.Context) {
|
||
p := a.principalFromRequest(c)
|
||
if p == "public" {
|
||
c.Set("authz_decision", "deny")
|
||
c.AbortWithStatusJSON(401, gin.H{"error": "MCP 需要鉴权(manager/ingest/connector 主体)", "principal": p})
|
||
return
|
||
}
|
||
c.Next()
|
||
}
|
||
}
|
||
|
||
func corsMiddleware() gin.HandlerFunc {
|
||
return func(c *gin.Context) {
|
||
c.Header("Access-Control-Allow-Origin", "*")
|
||
c.Header("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS")
|
||
c.Header("Access-Control-Allow-Headers", "Content-Type,X-API-Key,Authorization")
|
||
if c.Request.Method == http.MethodOptions {
|
||
c.AbortWithStatus(204)
|
||
return
|
||
}
|
||
c.Next()
|
||
}
|
||
}
|