MCPcopy Index your code
hub / github.com/coder/coder / main

Function main

scripts/telemetry-server/main.go:25–76  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

23)
24
25func main() {
26 port := flag.String("port", "8081", "Port to listen on")
27 flag.Parse()
28
29 enc := json.NewEncoder(os.Stdout)
30
31 mux := http.NewServeMux()
32
33 handleTelemetry := func(telemetryType string) http.HandlerFunc {
34 return func(w http.ResponseWriter, r *http.Request) {
35 body, err := io.ReadAll(r.Body)
36 if err != nil {
37 http.Error(w, err.Error(), http.StatusBadRequest)
38 return
39 }
40
41 output := map[string]any{
42 "type": telemetryType,
43 "version": r.Header.Get("X-Telemetry-Version"),
44 "data": json.RawMessage(body),
45 }
46 if err := enc.Encode(output); err != nil {
47 _, _ = fmt.Fprintf(os.Stderr, "Error encoding telemetry output: %v\n", err)
48 }
49
50 w.WriteHeader(http.StatusAccepted)
51 }
52 }
53
54 mux.HandleFunc("POST /snapshot", handleTelemetry("snapshot"))
55 mux.HandleFunc("POST /deployment", handleTelemetry("deployment"))
56
57 addr := net.JoinHostPort("127.0.0.1", *port)
58 server := &http.Server{
59 Addr: addr,
60 Handler: mux,
61 ReadHeaderTimeout: 10 * time.Second,
62 }
63
64 go func() {
65 c := make(chan os.Signal, 1)
66 signal.Notify(c, os.Interrupt)
67 <-c
68 _ = server.Close()
69 }()
70
71 _, _ = fmt.Fprintf(os.Stdout, "Mock telemetry server listening on %s\n", addr)
72 if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
73 _, _ = fmt.Fprintf(os.Stderr, "Error: %v\n", err)
74 os.Exit(1)
75 }
76}

Callers

nothing calls this directly

Calls 11

CloseMethod · 0.95
EncodeMethod · 0.80
ExitMethod · 0.80
ParseMethod · 0.65
GetMethod · 0.65
StringMethod · 0.45
ReadAllMethod · 0.45
ErrorMethod · 0.45
WriteHeaderMethod · 0.45
NotifyMethod · 0.45
IsMethod · 0.45

Tested by

no test coverage detected