passthrough OLLAMA_HOST path to client

This commit is contained in:
Michael Yang 2024-08-23 13:16:30 -07:00
parent 90ca84172c
commit 386af6c1a0
2 changed files with 27 additions and 30 deletions

View file

@ -30,9 +30,7 @@ func Host() *url.URL {
defaultPort = "443" defaultPort = "443"
} }
// trim trailing slashes hostport, path, _ := strings.Cut(hostport, "/")
hostport = strings.TrimRight(hostport, "/")
host, port, err := net.SplitHostPort(hostport) host, port, err := net.SplitHostPort(hostport)
if err != nil { if err != nil {
host, port = "127.0.0.1", defaultPort host, port = "127.0.0.1", defaultPort
@ -45,15 +43,13 @@ func Host() *url.URL {
if n, err := strconv.ParseInt(port, 10, 32); err != nil || n > 65535 || n < 0 { if n, err := strconv.ParseInt(port, 10, 32); err != nil || n > 65535 || n < 0 {
slog.Warn("invalid port, using default", "port", port, "default", defaultPort) slog.Warn("invalid port, using default", "port", port, "default", defaultPort)
return &url.URL{ port = defaultPort
Scheme: scheme,
Host: net.JoinHostPort(host, defaultPort),
}
} }
return &url.URL{ return &url.URL{
Scheme: scheme, Scheme: scheme,
Host: net.JoinHostPort(host, port), Host: net.JoinHostPort(host, port),
Path: path,
} }
} }

View file

@ -13,34 +13,35 @@ func TestHost(t *testing.T) {
value string value string
expect string expect string
}{ }{
"empty": {"", "127.0.0.1:11434"}, "empty": {"", "http://127.0.0.1:11434"},
"only address": {"1.2.3.4", "1.2.3.4:11434"}, "only address": {"1.2.3.4", "http://1.2.3.4:11434"},
"only port": {":1234", ":1234"}, "only port": {":1234", "http://:1234"},
"address and port": {"1.2.3.4:1234", "1.2.3.4:1234"}, "address and port": {"1.2.3.4:1234", "http://1.2.3.4:1234"},
"hostname": {"example.com", "example.com:11434"}, "hostname": {"example.com", "http://example.com:11434"},
"hostname and port": {"example.com:1234", "example.com:1234"}, "hostname and port": {"example.com:1234", "http://example.com:1234"},
"zero port": {":0", ":0"}, "zero port": {":0", "http://:0"},
"too large port": {":66000", ":11434"}, "too large port": {":66000", "http://:11434"},
"too small port": {":-1", ":11434"}, "too small port": {":-1", "http://:11434"},
"ipv6 localhost": {"[::1]", "[::1]:11434"}, "ipv6 localhost": {"[::1]", "http://[::1]:11434"},
"ipv6 world open": {"[::]", "[::]:11434"}, "ipv6 world open": {"[::]", "http://[::]:11434"},
"ipv6 no brackets": {"::1", "[::1]:11434"}, "ipv6 no brackets": {"::1", "http://[::1]:11434"},
"ipv6 + port": {"[::1]:1337", "[::1]:1337"}, "ipv6 + port": {"[::1]:1337", "http://[::1]:1337"},
"extra space": {" 1.2.3.4 ", "1.2.3.4:11434"}, "extra space": {" 1.2.3.4 ", "http://1.2.3.4:11434"},
"extra quotes": {"\"1.2.3.4\"", "1.2.3.4:11434"}, "extra quotes": {"\"1.2.3.4\"", "http://1.2.3.4:11434"},
"extra space+quotes": {" \" 1.2.3.4 \" ", "1.2.3.4:11434"}, "extra space+quotes": {" \" 1.2.3.4 \" ", "http://1.2.3.4:11434"},
"extra single quotes": {"'1.2.3.4'", "1.2.3.4:11434"}, "extra single quotes": {"'1.2.3.4'", "http://1.2.3.4:11434"},
"http": {"http://1.2.3.4", "1.2.3.4:80"}, "http": {"http://1.2.3.4", "http://1.2.3.4:80"},
"http port": {"http://1.2.3.4:4321", "1.2.3.4:4321"}, "http port": {"http://1.2.3.4:4321", "http://1.2.3.4:4321"},
"https": {"https://1.2.3.4", "1.2.3.4:443"}, "https": {"https://1.2.3.4", "https://1.2.3.4:443"},
"https port": {"https://1.2.3.4:4321", "1.2.3.4:4321"}, "https port": {"https://1.2.3.4:4321", "https://1.2.3.4:4321"},
"proxy path": {"https://example.com/ollama", "https://example.com:443/ollama"},
} }
for name, tt := range cases { for name, tt := range cases {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Setenv("OLLAMA_HOST", tt.value) t.Setenv("OLLAMA_HOST", tt.value)
if host := Host(); host.Host != tt.expect { if host := Host(); host.String() != tt.expect {
t.Errorf("%s: expected %s, got %s", name, tt.expect, host.Host) t.Errorf("%s: expected %s, got %s", name, tt.expect, host.String())
} }
}) })
} }