-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
80 lines (66 loc) · 2.15 KB
/
main.go
File metadata and controls
80 lines (66 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"log"
"net/http"
"os"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"github.com/matzew/mcp-launcher/catalog"
"github.com/matzew/mcp-launcher/handlers"
)
func main() {
catalogNamespace := envOr("CATALOG_NAMESPACE", "mcp-catalog")
targetNamespace := envOr("TARGET_NAMESPACE", "default")
listenAddr := envOr("LISTEN_ADDR", ":8080")
config, err := buildConfig()
if err != nil {
log.Fatalf("Failed to build kubeconfig: %v", err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatalf("Failed to create kubernetes client: %v", err)
}
dynClient, err := dynamic.NewForConfig(config)
if err != nil {
log.Fatalf("Failed to create dynamic client: %v", err)
}
gwCfg := handlers.GatewayConfig{
Enabled: envOr("MCP_GATEWAY_ENABLED", "true") != "false",
GatewayName: envOr("MCP_GATEWAY_NAME", "mcp-gateway"),
GatewayNamespace: envOr("MCP_GATEWAY_NAMESPACE", "gateway-system"),
}
store := catalog.NewStore(clientset, catalogNamespace)
h := handlers.New(store, clientset, dynClient, targetNamespace, gwCfg)
mux := http.NewServeMux()
mux.HandleFunc("GET /", h.Catalog)
mux.HandleFunc("GET /configure/{name}", h.Configure)
mux.HandleFunc("GET /preview/{name}", h.Preview)
mux.HandleFunc("POST /run", h.Run)
mux.HandleFunc("POST /deploy/{name}", h.QuickDeploy)
mux.HandleFunc("GET /running", h.Running)
mux.HandleFunc("DELETE /server/{namespace}/{name}", h.Delete)
log.Printf("MCP Launcher listening on %s", listenAddr)
log.Printf("Catalog namespace: %s, Target namespace: %s", catalogNamespace, targetNamespace)
if err := http.ListenAndServe(listenAddr, mux); err != nil {
log.Fatalf("Server failed: %v", err)
}
}
func buildConfig() (*rest.Config, error) {
config, err := rest.InClusterConfig()
if err == nil {
return config, nil
}
kubeconfig := os.Getenv("KUBECONFIG")
if kubeconfig == "" {
kubeconfig = os.Getenv("HOME") + "/.kube/config"
}
return clientcmd.BuildConfigFromFlags("", kubeconfig)
}
func envOr(key, defaultVal string) string {
if v := os.Getenv(key); v != "" {
return v
}
return defaultVal
}