Skip to content

Commit

Permalink
fix: optimize api server-configs
Browse files Browse the repository at this point in the history
  • Loading branch information
ruquanzhao committed Dec 30, 2024
1 parent 04f6467 commit 9665532
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions cmd/karpor/app/options/core_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type CoreOptions struct {
ReadOnlyMode bool
GithubBadge bool
Version bool
EnableAI bool
}

func NewCoreOptions() *CoreOptions {
Expand Down
10 changes: 9 additions & 1 deletion cmd/karpor/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ func NewServerCommand(ctx context.Context) *cobra.Command {
return o.SearchStorageOptions
}))
expvar.Publish("AIOptions", expvar.Func(func() interface{} {
return o.AIOptions
displayOpts := *o.AIOptions
displayOpts.AIAuthToken = "[hidden]"
return &displayOpts
}))
expvar.Publish("Version", expvar.Func(func() interface{} {
return version.GetVersion()
Expand Down Expand Up @@ -191,6 +193,12 @@ func (o *Options) Complete() error {
if err != nil {
return fmt.Errorf("create token generator failed: %w", err)
}

// set enableAI true if AIBaseURL is not empty
if o.AIOptions.AIBaseURL != "" {
o.CoreOptions.EnableAI = true
}

return nil
}

Expand Down
43 changes: 42 additions & 1 deletion pkg/core/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
package route

import (
"encoding/json"
"errors"
"expvar"
"fmt"
"net/http"
"strings"

docs "github.com/KusionStack/karpor/api/openapispec"
aggregatorhandler "github.com/KusionStack/karpor/pkg/core/handler/aggregator"
Expand Down Expand Up @@ -125,7 +129,7 @@ func NewCoreRoute(
router.Get("/endpoints", endpointhandler.Endpoints(router))

// Expose server configuration and runtime statistics.
router.Get("/server-configs", expvar.Handler().ServeHTTP)
router.Get("/server-configs", customExpvarHandler().ServeHTTP)

healthhandler.Register(router, generalStorage)
return router, nil
Expand Down Expand Up @@ -188,3 +192,40 @@ func setupRestAPIV1(
r.Get("/resource-groups/{resourceGroupRuleName}", resourcegrouphandler.List(resourceGroupMgr))
r.Get("/authn", authnhandler.Get())
}

func customExpvarHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")

first := true
w.Write([]byte("{\n"))
expvar.Do(func(kv expvar.KeyValue) {
if !first {
w.Write([]byte(",\n"))
}
first = false

if kv.Key == "cmdline" {
handleCmdline(w, kv.Value)
return
}

fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
})
w.Write([]byte("\n}\n"))
})
}

func handleCmdline(w http.ResponseWriter, v expvar.Var) {
var cmdline []string
json.Unmarshal([]byte(v.String()), &cmdline)

for i, arg := range cmdline {
if strings.Contains(arg, "token=") {
cmdline[i] = strings.Split(arg, "=")[0] + "=<hidden>"
}
}

output, _ := json.Marshal(cmdline)
fmt.Fprintf(w, "%q: %s", "cmdline", output)
}

0 comments on commit 9665532

Please sign in to comment.