clean up cli flags

This commit is contained in:
Jeffrey Morgan 2023-08-10 09:27:03 -07:00
parent 5b5cc9c9f1
commit 040a5b9750
3 changed files with 8 additions and 10 deletions

View file

@ -538,7 +538,7 @@ func getRunServerParams(cmd *cobra.Command) (host, port string, extraOrigins []s
if portFlag.Changed || port == "" { if portFlag.Changed || port == "" {
port = portFlag.Value.String() port = portFlag.Value.String()
} }
extraOrigins, err = cmd.Flags().GetStringSlice("allowed-origins") extraOrigins, err = cmd.Flags().GetStringSlice("origins")
if err != nil { if err != nil {
return "", "", nil, err return "", "", nil, err
} }
@ -546,7 +546,7 @@ func getRunServerParams(cmd *cobra.Command) (host, port string, extraOrigins []s
} }
func RunServer(cmd *cobra.Command, _ []string) error { func RunServer(cmd *cobra.Command, _ []string) error {
host, port, extraOrigins, err := getRunServerParams(cmd) host, port, origins, err := getRunServerParams(cmd)
if err != nil { if err != nil {
return err return err
} }
@ -556,7 +556,7 @@ func RunServer(cmd *cobra.Command, _ []string) error {
return err return err
} }
return server.Serve(ln, extraOrigins) return server.Serve(ln, origins)
} }
func startMacApp(client *api.Client) error { func startMacApp(client *api.Client) error {
@ -650,7 +650,7 @@ func NewCLI() *cobra.Command {
serveCmd.Flags().String("port", "11434", "Port to listen on, may also use OLLAMA_PORT environment variable") serveCmd.Flags().String("port", "11434", "Port to listen on, may also use OLLAMA_PORT environment variable")
serveCmd.Flags().String("host", "127.0.0.1", "Host listen address, may also use OLLAMA_HOST environment variable") serveCmd.Flags().String("host", "127.0.0.1", "Host listen address, may also use OLLAMA_HOST environment variable")
serveCmd.Flags().StringSlice("allowed-origins", []string{}, "Additional allowed CORS origins (outside of localhost), specify as comma-separated list") serveCmd.Flags().StringSlice("origins", nil, "Additional allowed CORS origins as comma-separated list")
pullCmd := &cobra.Command{ pullCmd := &cobra.Command{
Use: "pull MODEL", Use: "pull MODEL",

View file

@ -84,7 +84,7 @@ func TestGetRunServerParams(t *testing.T) {
// now set command flags // now set command flags
serveCmd.Flags().Set("host", "localhost") serveCmd.Flags().Set("host", "localhost")
serveCmd.Flags().Set("port", "8888") serveCmd.Flags().Set("port", "8888")
serveCmd.Flags().Set("allowed-origins", "http://foo.example.com,http://192.168.1.1") serveCmd.Flags().Set("origins", "http://foo.example.com,http://192.168.1.1")
host, port, extraOrigins, err := getRunServerParams(serveCmd) host, port, extraOrigins, err := getRunServerParams(serveCmd)
if err != nil { if err != nil {

View file

@ -391,10 +391,10 @@ func CopyModelHandler(c *gin.Context) {
} }
} }
func Serve(ln net.Listener, extraOrigins []string) error { func Serve(ln net.Listener, origins []string) error {
config := cors.DefaultConfig() config := cors.DefaultConfig()
config.AllowWildcard = true config.AllowWildcard = true
allowedOrigins := []string{ config.AllowOrigins = append(origins, []string{
"http://localhost", "http://localhost",
"http://localhost:*", "http://localhost:*",
"https://localhost", "https://localhost",
@ -407,9 +407,7 @@ func Serve(ln net.Listener, extraOrigins []string) error {
"http://0.0.0.0:*", "http://0.0.0.0:*",
"https://0.0.0.0", "https://0.0.0.0",
"https://0.0.0.0:*", "https://0.0.0.0:*",
} }...)
allowedOrigins = append(allowedOrigins, extraOrigins...)
config.AllowOrigins = allowedOrigins
r := gin.Default() r := gin.Default()
r.Use(cors.New(config)) r.Use(cors.New(config))