2018-01-03 01:04:48 -05:00
|
|
|
// Copyright 2018 Frédéric Guillot. All rights reserved.
|
2017-11-20 00:10:04 -05:00
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
package daemon // import "miniflux.app/daemon"
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
import (
|
2017-11-22 14:16:48 -05:00
|
|
|
"crypto/tls"
|
2017-11-20 00:10:04 -05:00
|
|
|
"net/http"
|
|
|
|
"time"
|
2017-11-21 22:37:47 -05:00
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
"miniflux.app/config"
|
|
|
|
"miniflux.app/locale"
|
|
|
|
"miniflux.app/logger"
|
|
|
|
"miniflux.app/reader/feed"
|
|
|
|
"miniflux.app/scheduler"
|
|
|
|
"miniflux.app/storage"
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2018-01-03 01:04:48 -05:00
|
|
|
"golang.org/x/crypto/acme/autocert"
|
|
|
|
)
|
2017-11-22 14:16:48 -05:00
|
|
|
|
2018-02-28 00:08:32 -05:00
|
|
|
func newServer(cfg *config.Config, store *storage.Storage, pool *scheduler.WorkerPool, feedHandler *feed.Handler, translator *locale.Translator) *http.Server {
|
2018-01-15 21:08:30 -05:00
|
|
|
certFile := cfg.CertFile()
|
|
|
|
keyFile := cfg.KeyFile()
|
|
|
|
certDomain := cfg.CertDomain()
|
|
|
|
certCache := cfg.CertCache()
|
2017-11-20 00:10:04 -05:00
|
|
|
server := &http.Server{
|
2018-04-14 16:52:53 -04:00
|
|
|
ReadTimeout: 30 * time.Second,
|
|
|
|
WriteTimeout: 30 * time.Second,
|
2017-11-20 00:10:04 -05:00
|
|
|
IdleTimeout: 60 * time.Second,
|
2018-01-15 21:08:30 -05:00
|
|
|
Addr: cfg.ListenAddr(),
|
2018-02-28 00:08:32 -05:00
|
|
|
Handler: routes(cfg, store, feedHandler, pool, translator),
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-22 16:11:01 -05:00
|
|
|
if certDomain != "" && certCache != "" {
|
2017-12-28 23:34:18 -05:00
|
|
|
cfg.IsHTTPS = true
|
2017-11-22 16:11:01 -05:00
|
|
|
server.Addr = ":https"
|
|
|
|
certManager := autocert.Manager{
|
|
|
|
Cache: autocert.DirCache(certCache),
|
|
|
|
Prompt: autocert.AcceptTOS,
|
|
|
|
HostPolicy: autocert.HostWhitelist(certDomain),
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
2017-11-22 14:16:48 -05:00
|
|
|
|
2018-02-04 21:05:45 -05:00
|
|
|
// Handle http-01 challenge.
|
|
|
|
s := &http.Server{
|
|
|
|
Handler: certManager.HTTPHandler(nil),
|
|
|
|
Addr: ":http",
|
|
|
|
}
|
|
|
|
go s.ListenAndServe()
|
|
|
|
|
2017-11-22 16:11:01 -05:00
|
|
|
go func() {
|
2017-12-15 21:55:57 -05:00
|
|
|
logger.Info(`Listening on "%s" by using auto-configured certificate for "%s"`, server.Addr, certDomain)
|
2018-09-09 00:20:24 -04:00
|
|
|
if err := server.Serve(certManager.Listener()); err != http.ErrServerClosed {
|
|
|
|
logger.Fatal(`Server failed to start: %v`, err)
|
|
|
|
}
|
2017-11-22 16:11:01 -05:00
|
|
|
}()
|
|
|
|
} else if certFile != "" && keyFile != "" {
|
|
|
|
server.TLSConfig = &tls.Config{MinVersion: tls.VersionTLS12}
|
2017-12-28 23:34:18 -05:00
|
|
|
cfg.IsHTTPS = true
|
2017-11-22 16:11:01 -05:00
|
|
|
|
2017-11-22 14:16:48 -05:00
|
|
|
go func() {
|
2017-12-15 21:55:57 -05:00
|
|
|
logger.Info(`Listening on "%s" by using certificate "%s" and key "%s"`, server.Addr, certFile, keyFile)
|
2018-09-09 00:20:24 -04:00
|
|
|
if err := server.ListenAndServeTLS(certFile, keyFile); err != http.ErrServerClosed {
|
|
|
|
logger.Fatal(`Server failed to start: %v`, err)
|
|
|
|
}
|
2017-11-22 14:16:48 -05:00
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
go func() {
|
2017-12-15 21:55:57 -05:00
|
|
|
logger.Info(`Listening on "%s" without TLS`, server.Addr)
|
2018-09-09 00:20:24 -04:00
|
|
|
if err := server.ListenAndServe(); err != http.ErrServerClosed {
|
|
|
|
logger.Fatal(`Server failed to start: %v`, err)
|
|
|
|
}
|
2017-11-22 14:16:48 -05:00
|
|
|
}()
|
|
|
|
}
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
return server
|
|
|
|
}
|