server.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package daemon
  5. import (
  6. "crypto/tls"
  7. "net/http"
  8. "time"
  9. "github.com/miniflux/miniflux/config"
  10. "github.com/miniflux/miniflux/logger"
  11. "github.com/miniflux/miniflux/reader/feed"
  12. "github.com/miniflux/miniflux/scheduler"
  13. "github.com/miniflux/miniflux/storage"
  14. "golang.org/x/crypto/acme/autocert"
  15. )
  16. func newServer(cfg *config.Config, store *storage.Storage, pool *scheduler.WorkerPool, feedHandler *feed.Handler) *http.Server {
  17. certFile := cfg.CertFile()
  18. keyFile := cfg.KeyFile()
  19. certDomain := cfg.CertDomain()
  20. certCache := cfg.CertCache()
  21. server := &http.Server{
  22. ReadTimeout: 5 * time.Second,
  23. WriteTimeout: 10 * time.Second,
  24. IdleTimeout: 60 * time.Second,
  25. Addr: cfg.ListenAddr(),
  26. Handler: routes(cfg, store, feedHandler, pool),
  27. }
  28. if certDomain != "" && certCache != "" {
  29. cfg.IsHTTPS = true
  30. server.Addr = ":https"
  31. certManager := autocert.Manager{
  32. Cache: autocert.DirCache(certCache),
  33. Prompt: autocert.AcceptTOS,
  34. HostPolicy: autocert.HostWhitelist(certDomain),
  35. }
  36. go func() {
  37. logger.Info(`Listening on "%s" by using auto-configured certificate for "%s"`, server.Addr, certDomain)
  38. logger.Fatal(server.Serve(certManager.Listener()).Error())
  39. }()
  40. } else if certFile != "" && keyFile != "" {
  41. server.TLSConfig = &tls.Config{MinVersion: tls.VersionTLS12}
  42. cfg.IsHTTPS = true
  43. go func() {
  44. logger.Info(`Listening on "%s" by using certificate "%s" and key "%s"`, server.Addr, certFile, keyFile)
  45. logger.Fatal(server.ListenAndServeTLS(certFile, keyFile).Error())
  46. }()
  47. } else {
  48. go func() {
  49. logger.Info(`Listening on "%s" without TLS`, server.Addr)
  50. logger.Fatal(server.ListenAndServe().Error())
  51. }()
  52. }
  53. return server
  54. }