diff --git a/ai/go.mod b/ai/go.mod index f3b3326..99b1d37 100644 --- a/ai/go.mod +++ b/ai/go.mod @@ -3,6 +3,7 @@ module goapp go 1.22.5 require ( + github.com/go-chi/chi/v5 v5.1.0 github.com/joho/godotenv v1.5.1 github.com/lib/pq v1.10.9 ) diff --git a/ai/go.sum b/ai/go.sum index ecb9035..49887b7 100644 --- a/ai/go.sum +++ b/ai/go.sum @@ -1,3 +1,5 @@ +github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= +github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= diff --git a/ai/main.go b/ai/main.go index a13f076..308db0c 100644 --- a/ai/main.go +++ b/ai/main.go @@ -4,21 +4,27 @@ import ( "database/sql" "fmt" "io" - "log" "net" "net/http" "net/url" "os" + "strings" + "github.com/go-chi/chi/v5" "github.com/joho/godotenv" _ "github.com/lib/pq" ) +var ( + hostname = envOrDefault("HTTP_HOST", "localhost") // hostname used by the server + port = envOrDefault("PORT", "3000") // port used by the server +) + func main() { // Load .env file err := godotenv.Load() if err != nil { - log.Fatalf("Error loading .env file: %v", err) + panic(fmt.Errorf("couldn't loading .env file: %w", err)) } // Connect to the PostgreSQL instance @@ -29,7 +35,7 @@ func main() { Path: os.Getenv("POSTGRES_DB"), }).String()) if err != nil { - log.Fatalf("Error connecting to PostgreSQL: %v", err) + panic(fmt.Errorf("failed ot connect to PostgreSQL: %w", err)) } defer db.Close() @@ -40,22 +46,36 @@ func main() { }).String() resp, err := http.Get(qdrantBaseURL + "/v1/collections") if err != nil { - log.Fatalf("Error connecting to Qdrant: %v", err) + panic(fmt.Errorf("failed to query Qdrant DB: %w", err)) } defer resp.Body.Close() // Declare your http routes - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + r := chi.NewRouter() + + r.Get("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) _, _ = io.WriteString(w, "Hello, World!") - }) + })) - http.HandleFunc("/livez", func(w http.ResponseWriter, r *http.Request) { + http.HandleFunc("/livez", http.HandlerFunc(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)) + addr := net.JoinHostPort("localhost", "8080") + fmt.Printf("Starting server on %s\n", addr) + if err := http.ListenAndServe(addr, r); err != nil { + panic(fmt.Errorf("failed to start server on %s: %w", addr, err)) + } +} + +// envOrDefault returns the value of the environment variable at the given key. +// Fallbacks to the given default if the value found is missing or empty. +func envOrDefault(key string, defaultValue string) string { + if v := strings.TrimSpace(os.Getenv(key)); len(v) > 0 { + return v + } + return defaultValue }