Run starts the http server and if configured a https server.
(router http.Handler, conf *config.Configuration)
| 19 | |
| 20 | // Run starts the http server and if configured a https server. |
| 21 | func Run(router http.Handler, conf *config.Configuration) error { |
| 22 | shutdown := make(chan error) |
| 23 | go doShutdownOnSignal(shutdown) |
| 24 | |
| 25 | httpListener, err := startListening("plain connection", conf.Server.ListenAddr, conf.Server.Port, conf.Server.KeepAlivePeriodSeconds) |
| 26 | if err != nil { |
| 27 | return err |
| 28 | } |
| 29 | defer httpListener.Close() |
| 30 | |
| 31 | s := &http.Server{Handler: router} |
| 32 | if conf.Server.SSL.Enabled { |
| 33 | if conf.Server.SSL.LetsEncrypt.Enabled { |
| 34 | applyLetsEncrypt(s, conf) |
| 35 | } else if conf.Server.SSL.CertFile == "" || conf.Server.SSL.CertKey == "" { |
| 36 | log.Fatalln("CertFile and CertKey must be set to use HTTPS when LetsEncrypt is disabled, please set GOTIFY_SERVER_SSL_CERTFILE and GOTIFY_SERVER_SSL_CERTKEY") |
| 37 | } |
| 38 | |
| 39 | httpsListener, err := startListening("TLS connection", conf.Server.SSL.ListenAddr, conf.Server.SSL.Port, conf.Server.KeepAlivePeriodSeconds) |
| 40 | if err != nil { |
| 41 | return err |
| 42 | } |
| 43 | defer httpsListener.Close() |
| 44 | |
| 45 | go func() { |
| 46 | err := s.ServeTLS(httpsListener, conf.Server.SSL.CertFile, conf.Server.SSL.CertKey) |
| 47 | doShutdown(shutdown, err) |
| 48 | }() |
| 49 | } |
| 50 | go func() { |
| 51 | err := s.Serve(httpListener) |
| 52 | doShutdown(shutdown, err) |
| 53 | }() |
| 54 | |
| 55 | err = <-shutdown |
| 56 | fmt.Println("Shutting down:", err) |
| 57 | |
| 58 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 59 | defer cancel() |
| 60 | return s.Shutdown(ctx) |
| 61 | } |
| 62 | |
| 63 | func doShutdownOnSignal(shutdown chan<- error) { |
| 64 | onSignal := make(chan os.Signal, 1) |
no test coverage detected