Skip to content

Commit

Permalink
Create ai client and db before passing to handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
tygern committed May 2, 2024
1 parent b0c83ba commit cd81d79
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
7 changes: 6 additions & 1 deletion cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"github.com/initialcapacity/ai-starter/internal/app"
"github.com/initialcapacity/ai-starter/pkg/ai"
"github.com/initialcapacity/ai-starter/pkg/dbsupport"
"github.com/initialcapacity/ai-starter/pkg/websupport"
"log"
)
Expand All @@ -13,7 +15,10 @@ func main() {
openAiKey := websupport.RequireEnvironmentVariable[string]("OPEN_AI_KEY")
databaseUrl := websupport.RequireEnvironmentVariable[string]("DATABASE_URL")

server := websupport.NewServer(app.Handlers(openAiKey, openAiEndpoint, databaseUrl))
aiClient := ai.NewClient(openAiKey, openAiEndpoint)
db := dbsupport.CreateConnection(databaseUrl)

server := websupport.NewServer(app.Handlers(aiClient, db))

_, done := server.Start(host, port)
log.Fatal(<-done)
Expand Down
6 changes: 2 additions & 4 deletions internal/app/handlers.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package app

import (
"database/sql"
"github.com/initialcapacity/ai-starter/internal/analyzer"
"github.com/initialcapacity/ai-starter/pkg/ai"
"github.com/initialcapacity/ai-starter/pkg/dbsupport"
"io/fs"
"net/http"
)

func Handlers(openAiKey, openAiEndpoint, databaseUrl string) func(mux *http.ServeMux) {
aiClient := ai.NewClient(openAiKey, openAiEndpoint)
db := dbsupport.CreateConnection(databaseUrl)
func Handlers(aiClient ai.Client, db *sql.DB) func(mux *http.ServeMux) {
embeddingsGateway := analyzer.NewEmbeddingsGateway(db)

return func(mux *http.ServeMux) {
Expand Down
2 changes: 1 addition & 1 deletion internal/app/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestHealth(t *testing.T) {
server := websupport.NewServer(app.Handlers("", "", ""))
server := websupport.NewServer(app.Handlers(testsupport.NewTestAiClient(""), nil))
port, _ := server.Start("localhost", 0)
testsupport.AssertHealthy(t, port, "/health")
}

0 comments on commit cd81d79

Please sign in to comment.