From 0e84c61c96caca420e8cb64de42c827f0b6c0f26 Mon Sep 17 00:00:00 2001 From: "a.kosourov" Date: Sun, 4 Nov 2018 16:15:45 +0300 Subject: [PATCH] move ssl parts to ssl.go --- backend/app/cmd/server.go | 8 +++--- backend/app/main_test.go | 4 +-- backend/app/rest/api/rest.go | 37 ---------------------------- backend/app/rest/api/ssl.go | 47 ++++++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 43 deletions(-) create mode 100644 backend/app/rest/api/ssl.go diff --git a/backend/app/cmd/server.go b/backend/app/cmd/server.go index d150192d..6ebf7ea0 100644 --- a/backend/app/cmd/server.go +++ b/backend/app/cmd/server.go @@ -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": diff --git a/backend/app/main_test.go b/backend/app/main_test.go index 2d979b75..4eb6e344 100644 --- a/backend/app/main_test.go +++ b/backend/app/main_test.go @@ -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) diff --git a/backend/app/rest/api/rest.go b/backend/app/rest/api/rest.go index 2d377156..1e087e19 100644 --- a/backend/app/rest/api/rest.go +++ b/backend/app/rest/api/rest.go @@ -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) diff --git a/backend/app/rest/api/ssl.go b/backend/app/rest/api/ssl.go new file mode 100644 index 00000000..36c15b52 --- /dev/null +++ b/backend/app/rest/api/ssl.go @@ -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 +}