move ssl parts to ssl.go
This commit is contained in:
@@ -132,7 +132,7 @@ type NotifyGroup struct {
|
||||
|
||||
// SSLGroup defines options group for server ssl params
|
||||
type SSLGroup struct {
|
||||
Mode string `long:"mode" env:"MODE" description:"ssl (auto)support" choice:"none" choice:"static" choice:"auto" default:"none"`
|
||||
Type string `long:"type" env:"TYPE" description:"ssl (auto)support" choice:"none" choice:"static" choice:"auto" default:"none"`
|
||||
Port int `long:"port" env:"PORT" description:"port number for https server" default:"8443"`
|
||||
Cert string `long:"cert" env:"CERT" description:"path to cert.pem file"`
|
||||
Key string `long:"key" env:"KEY" description:"path to key.pem file"`
|
||||
@@ -152,7 +152,7 @@ type serverApp struct {
|
||||
|
||||
// Execute is the entry point for "server" command, called by flag parser
|
||||
func (s *ServerCommand) Execute(args []string) error {
|
||||
log.Printf("[INFO] start rest server in '%s' mode", s.SSL.Mode)
|
||||
log.Printf("[INFO] start server on port %d", s.Port)
|
||||
resetEnv("SECRET", "AUTH_GOOGLE_CSEC", "AUTH_GITHUB_CSEC", "AUTH_FACEBOOK_CSEC", "AUTH_YANDEX_CSEC")
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
@@ -270,7 +270,7 @@ func (s *ServerCommand) newServerApp() (*serverApp, error) {
|
||||
},
|
||||
Cache: loadingCache,
|
||||
NotifyService: notifyService,
|
||||
SSLConfig: sslConfig,
|
||||
SSLConfig: sslConfig,
|
||||
}
|
||||
|
||||
srv.ScoreThresholds.Low, srv.ScoreThresholds.Critical = s.LowScore, s.CriticalScore
|
||||
@@ -495,7 +495,7 @@ func (s *ServerCommand) makeNotify(dataStore *service.DataStore) (*notify.Servic
|
||||
}
|
||||
|
||||
func (s *ServerCommand) makeSSLConfig() (group api.SSLConfig, err error) {
|
||||
switch s.SSL.Mode {
|
||||
switch s.SSL.Type {
|
||||
case "none":
|
||||
group.SSLMode = api.None
|
||||
case "static":
|
||||
|
||||
@@ -50,8 +50,8 @@ func TestMain(t *testing.T) {
|
||||
|
||||
func TestMain_SSLStaticMode(t *testing.T) {
|
||||
os.Args = []string{"test", "server", "--secret=123456", "--store.bolt.path=/tmp/xyz", "--backup=/tmp",
|
||||
"--avatar.fs.path=/tmp", "--port=18080", "--url=https://localhost", "--dbg",
|
||||
"--ssl.mode=static", "--ssl.cert=testdata/cert.pem", "--ssl.key=testdata/key.pem", "--ssl.port=18443"}
|
||||
"--avatar.fs.path=/tmp", "--port=18080", "--url=https://localhost:18443", "--dbg",
|
||||
"--ssl.type=static", "--ssl.cert=testdata/cert.pem", "--ssl.key=testdata/key.pem", "--ssl.port=18443"}
|
||||
|
||||
go func() {
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
|
||||
@@ -31,28 +31,6 @@ import (
|
||||
"github.com/umputun/remark/backend/app/store/service"
|
||||
)
|
||||
|
||||
// sslMode defines rest server mode (http or https)
|
||||
type sslMode int8
|
||||
|
||||
const (
|
||||
// None defines to run http server only
|
||||
None sslMode = iota
|
||||
|
||||
// Static defines to run both https and http server. Redirect http to https
|
||||
Static
|
||||
|
||||
// Auto defines to run both https and http server. Redirect http to https. Https server with autocert support
|
||||
Auto
|
||||
)
|
||||
|
||||
// SSLConfig holds all params for ssl server mode
|
||||
type SSLConfig struct {
|
||||
SSLMode sslMode
|
||||
Cert string
|
||||
Key string
|
||||
Port int
|
||||
}
|
||||
|
||||
// Rest is a rest access server
|
||||
type Rest struct {
|
||||
Version string
|
||||
@@ -160,21 +138,6 @@ func (s *Rest) makeHTTPServer(port int, router chi.Router) *http.Server {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Rest) httpToHTTPSRouter() chi.Router {
|
||||
router := chi.NewRouter()
|
||||
router.Use(middleware.RealIP, Recoverer)
|
||||
router.Use(middleware.Throttle(1000), middleware.Timeout(60*time.Second))
|
||||
|
||||
router.HandleFunc("/*", func(w http.ResponseWriter, r *http.Request) {
|
||||
newURL := fmt.Sprintf("%s:%d", s.RemarkURL, s.SSLConfig.Port) + r.URL.Path
|
||||
if r.URL.RawQuery != "" {
|
||||
newURL += "?" + r.URL.RawQuery
|
||||
}
|
||||
http.Redirect(w, r, newURL, http.StatusTemporaryRedirect)
|
||||
})
|
||||
return router
|
||||
}
|
||||
|
||||
func (s *Rest) routes() chi.Router {
|
||||
router := chi.NewRouter()
|
||||
router.Use(middleware.RealIP, Recoverer)
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
)
|
||||
|
||||
// sslMode defines ssl mode for rest server
|
||||
type sslMode int8
|
||||
|
||||
const (
|
||||
// None defines to run http server only
|
||||
None sslMode = iota
|
||||
|
||||
// Static defines to run both https and http server. Redirect http to https
|
||||
Static
|
||||
|
||||
// Auto defines to run both https and http server. Redirect http to https. Https server with autocert support
|
||||
Auto
|
||||
)
|
||||
|
||||
// SSLConfig holds all ssl params for rest server
|
||||
type SSLConfig struct {
|
||||
SSLMode sslMode
|
||||
Cert string
|
||||
Key string
|
||||
Port int
|
||||
}
|
||||
|
||||
// httpToHTTPSRouter creates new router which does redirect from http to https server
|
||||
func (s *Rest) httpToHTTPSRouter() chi.Router {
|
||||
router := chi.NewRouter()
|
||||
router.Use(middleware.RealIP, Recoverer)
|
||||
router.Use(middleware.Throttle(1000), middleware.Timeout(60*time.Second))
|
||||
|
||||
router.HandleFunc("/*", func(w http.ResponseWriter, r *http.Request) {
|
||||
newURL := s.RemarkURL + r.URL.Path
|
||||
if r.URL.RawQuery != "" {
|
||||
newURL += "?" + r.URL.RawQuery
|
||||
}
|
||||
http.Redirect(w, r, newURL, http.StatusTemporaryRedirect)
|
||||
})
|
||||
return router
|
||||
}
|
||||
Reference in New Issue
Block a user