Refactor: 统一配置类
This commit is contained in:
@@ -0,0 +1,88 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-contrib/cors"
|
||||||
|
"github.com/joho/godotenv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ServerConfig struct {
|
||||||
|
Port string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Neo4jConfig struct {
|
||||||
|
URI string
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
}
|
||||||
|
|
||||||
|
type DataConfig struct {
|
||||||
|
FilePath string
|
||||||
|
}
|
||||||
|
|
||||||
|
type CORSConfig struct {
|
||||||
|
AllowOrigins []string
|
||||||
|
AllowMethods []string
|
||||||
|
AllowHeaders []string
|
||||||
|
ExposeHeaders []string
|
||||||
|
AllowCredentials bool
|
||||||
|
MaxAge time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Server ServerConfig
|
||||||
|
Neo4j Neo4jConfig
|
||||||
|
Data DataConfig
|
||||||
|
CORS CORSConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
func Load() (*Config, error) {
|
||||||
|
if err := godotenv.Load("../config/.env"); err != nil {
|
||||||
|
return nil, fmt.Errorf("error loading .env file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg := &Config{
|
||||||
|
Server: ServerConfig{
|
||||||
|
Port: getEnv("PORT", "3001"),
|
||||||
|
},
|
||||||
|
Neo4j: Neo4jConfig{
|
||||||
|
URI: os.Getenv("NEO4J_URI"),
|
||||||
|
Username: os.Getenv("NEO4J_USERNAME"),
|
||||||
|
Password: os.Getenv("NEO4J_PASSWORD"),
|
||||||
|
},
|
||||||
|
Data: DataConfig{
|
||||||
|
FilePath: getEnv("DATA_FILE", "data.json"),
|
||||||
|
},
|
||||||
|
CORS: CORSConfig{
|
||||||
|
AllowOrigins: []string{"http://localhost:5173", "http://localhost:3000"},
|
||||||
|
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
||||||
|
AllowHeaders: []string{"Origin", "Content-Type", "Authorization", "Accept"},
|
||||||
|
ExposeHeaders: []string{"Content-Length", "Content-Type"},
|
||||||
|
AllowCredentials: true,
|
||||||
|
MaxAge: 12 * time.Hour,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return cfg, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CORSConfig) ToGinConfig() cors.Config {
|
||||||
|
return cors.Config{
|
||||||
|
AllowOrigins: c.AllowOrigins,
|
||||||
|
AllowMethods: c.AllowMethods,
|
||||||
|
AllowHeaders: c.AllowHeaders,
|
||||||
|
ExposeHeaders: c.ExposeHeaders,
|
||||||
|
AllowCredentials: c.AllowCredentials,
|
||||||
|
MaxAge: c.MaxAge,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getEnv(key, defaultValue string) string {
|
||||||
|
if value := os.Getenv(key); value != "" {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
+12
-19
@@ -1,4 +1,4 @@
|
|||||||
package neo4j
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
@@ -7,18 +7,18 @@ import (
|
|||||||
"github.com/gin-contrib/cors"
|
"github.com/gin-contrib/cors"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
"knowledge-graph-backend/config"
|
||||||
"knowledge-graph-backend/handlers"
|
"knowledge-graph-backend/handlers"
|
||||||
"knowledge-graph-backend/services"
|
"knowledge-graph-backend/services"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
func main() {
|
||||||
PORT = "3001"
|
cfg, err := config.Load()
|
||||||
DATA_FILE = "data.json"
|
if err != nil {
|
||||||
)
|
log.Fatalf("Failed to load config: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
func test() {
|
dataService, err := services.NewDataService(cfg.Data.FilePath)
|
||||||
|
|
||||||
dataService, err := services.NewDataService(DATA_FILE)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to initialize data service: %v", err)
|
log.Fatalf("Failed to initialize data service: %v", err)
|
||||||
}
|
}
|
||||||
@@ -29,14 +29,7 @@ func test() {
|
|||||||
|
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
|
|
||||||
router.Use(cors.New(cors.Config{
|
router.Use(cors.New(cfg.CORS.ToGinConfig()))
|
||||||
AllowOrigins: []string{"http://localhost:5173", "http://localhost:3000"},
|
|
||||||
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
|
||||||
AllowHeaders: []string{"Origin", "Content-Type", "Authorization", "Accept"},
|
|
||||||
ExposeHeaders: []string{"Content-Length", "Content-Type"},
|
|
||||||
AllowCredentials: true,
|
|
||||||
MaxAge: 12 * 3600,
|
|
||||||
}))
|
|
||||||
|
|
||||||
router.GET("/health", func(c *gin.Context) {
|
router.GET("/health", func(c *gin.Context) {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
@@ -57,8 +50,8 @@ func test() {
|
|||||||
api.GET("/nodes/:id/neighbors", nodeHandler.GetNeighbors)
|
api.GET("/nodes/:id/neighbors", nodeHandler.GetNeighbors)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("Knowledge Graph API Server running on http://localhost:" + PORT)
|
log.Println("Knowledge Graph API Server running on http://localhost:" + cfg.Server.Port)
|
||||||
log.Println("Data file loaded from: " + DATA_FILE)
|
log.Println("Data file loaded from: " + cfg.Data.FilePath)
|
||||||
log.Println("Available endpoints:")
|
log.Println("Available endpoints:")
|
||||||
log.Println(" GET /health - Health check")
|
log.Println(" GET /health - Health check")
|
||||||
log.Println(" GET /api/graph - Get all graph data")
|
log.Println(" GET /api/graph - Get all graph data")
|
||||||
@@ -67,7 +60,7 @@ func test() {
|
|||||||
log.Println(" GET /api/nodes/:id - Get node by ID")
|
log.Println(" GET /api/nodes/:id - Get node by ID")
|
||||||
log.Println(" GET /api/nodes/:id/neighbors - Get node neighbors")
|
log.Println(" GET /api/nodes/:id/neighbors - Get node neighbors")
|
||||||
|
|
||||||
if err := router.Run(":" + PORT); err != nil {
|
if err := router.Run(":" + cfg.Server.Port); err != nil {
|
||||||
log.Fatalf("Failed to start server: %v", err)
|
log.Fatalf("Failed to start server: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-37
@@ -3,46 +3,25 @@ package neo4j
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
|
||||||
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
|
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
|
||||||
|
|
||||||
|
"knowledge-graph-backend/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
func DoConnect(cfg config.Neo4jConfig) {
|
||||||
NEO4J_URI string
|
ctx := context.Background()
|
||||||
NEO4J_USERNAME string
|
driver, err := neo4j.NewDriverWithContext(
|
||||||
NEO4J_PASSWORD string
|
cfg.URI,
|
||||||
)
|
neo4j.BasicAuth(cfg.Username, cfg.Password, ""))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer driver.Close(ctx)
|
||||||
|
|
||||||
func init() {
|
err = driver.VerifyConnectivity(ctx)
|
||||||
err := godotenv.Load()
|
if err != nil {
|
||||||
if err != nil {
|
panic(err)
|
||||||
log.Fatal("Error loading .env file")
|
}
|
||||||
}
|
fmt.Println("Connection established.")
|
||||||
|
|
||||||
NEO4J_URI = os.Getenv("NEO4J_URI")
|
|
||||||
NEO4J_USERNAME = os.Getenv("NEO4J_USERNAME")
|
|
||||||
NEO4J_PASSWORD = os.Getenv("NEO4J_PASSWORD")
|
|
||||||
}
|
|
||||||
|
|
||||||
func doConnect() {
|
|
||||||
ctx := context.Background()
|
|
||||||
// dbUri := "<database-uri>"
|
|
||||||
// dbUser := "<username>"
|
|
||||||
// dbPassword := "<password>"
|
|
||||||
driver, err := neo4j.NewDriverWithContext(
|
|
||||||
NEO4J_URI,
|
|
||||||
neo4j.BasicAuth(NEO4J_USERNAME, NEO4J_PASSWORD, ""))
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer driver.Close(ctx)
|
|
||||||
|
|
||||||
err = driver.VerifyConnectivity(ctx)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
fmt.Println("Connection established.")
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,20 @@
|
|||||||
package neo4j
|
package neo4j
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
func TestDoConnect(t *testing.T){
|
"knowledge-graph-backend/config"
|
||||||
doConnect()
|
)
|
||||||
|
|
||||||
|
func TestDoConnect(t *testing.T) {
|
||||||
|
cnf, err := config.Load()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("[cnf]: %v", err)
|
||||||
|
}
|
||||||
|
cfg := config.Neo4jConfig{
|
||||||
|
URI: cnf.Neo4j.URI,
|
||||||
|
Username: cnf.Neo4j.Username,
|
||||||
|
Password: cnf.Neo4j.Password,
|
||||||
|
}
|
||||||
|
DoConnect(cfg)
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user