Skip to content

Commit

Permalink
Merge pull request #8 from keyallis/index-post-scheme
Browse files Browse the repository at this point in the history
fix: set api url scheme if not configured
  • Loading branch information
Oscar Ward authored Apr 11, 2024
2 parents f22a3ef + 5f120f9 commit 06b2616
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions indexer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"encoding/json"
"net/http"
"net/url"
"os"
"time"

Expand All @@ -23,11 +24,19 @@ func main() {
log.Fatal("DB_URL environment variable is not set")
}

apiURL := os.Getenv("API_URL")
if apiURL == "" {
apiURLString := os.Getenv("API_URL")
if apiURLString == "" {
log.Fatal("API_URL environment variable is not set")
}

apiURL, err := url.Parse(apiURLString)
if err != nil {
log.Fatal("Failed to parse api url")
}
if apiURL.Scheme == "" {
apiURL.Scheme = "http"
}

db, err := sql.Open("postgres", dbConnectionString)
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -68,7 +77,7 @@ func insertSystemTools(db *sql.DB) {

// reindexRemoteTools reindexes all of the current remote tools in the database. If
// an error occurs, it logs the error and continues to the next tool.
func reindexRemoteTools(db *sql.DB, apiURL string) {
func reindexRemoteTools(db *sql.DB, apiURL *url.URL) {
rows, err := db.Query(`
SELECT *
FROM public."ToolEntry"
Expand Down Expand Up @@ -98,8 +107,8 @@ func reindexRemoteTools(db *sql.DB, apiURL string) {
continue
}

url := apiURL + "/" + reference
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte{}))
url := apiURL.JoinPath(reference)
req, err := http.NewRequest("POST", url.String(), bytes.NewBuffer([]byte{}))
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 06b2616

Please sign in to comment.