Browse Source

parse protocol for `OLLAMA_HOST`

Jeffrey Morgan 1 year ago
parent
commit
54bb49a502
1 changed files with 12 additions and 13 deletions
  1. 12 13
      api/client.go

+ 12 - 13
api/client.go

@@ -10,9 +10,10 @@ import (
 	"net/http"
 	"net/http"
 	"net/url"
 	"net/url"
 	"os"
 	"os"
+	"strings"
 )
 )
 
 
-const DefaultHost = "http://localhost:11434"
+const DefaultHost = "localhost:11434"
 
 
 var (
 var (
 	envHost = os.Getenv("OLLAMA_HOST")
 	envHost = os.Getenv("OLLAMA_HOST")
@@ -53,23 +54,21 @@ func Host() string {
 // FromEnv creates a new client using Host() as the host. An error is returns
 // FromEnv creates a new client using Host() as the host. An error is returns
 // if the host is invalid.
 // if the host is invalid.
 func FromEnv() (*Client, error) {
 func FromEnv() (*Client, error) {
-	u, err := url.Parse(Host())
-	if err != nil {
-		return nil, err
+	h := Host()
+	if !strings.HasPrefix(h, "http://") && !strings.HasPrefix(h, "https://") {
+		h = "http://" + h
 	}
 	}
-	return &Client{Base: *u}, nil
-}
 
 
-func NewClient(hosts ...string) *Client {
-	host := DefaultHost
-	if len(hosts) > 0 {
-		host = hosts[0]
+	u, err := url.Parse(h)
+	if err != nil {
+		return nil, fmt.Errorf("could not parse host: %w", err)
 	}
 	}
 
 
-	return &Client{
-		Base: url.URL{Scheme: "http", Host: host},
-		HTTP: http.Client{},
+	if u.Port() == "" {
+		u.Host += ":11434"
 	}
 	}
+
+	return &Client{Base: *u, HTTP: http.Client{}}, nil
 }
 }
 
 
 func (c *Client) do(ctx context.Context, method, path string, reqData, respData any) error {
 func (c *Client) do(ctx context.Context, method, path string, reqData, respData any) error {