From d52a5bdf48cb43f7f349b439db14db515d4a514b Mon Sep 17 00:00:00 2001 From: Sung <8265228+sungwoncho@users.noreply.github.com> Date: Sat, 4 Mar 2023 10:52:35 +1100 Subject: [PATCH] Use OnPremises (#631) --- SELF_HOSTING.md | 6 +++--- host/docker/docker-compose.yml | 2 +- pkg/server/app/email.go | 2 +- pkg/server/app/email_test.go | 10 +++++----- pkg/server/app/testutils.go | 6 +++--- pkg/server/app/users.go | 2 +- pkg/server/app/users_test.go | 10 +++++----- pkg/server/config/config.go | 20 +++++++++++++++----- pkg/server/controllers/users_test.go | 12 ++++++------ 9 files changed, 40 insertions(+), 30 deletions(-) diff --git a/SELF_HOSTING.md b/SELF_HOSTING.md index c60c06b8..9edc82e1 100644 --- a/SELF_HOSTING.md +++ b/SELF_HOSTING.md @@ -22,7 +22,7 @@ mv ./dnote-server /usr/local/bin ```bash GO_ENV=PRODUCTION \ -OnPremise=true \ +OnPremises=true \ DBHost=localhost \ DBPort=5432 \ DBName=dnote \ @@ -33,7 +33,7 @@ SmtpHost=$SmtpHost \ SmtpPort=$SmtpPort \ SmtpUsername=$SmtpUsername \ SmtpPassword=$SmtpPassword \ -DisableRegistration=false +DisableRegistration=false \ dnote-server start ``` @@ -129,7 +129,7 @@ RestartSec=3 WorkingDirectory=/home/$user ExecStart=/usr/local/bin/dnote-server start Environment=GO_ENV=PRODUCTION -Environment=OnPremise=true +Environment=OnPremises=true Environment=DBHost=localhost Environment=DBPort=5432 Environment=DBName=dnote diff --git a/host/docker/docker-compose.yml b/host/docker/docker-compose.yml index c10efe37..446e841d 100644 --- a/host/docker/docker-compose.yml +++ b/host/docker/docker-compose.yml @@ -22,7 +22,7 @@ services: DBUser: dnote DBPassword: dnote WebURL: localhost:3000 - OnPremise: "true" + OnPremises: "true" SmtpHost: SmtpPort: SmtpUsername: diff --git a/pkg/server/app/email.go b/pkg/server/app/email.go index 3f398353..fe8811aa 100644 --- a/pkg/server/app/email.go +++ b/pkg/server/app/email.go @@ -32,7 +32,7 @@ var defaultSender = "admin@getdnote.com" // GetSenderEmail returns the sender email func GetSenderEmail(c config.Config, want string) (string, error) { - if !c.OnPremise { + if !c.OnPremises { return want, nil } diff --git a/pkg/server/app/email_test.go b/pkg/server/app/email_test.go index e6e6aa1d..e7d828a1 100644 --- a/pkg/server/app/email_test.go +++ b/pkg/server/app/email_test.go @@ -45,7 +45,7 @@ func TestSendVerificationEmail(t *testing.T) { for _, tc := range testCases { t.Run(fmt.Sprintf("self hosted %t", tc.onPremise), func(t *testing.T) { c := config.Load() - c.SetOnPremise(tc.onPremise) + c.SetOnPremises(tc.onPremise) c.WebURL = "http://example.com" emailBackend := testutils.MockEmailbackendImplementation{} @@ -83,7 +83,7 @@ func TestSendWelcomeEmail(t *testing.T) { for _, tc := range testCases { t.Run(fmt.Sprintf("self hosted %t", tc.onPremise), func(t *testing.T) { c := config.Load() - c.SetOnPremise(tc.onPremise) + c.SetOnPremises(tc.onPremise) c.WebURL = "http://example.com" emailBackend := testutils.MockEmailbackendImplementation{} @@ -121,7 +121,7 @@ func TestSendPasswordResetEmail(t *testing.T) { for _, tc := range testCases { t.Run(fmt.Sprintf("self hosted %t", tc.onPremise), func(t *testing.T) { c := config.Load() - c.SetOnPremise(tc.onPremise) + c.SetOnPremises(tc.onPremise) c.WebURL = "http://example.com" emailBackend := testutils.MockEmailbackendImplementation{} @@ -143,7 +143,7 @@ func TestSendPasswordResetEmail(t *testing.T) { func TestSendSubscriptionConfirmationEmail(t *testing.T) { c := config.Load() - c.SetOnPremise(false) + c.SetOnPremises(false) c.WebURL = "http://example.com" emailBackend := testutils.MockEmailbackendImplementation{} @@ -185,7 +185,7 @@ func TestGetSenderEmail(t *testing.T) { for _, tc := range testCases { t.Run(fmt.Sprintf("on premise %t candidate %s", tc.onPremise, tc.candidate), func(t *testing.T) { c := config.Load() - c.SetOnPremise(tc.onPremise) + c.SetOnPremises(tc.onPremise) c.WebURL = tc.webURL got, err := GetSenderEmail(c, tc.candidate) diff --git a/pkg/server/app/testutils.go b/pkg/server/app/testutils.go index c4d9d5ad..e8174ff5 100644 --- a/pkg/server/app/testutils.go +++ b/pkg/server/app/testutils.go @@ -30,7 +30,7 @@ import ( // NewTest returns an app for a testing environment func NewTest(appParams *App) App { c := config.Load() - c.SetOnPremise(false) + c.SetOnPremises(false) a := App{ DB: testutils.DB, @@ -51,8 +51,8 @@ func NewTest(appParams *App) App { if appParams != nil && appParams.EmailTemplates != nil { a.EmailTemplates = appParams.EmailTemplates } - if appParams != nil && appParams.Config.OnPremise { - a.Config.OnPremise = appParams.Config.OnPremise + if appParams != nil && appParams.Config.OnPremises { + a.Config.OnPremises = appParams.Config.OnPremises } if appParams != nil && appParams.Config.WebURL != "" { a.Config.WebURL = appParams.Config.WebURL diff --git a/pkg/server/app/users.go b/pkg/server/app/users.go index 59a79d06..cf0d1d8c 100644 --- a/pkg/server/app/users.go +++ b/pkg/server/app/users.go @@ -80,7 +80,7 @@ func (a *App) CreateUser(email, password string, passwordConfirmation string) (d // Grant all privileges if self-hosting var pro bool - if a.Config.OnPremise { + if a.Config.OnPremises { pro = true } else { pro = false diff --git a/pkg/server/app/users_test.go b/pkg/server/app/users_test.go index 74eef5f5..1241904d 100644 --- a/pkg/server/app/users_test.go +++ b/pkg/server/app/users_test.go @@ -32,23 +32,23 @@ import ( func TestCreateUser_ProValue(t *testing.T) { testCases := []struct { - onPremise bool + onPremises bool expectedPro bool }{ { - onPremise: true, + onPremises: true, expectedPro: true, }, { - onPremise: false, + onPremises: false, expectedPro: false, }, } for _, tc := range testCases { - t.Run(fmt.Sprintf("self hosting %t", tc.onPremise), func(t *testing.T) { + t.Run(fmt.Sprintf("self hosting %t", tc.onPremises), func(t *testing.T) { c := config.Load() - c.SetOnPremise(tc.onPremise) + c.SetOnPremises(tc.onPremises) defer testutils.ClearData(testutils.DB) diff --git a/pkg/server/config/config.go b/pkg/server/config/config.go index 2d92521e..0b15cb3e 100644 --- a/pkg/server/config/config.go +++ b/pkg/server/config/config.go @@ -24,6 +24,7 @@ import ( "os" "github.com/dnote/dnote/pkg/server/assets" + "github.com/dnote/dnote/pkg/server/log" "github.com/pkg/errors" ) @@ -101,7 +102,7 @@ func loadDBConfig() PostgresConfig { type Config struct { AppEnv string WebURL string - OnPremise bool + OnPremises bool DisableRegistration bool Port string DB PostgresConfig @@ -119,6 +120,13 @@ func getAppEnv() string { return os.Getenv("APP_ENV") } +func checkDeprecatedEnvVars() { + if os.Getenv("OnPremise") != "" { + + log.WithFields(log.Fields{}).Warn("Environment variable 'OnPremise' is deprecated. Please use OnPremises.") + } +} + // Load constructs and returns a new config based on the environment variables. func Load() Config { port := os.Getenv("PORT") @@ -126,11 +134,13 @@ func Load() Config { port = "3000" } + checkDeprecatedEnvVars() + c := Config{ AppEnv: getAppEnv(), WebURL: os.Getenv("WebURL"), Port: port, - OnPremise: readBoolEnv("OnPremise"), + OnPremises: readBoolEnv("OnPremise") || readBoolEnv("OnPremises"), DisableRegistration: readBoolEnv("DisableRegistration"), DB: loadDBConfig(), AssetBaseURL: "", @@ -144,9 +154,9 @@ func Load() Config { return c } -// SetOnPremise sets the OnPremise value -func (c *Config) SetOnPremise(val bool) { - c.OnPremise = val +// SetOnPremises sets the OnPremise value +func (c *Config) SetOnPremises(val bool) { + c.OnPremises = val } // SetAssetBaseURL sets static dir for the confi diff --git a/pkg/server/controllers/users_test.go b/pkg/server/controllers/users_test.go index 7e014c99..907312d2 100644 --- a/pkg/server/controllers/users_test.go +++ b/pkg/server/controllers/users_test.go @@ -55,28 +55,28 @@ func TestJoin(t *testing.T) { email string password string passwordConfirmation string - onPremise bool + onPremises bool expectedPro bool }{ { email: "alice@example.com", password: "pass1234", passwordConfirmation: "pass1234", - onPremise: false, + onPremises: false, expectedPro: false, }, { email: "bob@example.com", password: "Y9EwmjH@Jq6y5a64MSACUoM4w7SAhzvY", passwordConfirmation: "Y9EwmjH@Jq6y5a64MSACUoM4w7SAhzvY", - onPremise: false, + onPremises: false, expectedPro: false, }, { email: "chuck@example.com", password: "e*H@kJi^vXbWEcD9T5^Am!Y@7#Po2@PC", passwordConfirmation: "e*H@kJi^vXbWEcD9T5^Am!Y@7#Po2@PC", - onPremise: false, + onPremises: false, expectedPro: false, }, // on premise @@ -84,7 +84,7 @@ func TestJoin(t *testing.T) { email: "dan@example.com", password: "e*H@kJi^vXbWEcD9T5^Am!Y@7#Po2@PC", passwordConfirmation: "e*H@kJi^vXbWEcD9T5^Am!Y@7#Po2@PC", - onPremise: true, + onPremises: true, expectedPro: true, }, } @@ -99,7 +99,7 @@ func TestJoin(t *testing.T) { Clock: clock.NewMock(), EmailBackend: &emailBackend, Config: config.Config{ - OnPremise: tc.onPremise, + OnPremises: tc.onPremises, }, }) defer server.Close()