Use OnPremises (#631)

This commit is contained in:
Sung 2023-03-04 10:52:35 +11:00 committed by GitHub
commit d52a5bdf48
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 40 additions and 30 deletions

View file

@ -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

View file

@ -22,7 +22,7 @@ services:
DBUser: dnote
DBPassword: dnote
WebURL: localhost:3000
OnPremise: "true"
OnPremises: "true"
SmtpHost:
SmtpPort:
SmtpUsername:

View file

@ -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
}

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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()