- access_log 中间件按路由模板映射 target(修复 :name/:id 参数名导致
/object-types/:name 等路由 target 长期为空);401/4xx 兜底记 deny;
action 简化为 read/write/delete/manage;新增 90 天保留策略
(ACCESS_LOG_RETENTION_DAYS 覆盖,0 关闭)
- schema 审计:对象类型/属性/链接类型/链接属性/动作类型/分级配置的
增删改写入 ontology_audit_log(target_pk 落库,update/delete 带 before 快照);
存量库补 target_pk 列(幂等 ALTER)
- 新增 /api/v1/analytics/{overview,types,users,sharing}(仅 manager):
总体读写比/活跃主体/来源分布/按天趋势、分项热度、用户活跃、共享授权使用量
- 前端新增「使用分析」tab(AnalyticsView)
476 lines
17 KiB
Go
476 lines
17 KiB
Go
package main
|
||
|
||
import (
|
||
"context"
|
||
"embed"
|
||
"io/fs"
|
||
"log"
|
||
"net/http"
|
||
"os"
|
||
"path/filepath"
|
||
"strconv"
|
||
"strings"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
"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 dbRunner // dbRunner 是 *pgxpool.Pool / *txPool 的接口,snapshot_router.go 定义
|
||
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); 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)
|
||
// access_log 保留策略(2026-09-11):默认 90 天,ACCESS_LOG_RETENTION_DAYS 覆盖
|
||
startAccessLogRetention(pool, cfg.AccessLogRetentionDays)
|
||
|
||
// 物化层调度器(P2-1):后台 ticker 定期刷新 interval 物化
|
||
startMaterializationScheduler(context.Background(), a)
|
||
|
||
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"}) })
|
||
// 版本自描述(公开):部署核对用,前端页脚 / MCP get_server_info 同源
|
||
r.GET("/api/v1/version", a.versionHandler)
|
||
r.GET("/api/v1/version/changelog", a.changelogHandler)
|
||
// 反馈代理(公开):bug/建议自动转 Gitea issue,调用方零凭据
|
||
r.POST("/api/v1/feedback", a.feedbackHandler)
|
||
|
||
api := r.Group("/api/v1")
|
||
// 快照路由中间件:读 X-Snapshot-Id / X-Snapshot-Ts 头 → 把 a.pool 临时换为事务视图
|
||
// (SET LOCAL search_path TO backups_<id>,> public)。写方法带快照头 → 403。
|
||
// 必须在 loginRequired / managerAuth 之前,这样 GET 走快照不被鉴权误拦。
|
||
api.Use(a.snapshotMiddleware())
|
||
|
||
// 账号密码登录(公开端点,登录前访问)
|
||
api.POST("/auth/login", a.authLogin)
|
||
api.POST("/auth/logout", a.authLogout)
|
||
api.GET("/auth/me", a.authMe)
|
||
api.POST("/auth/change-password", a.changePassword)
|
||
|
||
// 管理类接口(本体/实例/AI摄取):需 manager 主体(密码登录或 manager 的 api_key)
|
||
mgmt := api.Group("")
|
||
mgmt.Use(a.loginRequired(), 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)
|
||
|
||
// 数据分级(issue #5):类型级分级配置总览 + 实例生效分级批量重算(默认 dry_run)。
|
||
// 挂在本组而非 /objects 下,避免与 /objects/:type/:pk 参数路由冲突被抢占匹配。
|
||
ot.GET("/classification/config", a.classificationConfig)
|
||
// 管理员设置对象类型的分级配置(v1.3.0):之前只有 GET 没有 PUT,导致前端保存失败
|
||
// 「对象类型分级」路由按对象名(/object-types/:name/classification)放,方便前端「按类型编辑」
|
||
ot.PUT("/object-types/:name/classification", a.setObjectTypeClassification)
|
||
ot.POST("/classification/recompute", a.recomputeClassification)
|
||
|
||
ot.GET("/link-types", a.listLinkTypes)
|
||
ot.POST("/link-types", a.createLinkType)
|
||
// 链接属性(链接本身带字段,P1-3)
|
||
ot.GET("/link-types/:name/properties", a.listLinkProperties)
|
||
ot.POST("/link-types/:name/properties", a.addLinkProperty)
|
||
ot.DELETE("/link-properties/:id", a.deleteLinkProperty)
|
||
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)
|
||
|
||
// 物化层(P2-1 2026-08-24)
|
||
ot.GET("/materializations", a.listMaterializations)
|
||
ot.POST("/materializations", a.createMaterialization)
|
||
ot.DELETE("/materializations/:name", a.deleteMaterialization)
|
||
ot.POST("/materializations/:name/refresh", a.refreshMaterializationHandler)
|
||
ot.GET("/materializations/:name/rows", a.listMaterializationRows)
|
||
|
||
// Backing Sources(P2-2 2026-08-24):物化层的源配置(精简版仅 metadata)
|
||
ot.GET("/backing-sources", a.listBackingSources)
|
||
ot.POST("/backing-sources", a.createBackingSource)
|
||
ot.DELETE("/backing-sources/:name", a.deleteBackingSource)
|
||
|
||
// 提示调度(Suggestive Dispatch):按当前上下文返回 Function/Action 建议列表(2026-08-24)
|
||
ot.GET("/suggestions", a.listSuggestions)
|
||
|
||
ot.GET("/graph", a.getGraph)
|
||
ot.GET("/links/count", a.countLinks)
|
||
|
||
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)
|
||
// 提交 Action(P0-1 + P0-3:完整契约 + FBA 执行链路,2026-08-24)
|
||
obj.POST("/:type/:pk/actions/:action", a.applyAction)
|
||
|
||
// 实例治理(2026-09-05):批量写入 + 变更历史 + 软删除回滚 + 重复实体扫描
|
||
obj.POST("/batch", a.batchCreateObjects)
|
||
obj.GET("/changes", a.listRecentChanges)
|
||
obj.GET("/duplicates", a.findDuplicates)
|
||
obj.POST("/:type/:pk/restore", a.restoreObject)
|
||
obj.GET("/:type/:pk/history", a.getObjectHistory)
|
||
|
||
lk := mgmt.Group("/links")
|
||
lk.POST("", a.createLink)
|
||
lk.POST("/batch", a.batchCreateLinks)
|
||
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)
|
||
// 文件级排除标记(2026-09-05):标记后默认不参与批量抽取
|
||
km.POST("/exclude", a.kmSetExcluded)
|
||
// 文件级分级(issue #5-⑤):整份文档打级,传导给已抽取实例(文件越敏→产物越敏)
|
||
km.POST("/classification", a.kmSetClassification)
|
||
|
||
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 时须在 X-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)
|
||
// 实例治理(2026-09-05):MCP 写入端点
|
||
conn.POST("/objects/batch", a.batchCreateObjects)
|
||
conn.POST("/links/batch", a.batchCreateLinks)
|
||
}
|
||
|
||
// 访问控制元原语管理接口(manager 主体可见)
|
||
// whoami 任何 principal 都可调(诊断当前身份)——挂在 mgmt 外,登录即可查
|
||
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)
|
||
}
|
||
|
||
// 使用分析(2026-09-11):本体总体/分项使用、用户活跃、共享情况(仅 manager)
|
||
ana := mgmt.Group("/analytics")
|
||
{
|
||
ana.GET("/overview", a.analyticsOverview)
|
||
ana.GET("/types", a.analyticsTypes)
|
||
ana.GET("/users", a.analyticsUsers)
|
||
ana.GET("/sharing", a.analyticsSharing)
|
||
}
|
||
|
||
// 数据备份(v1.3.0):仅 manager 主体可访问,含 create/inspect/list/download/diff/delete
|
||
// 不提供 restore 到线上——MCP 客户端拿到 dump 后在另一实例验证恢复路径
|
||
bkp := mgmt.Group("/backups")
|
||
{
|
||
bkp.POST("", a.createBackupHandler)
|
||
bkp.GET("", a.listBackupsHandler)
|
||
bkp.GET("/:id", a.inspectBackupHandler)
|
||
bkp.GET("/:id/switch", a.switchBackupHandler)
|
||
bkp.DELETE("/:id", a.deleteBackupHandler)
|
||
}
|
||
|
||
// 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()
|
||
}
|
||
}
|
||
|
||
// loginRequired 强制要求已登录(cookie session 或 X-API-Key)。
|
||
// manager 主体尚未配置 api_key(全新库、seed 未运行)→ 跳过,便于首次引导。
|
||
// 真实鉴权模式(manager 已有 key)下,到达此中间件的请求必须带 session 或 API Key,否则 401。
|
||
// 注意:本中间件不区分角色(manager/ingest/connector 都可过);
|
||
// 角色门由 managerAuth / ingestOrManagerAuth 等下游中间件把控。
|
||
func (a *app) loginRequired() gin.HandlerFunc {
|
||
return func(c *gin.Context) {
|
||
// manager 尚未配置密钥时保留开放体验:无人也能进(默认 manager)
|
||
if !a.managerKeySet() {
|
||
c.Next()
|
||
return
|
||
}
|
||
p := a.principalFromRequest(c)
|
||
if p == "" || p == "public" {
|
||
c.AbortWithStatusJSON(401, gin.H{"error": "需要登录或有效 API Key", "principal": p})
|
||
return
|
||
}
|
||
c.Next()
|
||
}
|
||
}
|
||
|
||
// connectorAuth 连接器入口鉴权:key 是「任意主体的有效 api_key」即可放行(修复 issue #8 v2)。
|
||
//
|
||
// 历史:早期只接受 name='connector' 的 key(连接器入口 = 单一 connector 身份)。
|
||
// v2:放宽到任一主体的 key——既然 manager / connector / analyst 等各有 api_key,
|
||
// 连接器入口就应当接受调用方的真实身份,让 principalFromRequest 按真实主体解析,
|
||
// 读路径的 visibility / classification filter 即按真实身份工作(manager 豁免、
|
||
// connector/analyst 按各自 clearance 子集)。
|
||
//
|
||
// 未提供 key 时:若全库任意主体已配置 key 则要求带(401),全库无 key 则放行(无密钥只读场景)。
|
||
func (a *app) connectorAuth() gin.HandlerFunc {
|
||
return func(c *gin.Context) {
|
||
key := c.GetHeader("X-API-Key")
|
||
if key == "" {
|
||
key = c.Query("key")
|
||
}
|
||
if key == "" {
|
||
// 无 key:若任意主体已有 key,则视为必须带 key;否则放行(无密钥只读兼容)
|
||
if a.anyPrincipalKeySet() {
|
||
c.AbortWithStatusJSON(401, gin.H{"error": "连接器接口需要 X-API-Key"})
|
||
return
|
||
}
|
||
c.Next()
|
||
return
|
||
}
|
||
var name string
|
||
if err := a.pool.QueryRow(c,
|
||
"SELECT name FROM principals WHERE api_key=$1", key).Scan(&name); err == nil && name != "" {
|
||
c.Next()
|
||
return
|
||
}
|
||
c.AbortWithStatusJSON(401, gin.H{"error": "连接器密钥无效"})
|
||
}
|
||
}
|
||
|
||
// ingestOrManagerAuth 知识管理写接口鉴权:仅放行 manager 或 ingest 主体。
|
||
// ingest 主体由 MCP/agent 使用(持有 seed 随机 key 或 UI 设置的 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。
|
||
// manager 尚未配置 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()
|
||
}
|
||
}
|