MCPcopy Create free account
hub / github.com/driangle/taskmd / Start

Method Start

apps/cli/internal/web/server.go:75–118  ·  view source on GitHub ↗

Start starts the HTTP server. It blocks until ctx is cancelled.

(ctx context.Context)

Source from the content-addressed store, hash-verified

73
74// Start starts the HTTP server. It blocks until ctx is cancelled.
75func (s *Server) Start(ctx context.Context) error {
76 mux := http.NewServeMux()
77 s.registerRoutes(mux)
78 s.mountStatic(mux)
79
80 var handler http.Handler = mux
81 handler = projectMiddleware(s.resolver, handler)
82 if s.config.Dev {
83 handler = corsMiddleware(handler)
84 }
85
86 srv := &http.Server{
87 Addr: fmt.Sprintf(":%d", s.config.Port),
88 Handler: handler,
89 }
90
91 // Start file watcher in background
92 go func() {
93 if err := s.watcher.Start(); err != nil && s.config.Verbose {
94 fmt.Printf("watcher error: %v\n", err)
95 }
96 }()
97
98 // Graceful shutdown
99 go func() {
100 <-ctx.Done()
101 s.watcher.Stop()
102 shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
103 defer cancel()
104 srv.Shutdown(shutdownCtx)
105 }()
106
107 listener, err := net.Listen("tcp", srv.Addr)
108 if err != nil {
109 return fmt.Errorf("failed to listen on port %d: %w", s.config.Port, err)
110 }
111
112 s.printBanner()
113
114 if err := srv.Serve(listener); err != http.ErrServerClosed {
115 return err
116 }
117 return nil
118}
119
120func (s *Server) registerRoutes(mux *http.ServeMux) {
121 mux.HandleFunc("GET /api/projects", handleProjects(s.config.ListProjects))

Callers 1

runWebStartFunction · 0.95

Calls 6

registerRoutesMethod · 0.95
mountStaticMethod · 0.95
printBannerMethod · 0.95
projectMiddlewareFunction · 0.85
corsMiddlewareFunction · 0.85
StopMethod · 0.80

Tested by

no test coverage detected