Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
aymericbeaumet committed Jul 10, 2024
1 parent 01872f7 commit 095cacb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
1 change: 1 addition & 0 deletions ai/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions ai/go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
40 changes: 30 additions & 10 deletions ai/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 19 in ai/main.go

View workflow job for this annotation

GitHub Actions / lint (./ai)

var `hostname` is unused (unused)
port = envOrDefault("PORT", "3000") // port used by the server

Check failure on line 20 in ai/main.go

View workflow job for this annotation

GitHub Actions / lint (./ai)

var `port` is unused (unused)
)

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
Expand All @@ -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()

Expand All @@ -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 {

Check failure on line 76 in ai/main.go

View workflow job for this annotation

GitHub Actions / lint (./ai)

func `envOrDefault` is unused (unused)
if v := strings.TrimSpace(os.Getenv(key)); len(v) > 0 {
return v
}
return defaultValue
}

0 comments on commit 095cacb

Please sign in to comment.