2017-11-20 06:10:04 +01:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2017-11-22 20:16:48 +01:00
|
|
|
"crypto/tls"
|
2017-11-20 06:10:04 +01:00
|
|
|
"net/http"
|
|
|
|
"time"
|
2017-11-22 04:37:47 +01:00
|
|
|
|
2017-11-22 20:16:48 +01:00
|
|
|
"github.com/gorilla/mux"
|
2017-12-16 03:55:57 +01:00
|
|
|
"github.com/miniflux/miniflux/logger"
|
2017-12-13 06:48:13 +01:00
|
|
|
"github.com/miniflux/miniflux/scheduler"
|
2017-11-22 22:11:01 +01:00
|
|
|
"golang.org/x/crypto/acme/autocert"
|
2017-11-22 07:36:00 +01:00
|
|
|
|
2017-12-13 06:48:13 +01:00
|
|
|
"github.com/miniflux/miniflux/config"
|
|
|
|
"github.com/miniflux/miniflux/reader/feed"
|
|
|
|
"github.com/miniflux/miniflux/storage"
|
2017-11-20 06:10:04 +01:00
|
|
|
)
|
|
|
|
|
2017-11-22 04:37:47 +01:00
|
|
|
// NewServer returns a new HTTP server.
|
2017-11-22 07:36:00 +01:00
|
|
|
func NewServer(cfg *config.Config, store *storage.Storage, pool *scheduler.WorkerPool, feedHandler *feed.Handler) *http.Server {
|
2017-11-22 20:16:48 +01:00
|
|
|
return startServer(cfg, getRoutes(cfg, store, feedHandler, pool))
|
|
|
|
}
|
|
|
|
|
|
|
|
func startServer(cfg *config.Config, handler *mux.Router) *http.Server {
|
|
|
|
certFile := cfg.Get("CERT_FILE", config.DefaultCertFile)
|
|
|
|
keyFile := cfg.Get("KEY_FILE", config.DefaultKeyFile)
|
2017-11-22 22:11:01 +01:00
|
|
|
certDomain := cfg.Get("CERT_DOMAIN", config.DefaultCertDomain)
|
|
|
|
certCache := cfg.Get("CERT_CACHE", config.DefaultCertCache)
|
2017-11-20 06:10:04 +01:00
|
|
|
server := &http.Server{
|
|
|
|
ReadTimeout: 5 * time.Second,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
IdleTimeout: 60 * time.Second,
|
2017-11-22 05:20:20 +01:00
|
|
|
Addr: cfg.Get("LISTEN_ADDR", config.DefaultListenAddr),
|
2017-11-22 20:16:48 +01:00
|
|
|
Handler: handler,
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-22 22:11:01 +01:00
|
|
|
if certDomain != "" && certCache != "" {
|
|
|
|
server.Addr = ":https"
|
|
|
|
certManager := autocert.Manager{
|
|
|
|
Cache: autocert.DirCache(certCache),
|
|
|
|
Prompt: autocert.AcceptTOS,
|
|
|
|
HostPolicy: autocert.HostWhitelist(certDomain),
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
2017-11-22 20:16:48 +01:00
|
|
|
|
2017-11-22 22:11:01 +01:00
|
|
|
go func() {
|
2017-12-16 03:55:57 +01:00
|
|
|
logger.Info(`Listening on "%s" by using auto-configured certificate for "%s"`, server.Addr, certDomain)
|
|
|
|
logger.Fatal(server.Serve(certManager.Listener()).Error())
|
2017-11-22 22:11:01 +01:00
|
|
|
}()
|
|
|
|
} else if certFile != "" && keyFile != "" {
|
|
|
|
server.TLSConfig = &tls.Config{MinVersion: tls.VersionTLS12}
|
|
|
|
|
2017-11-22 20:16:48 +01:00
|
|
|
go func() {
|
2017-12-16 03:55:57 +01:00
|
|
|
logger.Info(`Listening on "%s" by using certificate "%s" and key "%s"`, server.Addr, certFile, keyFile)
|
|
|
|
logger.Fatal(server.ListenAndServeTLS(certFile, keyFile).Error())
|
2017-11-22 20:16:48 +01:00
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
go func() {
|
2017-12-16 03:55:57 +01:00
|
|
|
logger.Info(`Listening on "%s" without TLS`, server.Addr)
|
|
|
|
logger.Fatal(server.ListenAndServe().Error())
|
2017-11-22 20:16:48 +01:00
|
|
|
}()
|
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
return server
|
|
|
|
}
|