From 7c2a6342d0a26a6c1363144a456bcd2b98fcb2c9 Mon Sep 17 00:00:00 2001 From: Aymeric Beaumet Date: Wed, 10 Jul 2024 16:46:21 +0200 Subject: [PATCH] wip --- ai/.env | 9 +++++---- ai/main.go | 36 +++++++++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/ai/.env b/ai/.env index fe3953b..88a36b8 100644 --- a/ai/.env +++ b/ai/.env @@ -1,7 +1,8 @@ POSTGRES_HOST=localhost POSTGRES_PORT=5432 -POSTGRES_USER=youruser -POSTGRES_PASSWORD=yourpassword -POSTGRES_DB=yourdbname +POSTGRES_USER=hearthands +POSTGRES_PASSWORD=hearthands +POSTGRES_DB=hearthands + QDRANT_HOST=localhost -QDRANT_PORT=6333 \ No newline at end of file +QDRANT_PORT=6333 diff --git a/ai/main.go b/ai/main.go index 5b28abc..831ac12 100644 --- a/ai/main.go +++ b/ai/main.go @@ -3,8 +3,11 @@ package main import ( "database/sql" "fmt" + "io" "log" + "net" "net/http" + "net/url" "os" "github.com/joho/godotenv" @@ -18,26 +21,41 @@ func main() { log.Fatalf("Error loading .env file: %v", err) } - // Connect to PostgreSQL - pgConnStr := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", - os.Getenv("POSTGRES_HOST"), os.Getenv("POSTGRES_PORT"), os.Getenv("POSTGRES_USER"), os.Getenv("POSTGRES_PASSWORD"), os.Getenv("POSTGRES_DB")) - db, err := sql.Open("postgres", pgConnStr) + // Connect to the PostgreSQL instance + db, err := sql.Open("postgres", (&url.URL{ + Scheme: "postgres", + User: url.UserPassword(os.Getenv("POSTGRES_USER"), os.Getenv("POSTGRES_PASSWORD")), + Host: net.JoinHostPort(os.Getenv("POSTGRES_HOST"), os.Getenv("POSTGRES_PORT")), + Path: os.Getenv("POSTGRES_DB"), + }).String()) if err != nil { log.Fatalf("Error connecting to PostgreSQL: %v", err) } defer db.Close() - // Connect to Qdrant - qdrantURL := fmt.Sprintf("http://%s:%s", os.Getenv("QDRANT_HOST"), os.Getenv("QDRANT_PORT")) - resp, err := http.Get(qdrantURL + "/v1/collections") + // Query the Qdrant instance + qdrantBaseURL := (&url.URL{ + Scheme: "http", + Host: net.JoinHostPort(os.Getenv("QDRANT_HOST"), os.Getenv("QDRANT_PORT")), + }).String() + resp, err := http.Get(qdrantBaseURL + "/v1/collections") if err != nil { log.Fatalf("Error connecting to Qdrant: %v", err) } defer resp.Body.Close() - // Simple HTTP server + // Declare your http routes + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "Connected to PostgreSQL and Qdrant successfully!") + w.WriteHeader(http.StatusOK) + io.WriteString(w, "Hello, World!") + }) + + http.HandleFunc("/livez", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) }) + + // Start the server, which will listen on http://localhost:8080 + fmt.Println("Starting server on port 8080") log.Fatal(http.ListenAndServe(":8080", nil)) }