-
Notifications
You must be signed in to change notification settings - Fork 612
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #254 from k8sgpt-ai/feat/serve
feat: http server
- Loading branch information
Showing
10 changed files
with
239 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package serve | ||
|
||
import ( | ||
"fmt" | ||
"github.com/fatih/color" | ||
k8sgptserver "github.com/k8sgpt-ai/k8sgpt/pkg/server" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"os" | ||
) | ||
|
||
var ( | ||
port string | ||
backend string | ||
token string | ||
) | ||
|
||
var ServeCmd = &cobra.Command{ | ||
Use: "serve", | ||
Short: "Runs k8sgpt as a server", | ||
Long: `Runs k8sgpt as a server to allow for easy integration with other applications.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
backendType := viper.GetString("backend_type") | ||
if backendType == "" { | ||
color.Red("No backend set. Please run k8sgpt auth") | ||
os.Exit(1) | ||
} | ||
|
||
if backend != "" { | ||
backendType = backend | ||
} | ||
|
||
token := viper.GetString(fmt.Sprintf("%s_key", backendType)) | ||
// check if nil | ||
if token == "" { | ||
color.Red("No %s key set. Please run k8sgpt auth", backendType) | ||
os.Exit(1) | ||
} | ||
|
||
server := k8sgptserver.Config{ | ||
Backend: backend, | ||
Port: port, | ||
Token: token, | ||
} | ||
|
||
err := server.Serve() | ||
if err != nil { | ||
color.Red("Error: %v", err) | ||
os.Exit(1) | ||
} | ||
// override the default backend if a flag is provided | ||
}, | ||
} | ||
|
||
func init() { | ||
// add flag for backend | ||
ServeCmd.Flags().StringVarP(&port, "port", "p", "8080", "Port to run the server on") | ||
ServeCmd.Flags().StringVarP(&backend, "backend", "b", "openai", "Backend AI provider") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.