85 lines
2.4 KiB
Go
85 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/gin-contrib/cors"
|
|
"github.com/gin-gonic/gin"
|
|
swaggerFiles "github.com/swaggo/files"
|
|
ginSwagger "github.com/swaggo/gin-swagger"
|
|
|
|
"knowledge-graph-backend/config"
|
|
"knowledge-graph-backend/docs"
|
|
"knowledge-graph-backend/handlers"
|
|
neo4jClient "knowledge-graph-backend/neo4j"
|
|
"knowledge-graph-backend/services"
|
|
)
|
|
|
|
// @title 知识图谱 API
|
|
// @version 1.0.0
|
|
// @description 知识图谱后端服务,提供图数据查询、节点搜索等功能
|
|
// @host localhost:8080
|
|
// @BasePath /
|
|
func main() {
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
log.Fatalf("Failed to load config: %v", err)
|
|
}
|
|
|
|
driver := neo4jClient.NewDriver(cfg.Neo4j)
|
|
defer driver.Close(context.Background())
|
|
|
|
graphService := services.NewNeo4jService(driver)
|
|
|
|
graphHandler := handlers.NewGraphHandler(graphService)
|
|
searchHandler := handlers.NewSearchHandler(graphService)
|
|
nodeHandler := handlers.NewNodeHandler(graphService)
|
|
|
|
router := gin.Default()
|
|
|
|
router.Use(cors.New(cfg.CORS.ToGinConfig()))
|
|
|
|
docs.SwaggerInfo.BasePath = "/"
|
|
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
|
|
|
// @Summary 健康检查
|
|
// @Description 检查服务是否正常运行
|
|
// @Produce json
|
|
// @Success 200 {object} map[string]string "成功"
|
|
// @Router /health [get]
|
|
router.GET("/health", func(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"status": "ok",
|
|
"message": "Knowledge Graph API is running",
|
|
"version": "1.0.0",
|
|
})
|
|
})
|
|
|
|
api := router.Group("/api")
|
|
{
|
|
api.GET("/graph", graphHandler.GetGraphData)
|
|
api.GET("/graph/stats", graphHandler.GetStats)
|
|
|
|
api.GET("/search", searchHandler.SearchNodes)
|
|
|
|
api.GET("/nodes/:id", nodeHandler.GetNodeByID)
|
|
api.GET("/nodes/:id/neighbors", nodeHandler.GetNeighbors)
|
|
}
|
|
|
|
log.Println("Knowledge Graph API Server running on http://localhost:" + cfg.Server.Port)
|
|
log.Println("Data file loaded from: " + cfg.Data.FilePath)
|
|
log.Println("Available endpoints:")
|
|
log.Println(" GET /health - Health check")
|
|
log.Println(" GET /api/graph - Get all graph data")
|
|
log.Println(" GET /api/graph/stats - Get graph statistics")
|
|
log.Println(" GET /api/search?q=query - Search nodes")
|
|
log.Println(" GET /api/nodes/:id - Get node by ID")
|
|
log.Println(" GET /api/nodes/:id/neighbors - Get node neighbors")
|
|
|
|
if err := router.Run(":" + cfg.Server.Port); err != nil {
|
|
log.Fatalf("Failed to start server: %v", err)
|
|
}
|
|
}
|