MCPcopy
hub / github.com/writefreely/writefreely / Serve

Function Serve

app.go:448–567  ·  view source on GitHub ↗
(app *App, r *mux.Router)

Source from the content-addressed store, hash-verified

446}
447
448func Serve(app *App, r *mux.Router) {
449 log.Info("Going to serve...")
450
451 isSingleUser = app.cfg.App.SingleUser
452 app.cfg.Server.Dev = debugging
453
454 // Handle shutdown
455 c := make(chan os.Signal, 2)
456 signal.Notify(c, os.Interrupt, syscall.SIGTERM)
457 go func() {
458 <-c
459 log.Info("Shutting down...")
460 shutdown(app)
461 log.Info("Done.")
462 os.Exit(0)
463 }()
464
465 // Start gopher server
466 if app.cfg.Server.GopherPort > 0 && !app.cfg.App.Private {
467 go initGopher(app)
468 }
469
470 // Start web application server
471 var bindAddress = app.cfg.Server.Bind
472 if bindAddress == "" {
473 bindAddress = "localhost"
474 }
475 var err error
476 if app.cfg.IsSecureStandalone() {
477 if app.cfg.Server.Autocert {
478 m := &autocert.Manager{
479 Prompt: autocert.AcceptTOS,
480 Cache: autocert.DirCache(app.cfg.Server.TLSCertPath),
481 }
482 host, err := url.Parse(app.cfg.App.Host)
483 if err != nil {
484 log.Error("[WARNING] Unable to parse configured host! %s", err)
485 log.Error(`[WARNING] ALL hosts are allowed, which can open you to an attack where
486clients connect to a server by IP address and pretend to be asking for an
487incorrect host name, and cause you to reach the CA's rate limit for certificate
488requests. We recommend supplying a valid host name.`)
489 log.Info("Using autocert on ANY host")
490 } else {
491 log.Info("Using autocert on host %s", host.Host)
492 m.HostPolicy = autocert.HostWhitelist(host.Host)
493 }
494 s := &http.Server{
495 Addr: ":https",
496 Handler: r,
497 TLSConfig: &tls.Config{
498 GetCertificate: m.GetCertificate,
499 },
500 }
501 s.SetKeepAlivesEnabled(false)
502
503 go func() {
504 log.Info("Serving redirects on http://%s:80", bindAddress)
505 err = http.ListenAndServe(":80", m.HTTPHandler(nil))

Callers 2

serveActionFunction · 0.92
legacyActionsFunction · 0.92

Calls 5

shutdownFunction · 0.85
initGopherFunction · 0.85
IsSecureStandaloneMethod · 0.80
RedirectMethod · 0.80
CloseMethod · 0.80

Tested by

no test coverage detected