removed client isLocal()

This commit is contained in:
Josh Yan 2024-07-08 10:33:47 -07:00
parent 689a7cb90d
commit 4c1e188200
3 changed files with 17 additions and 135 deletions

View File

@ -390,26 +390,6 @@ func (c *Client) Version(ctx context.Context) (string, error) {
return version.Version, nil
}
// IsLocal checks whether the client is connecting to a local server.
func (c *Client) IsLocal() bool {
// Resolve the host to an IP address and check if the IP is local
// Currently, only checks if it is localhost or loopback
host, _, err := net.SplitHostPort(c.base.Host)
if err != nil {
host = c.base.Host
}
if host == "" || host == "localhost" {
return true
}
if ip := net.ParseIP(host); ip != nil {
return ip.IsLoopback()
}
return false
}
func Authorization(ctx context.Context, request *http.Request) (string, error) {
data := []byte(fmt.Sprintf("%s,%s,%d", request.Method, request.URL.RequestURI(), time.Now().Unix()))

View File

@ -1,8 +1,6 @@
package api
import (
"net/http"
"net/url"
"testing"
"github.com/ollama/ollama/envconfig"
@ -48,80 +46,3 @@ func TestClientFromEnvironment(t *testing.T) {
})
}
}
// Test function
func TestIsLocal(t *testing.T) {
type test struct {
client *Client
want bool
err error
}
tests := map[string]test{
"localhost": {
client: func() *Client {
baseURL, _ := url.Parse("http://localhost:1234")
return &Client{base: baseURL, http: &http.Client{}}
}(),
want: true,
err: nil,
},
"127.0.0.1": {
client: func() *Client {
baseURL, _ := url.Parse("http://127.0.0.1:1234")
return &Client{base: baseURL, http: &http.Client{}}
}(),
want: true,
err: nil,
},
"example.com": {
client: func() *Client {
baseURL, _ := url.Parse("http://example.com:1111")
return &Client{base: baseURL, http: &http.Client{}}
}(),
want: false,
err: nil,
},
"8.8.8.8": {
client: func() *Client {
baseURL, _ := url.Parse("http://8.8.8.8:1234")
return &Client{base: baseURL, http: &http.Client{}}
}(),
want: false,
err: nil,
},
"empty host with port": {
client: func() *Client {
baseURL, _ := url.Parse("http://:1234")
return &Client{base: baseURL, http: &http.Client{}}
}(),
want: true,
err: nil,
},
"empty host without port": {
client: func() *Client {
baseURL, _ := url.Parse("http://")
return &Client{base: baseURL, http: &http.Client{}}
}(),
want: true,
err: nil,
},
"remote host without port": {
client: func() *Client {
baseURL, _ := url.Parse("http://example.com")
return &Client{base: baseURL, http: &http.Client{}}
}(),
want: false,
err: nil,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := tc.client.IsLocal()
if got != tc.want {
t.Errorf("test %s failed: got %v, want %v", name, got, tc.want)
}
})
}
}

View File

@ -282,52 +282,33 @@ func createBlob(cmd *cobra.Command, client *api.Client, path string) (string, er
digest := fmt.Sprintf("sha256:%x", hash.Sum(nil))
// Here, we want to check if the server is local
// If true, call, createBlobLocal
// This should find the model directory, copy blob over, and return the digest
// If this fails, just upload it
// If this is successful, return the digest
// We check if we can find the models directory locally
// If we can, we return the path to the directory
// If we can't, we return an error
// If the blob exists already, we return the digest
dest, err := getLocalPath(cmd.Context(), digest)
// Resolve server to IP
// Check if server is local
/* if client.IsLocal() {
digest = strings.ReplaceAll(digest, ":", "-")
config, err := client.HeadBlob(cmd.Context(), digest)
if err != nil {
return "", err
}
if errors.Is(err, ErrBlobExists) {
return digest, nil
}
modelDir := config.ModelDir
// Get blob destination
dest := filepath.Join(modelDir, "blobs", digest)
err = createBlobLocal(path, dest)
// Successfuly found the model directory
if err == nil {
// Copy blob in via OS specific copy
// Linux errors out to use io.copy
err = localCopy(path, dest)
if err == nil {
return digest, nil
}
} */
if client.IsLocal() {
dest, err := getLocalPath(cmd.Context(), digest)
if errors.Is(err, ErrBlobExists) {
return digest, nil
}
// Default copy using io.copy
err = defaultCopy(path, dest)
if err == nil {
err = localCopy(path, dest)
if err == nil {
return digest, nil
}
err = defaultCopy(path, dest)
if err == nil {
return digest, nil
}
return digest, nil
}
}
// If at any point copying the blob over locally fails, we default to the copy through the server
if err = client.CreateBlob(cmd.Context(), digest, bin); err != nil {
return "", err
}