MCPcopy
hub / github.com/cortesi/devd / Serve

Method Serve

server.go:320–374  ·  view source on GitHub ↗

Serve starts the devd server. The callback is called with the serving URL just before service starts.

(address string, port int, certFile string, logger termlog.TermLog, callback func(string))

Source from the content-addressed store, hash-verified

318// Serve starts the devd server. The callback is called with the serving URL
319// just before service starts.
320func (dd *Devd) Serve(address string, port int, certFile string, logger termlog.TermLog, callback func(string)) error {
321 templates, err := ricetemp.MakeTemplates(rice.MustFindBox("templates"))
322 if err != nil {
323 return fmt.Errorf("Error loading templates: %s", err)
324 }
325 mux, err := dd.Router(logger, templates)
326 if err != nil {
327 return err
328 }
329 var tlsConfig *tls.Config
330 var tlsEnabled bool
331 if certFile != "" {
332 tlsConfig, err = getTLSConfig(certFile)
333 if err != nil {
334 return fmt.Errorf("Could not load certs: %s", err)
335 }
336 tlsEnabled = true
337 }
338
339 var hl net.Listener
340 if port > 0 {
341 hl, err = net.Listen("tcp", fmt.Sprintf("%v:%d", address, port))
342 } else {
343 hl, err = pickPort(address, portLow, portHigh, tlsEnabled)
344 }
345 if err != nil {
346 return err
347 }
348
349 if tlsConfig != nil {
350 hl = tls.NewListener(hl, tlsConfig)
351 }
352
353 hl = slowdown.NewSlowListener(hl, dd.UpKbps*1024, dd.DownKbps*1024)
354 url := formatURL(tlsEnabled, address, hl.Addr().(*net.TCPAddr).Port)
355 logger.Say("Listening on %s (%s)", url, hl.Addr().String())
356 server := &http.Server{Addr: hl.Addr().String(), Handler: mux}
357 callback(url)
358
359 if dd.HasLivereload() {
360 c := make(chan os.Signal, 1)
361 signal.Notify(c, syscall.SIGHUP)
362 go func() {
363 for {
364 <-c
365 logger.Say("Received signal - reloading")
366 dd.lrserver.Reload([]string{"*"})
367 }
368 }()
369 }
370
371 err = server.Serve(hl)
372 logger.Shout("Server stopped: %v", err)
373 return nil
374}

Callers 1

mainFunction · 0.95

Calls 8

RouterMethod · 0.95
HasLivereloadMethod · 0.95
getTLSConfigFunction · 0.85
pickPortFunction · 0.85
formatURLFunction · 0.85
AddrMethod · 0.80
StringMethod · 0.65
ReloadMethod · 0.65

Tested by

no test coverage detected