mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2026-01-30 05:58:50 +00:00
79 lines
1.6 KiB
Go
79 lines
1.6 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/fatih/color"
|
|
"github.com/k8sgpt-ai/k8sgpt/pkg/analysis"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type Config struct {
|
|
Port string
|
|
Backend string
|
|
Key string
|
|
Token string
|
|
Output string
|
|
}
|
|
|
|
type Result struct {
|
|
Analysis []analysis.Analysis `json:"analysis"`
|
|
}
|
|
|
|
func (s *Config) analyzeHandler(w http.ResponseWriter, r *http.Request) {
|
|
namespace := r.URL.Query().Get("namespace")
|
|
explain := getBoolParam(r.URL.Query().Get("explain"))
|
|
anonymize := getBoolParam(r.URL.Query().Get("anonymize"))
|
|
nocache := getBoolParam(r.URL.Query().Get("nocache"))
|
|
language := r.URL.Query().Get("language")
|
|
|
|
config, err := analysis.NewAnalysis(s.Backend, language, []string{}, namespace, nocache, explain)
|
|
if err != nil {
|
|
fmt.Fprintf(w, err.Error())
|
|
}
|
|
|
|
err = config.RunAnalysis()
|
|
if err != nil {
|
|
color.Red("Error: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
if explain {
|
|
err := config.GetAIResults(s.Output, anonymize)
|
|
if err != nil {
|
|
color.Red("Error: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
output, err := config.JsonOutput()
|
|
if err != nil {
|
|
color.Red("Error: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Fprintf(w, string(output))
|
|
|
|
}
|
|
|
|
func (s *Config) Serve() error {
|
|
http.HandleFunc("/analyze", s.analyzeHandler)
|
|
color.Green("Starting server on port %d", s.Port)
|
|
err := http.ListenAndServe(":"+s.Port, nil)
|
|
if err != nil {
|
|
fmt.Printf("error starting server: %s\n", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func getBoolParam(param string) bool {
|
|
b, err := strconv.ParseBool(strings.ToLower(param))
|
|
if err != nil {
|
|
// Handle error if conversion fails
|
|
return false
|
|
}
|
|
return b
|
|
}
|