1
0
mirror of https://github.com/axllent/mailpit.git synced 2025-07-01 00:45:27 +02:00

Chore: Switch database flag/env to --database / MP_DATABASE

The original `--db-file` / `MP_DATA_FILE`, although deprecated, won't be removed any time soon to ensure backwards compatibility with existing integrations
This commit is contained in:
Ralph Slooten
2024-04-12 14:47:47 +12:00
parent a7dfbf4af0
commit 48c73ae97b
7 changed files with 22 additions and 20 deletions

View File

@ -19,7 +19,7 @@ If you have several thousand messages in your mailbox, then it is advised to shu
Mailpit while you reindex as this process will likely result in database locking issues.`, Mailpit while you reindex as this process will likely result in database locking issues.`,
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
config.DataFile = args[0] config.Database = args[0]
config.MaxMessages = 0 config.MaxMessages = 0
if err := storage.InitDB(); err != nil { if err := storage.InitDB(); err != nil {

View File

@ -80,8 +80,8 @@ func init() {
// load environment variables // load environment variables
initConfigFromEnv() initConfigFromEnv()
rootCmd.Flags().StringVarP(&config.DataFile, "db-file", "d", config.DataFile, "Database file to store persistent data") rootCmd.Flags().StringVarP(&config.Database, "database", "d", config.Database, "Database to store persistent data")
rootCmd.Flags().StringVar(&config.TenantID, "db-tenant-id", config.TenantID, "Database tenant ID to isolate data") rootCmd.Flags().StringVar(&config.TenantID, "tenant-id", config.TenantID, "Database tenant ID to isolate data")
rootCmd.Flags().IntVarP(&config.MaxMessages, "max", "m", config.MaxMessages, "Max number of messages to store") rootCmd.Flags().IntVarP(&config.MaxMessages, "max", "m", config.MaxMessages, "Max number of messages to store")
rootCmd.Flags().BoolVar(&config.UseMessageDates, "use-message-dates", config.UseMessageDates, "Use message dates as the received dates") rootCmd.Flags().BoolVar(&config.UseMessageDates, "use-message-dates", config.UseMessageDates, "Use message dates as the received dates")
rootCmd.Flags().BoolVar(&config.IgnoreDuplicateIDs, "ignore-duplicate-ids", config.IgnoreDuplicateIDs, "Ignore duplicate messages (by Message-Id)") rootCmd.Flags().BoolVar(&config.IgnoreDuplicateIDs, "ignore-duplicate-ids", config.IgnoreDuplicateIDs, "Ignore duplicate messages (by Message-Id)")
@ -133,6 +133,10 @@ func init() {
rootCmd.Flags().StringVar(&config.WebhookURL, "webhook-url", config.WebhookURL, "Send a webhook request for new messages") rootCmd.Flags().StringVar(&config.WebhookURL, "webhook-url", config.WebhookURL, "Send a webhook request for new messages")
rootCmd.Flags().IntVar(&webhook.RateLimit, "webhook-limit", webhook.RateLimit, "Limit webhook requests per second") rootCmd.Flags().IntVar(&webhook.RateLimit, "webhook-limit", webhook.RateLimit, "Limit webhook requests per second")
// DEPRECATED FLAG 2024/04/12 - but will not be removed to maintain backwards compatibility
rootCmd.Flags().StringVar(&config.Database, "db-file", config.Database, "Database file to store persistent data")
rootCmd.Flags().Lookup("db-file").Hidden = true
// DEPRECATED FLAGS 2023/03/12 // DEPRECATED FLAGS 2023/03/12
rootCmd.Flags().StringVar(&config.UITLSCert, "ui-ssl-cert", config.UITLSCert, "SSL certificate for web UI - requires ui-ssl-key") rootCmd.Flags().StringVar(&config.UITLSCert, "ui-ssl-cert", config.UITLSCert, "SSL certificate for web UI - requires ui-ssl-key")
rootCmd.Flags().StringVar(&config.UITLSKey, "ui-ssl-key", config.UITLSKey, "SSL key for web UI - requires ui-ssl-cert") rootCmd.Flags().StringVar(&config.UITLSKey, "ui-ssl-key", config.UITLSKey, "SSL key for web UI - requires ui-ssl-cert")
@ -156,11 +160,11 @@ func init() {
// Load settings from environment // Load settings from environment
func initConfigFromEnv() { func initConfigFromEnv() {
// General // General
if len(os.Getenv("MP_DB_FILE")) > 0 { if len(os.Getenv("MP_DATABASE")) > 0 {
config.DataFile = os.Getenv("MP_DB_FILE") config.Database = os.Getenv("MP_DATABASE")
} }
config.TenantID = os.Getenv("MP_DB_TENANT") config.TenantID = os.Getenv("MP_TENANT_ID")
if len(os.Getenv("MP_MAX_MESSAGES")) > 0 { if len(os.Getenv("MP_MAX_MESSAGES")) > 0 {
config.MaxMessages, _ = strconv.Atoi(os.Getenv("MP_MAX_MESSAGES")) config.MaxMessages, _ = strconv.Atoi(os.Getenv("MP_MAX_MESSAGES"))
@ -229,7 +233,6 @@ func initConfigFromEnv() {
if getEnabledFromEnv("MP_SMTP_REQUIRE_TLS") { if getEnabledFromEnv("MP_SMTP_REQUIRE_TLS") {
config.SMTPRequireTLS = true config.SMTPRequireTLS = true
} }
if getEnabledFromEnv("MP_SMTP_AUTH_ALLOW_INSECURE") { if getEnabledFromEnv("MP_SMTP_AUTH_ALLOW_INSECURE") {
config.SMTPAuthAllowInsecure = true config.SMTPAuthAllowInsecure = true
} }
@ -295,12 +298,11 @@ func initConfigFromEnv() {
// load deprecated settings from environment and warn // load deprecated settings from environment and warn
func initDeprecatedConfigFromEnv() { func initDeprecatedConfigFromEnv() {
// deprecated 2024/04/08 // deprecated 2024/04/12 - but will not be removed to maintain backwards compatibility
if len(os.Getenv("MP_DATA_FILE")) > 0 { if len(os.Getenv("MP_DATA_FILE")) > 0 {
// do not warn - this will remain for quite some time config.Database = os.Getenv("MP_DATA_FILE")
// logger.Log().Warn("ENV MP_DATA_FILE has been deprecated, use MP_DB_FILE")
config.DataFile = os.Getenv("MP_DATA_FILE")
} }
// deprecated 2023/03/12 // deprecated 2023/03/12
if len(os.Getenv("MP_UI_SSL_CERT")) > 0 { if len(os.Getenv("MP_UI_SSL_CERT")) > 0 {
logger.Log().Warn("ENV MP_UI_SSL_CERT has been deprecated, use MP_UI_TLS_CERT") logger.Log().Warn("ENV MP_UI_SSL_CERT has been deprecated, use MP_UI_TLS_CERT")

View File

@ -26,8 +26,8 @@ var (
// HTTPListen to listen on <interface>:<port> // HTTPListen to listen on <interface>:<port>
HTTPListen = "[::]:8025" HTTPListen = "[::]:8025"
// DataFile for mail (optional) // Database for mail (optional)
DataFile string Database string
// TenantID is an optional prefix to be applied to all database tables, // TenantID is an optional prefix to be applied to all database tables,
// allowing multiple isolated instances of Mailpit to share a database. // allowing multiple isolated instances of Mailpit to share a database.
@ -189,8 +189,8 @@ func VerifyConfig() error {
cssFontRestriction, cssFontRestriction, cssFontRestriction, cssFontRestriction,
) )
if DataFile != "" && isDir(DataFile) { if Database != "" && isDir(Database) {
DataFile = filepath.Join(DataFile, "mailpit.db") Database = filepath.Join(Database, "mailpit.db")
} }
TenantID = strings.TrimSpace(TenantID) TenantID = strings.TrimSpace(TenantID)

View File

@ -94,7 +94,7 @@ func Load() AppInformation {
} }
} }
info.Database = config.DataFile info.Database = config.Database
info.DatabaseSize = storage.DbSize() info.DatabaseSize = storage.DbSize()
info.Messages = storage.CountTotal() info.Messages = storage.CountTotal()
info.Unread = storage.CountUnread() info.Unread = storage.CountUnread()

View File

@ -39,7 +39,7 @@ var (
// InitDB will initialise the database // InitDB will initialise the database
func InitDB() error { func InitDB() error {
p := config.DataFile p := config.Database
var dsn string var dsn string
if p == "" { if p == "" {
@ -61,7 +61,7 @@ func InitDB() error {
logger.Log().Debugf("[db] opening database %s", p) logger.Log().Debugf("[db] opening database %s", p)
} }
config.DataFile = p config.Database = p
var err error var err error

View File

@ -19,7 +19,7 @@ var (
func setup() { func setup() {
logger.NoLogging = true logger.NoLogging = true
config.MaxMessages = 0 config.MaxMessages = 0
config.DataFile = os.Getenv("MP_DATA_FILE") config.Database = os.Getenv("MP_DATABASE")
if err := InitDB(); err != nil { if err := InitDB(); err != nil {
panic(err) panic(err)

View File

@ -205,7 +205,7 @@ func TestAPIv1Search(t *testing.T) {
func setup() { func setup() {
logger.NoLogging = true logger.NoLogging = true
config.MaxMessages = 0 config.MaxMessages = 0
config.DataFile = os.Getenv("MP_DATA_FILE") config.Database = os.Getenv("MP_DATABASE")
if err := storage.InitDB(); err != nil { if err := storage.InitDB(); err != nil {
panic(err) panic(err)