Self-hosted email delivery platform that enables applications to send emails via HTTP APIs while handling SMTP delivery, templates, storage, security, and analytics. Fully self-hostable alternative to services like SendGrid or Mailgun.
Go email-relayemail-routinghttp-to-smtpmail-deliverymailchimp-alternativemailgun-alternativemailsendermailservermailtemplatemailtestpostmark-alternativesend-emailsendgrid-alternativesendmailtransactional-emailtransactional-outbox
// Copyright (c) 2026 Jonas Kaninda.
//
// This file is part of Posta.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL-3.0-or-later
package settings
import (
"strconv"
"sync"
"time"
"github.com/goposta/posta/internal/storage/repositories"
"github.com/jkaninda/logger"
)
// Provider gives live access to platform settings with a short cache.
// It reads from the database and caches values in memory, refreshing
// periodically so that admin changes take effect without a restart.
type Provider struct {
repo *repositories.SettingRepository
mu sync.RWMutex
cache map[string]string
loadedAt time.Time
cacheTTL time.Duration
}
// NewProvider creates a settings provider with a default 30-second cache TTL.
func NewProvider(repo *repositories.SettingRepository) *Provider {
p := &Provider{
repo: repo,
cache: make(map[string]string),
cacheTTL: 30 * time.Second,
}
p.refresh()
return p
}
func (p *Provider) refresh() {
settings, err := p.repo.FindAll()
if err != nil {
logger.Error("settings provider: failed to refresh", "error", err)
return
}
p.mu.Lock()
defer p.mu.Unlock()
p.cache = make(map[string]string, len(settings))
for _, s := range settings {
p.cache[s.Key] = s.Value
}
p.loadedAt = time.Now()
}
func (p *Provider) ensureFresh() {
p.mu.RLock()
stale := time.Since(p.loadedAt) > p.cacheTTL
p.mu.RUnlock()
if stale {
p.refresh()
}
}
// GetString returns a string setting or the fallback if not found.
func (p *Provider) GetString(key, fallback string) string {
p.ensureFresh()
p.mu.RLock()
defer p.mu.RUnlock()
if v, ok := p.cache[key]; ok {
return v
}
return fallback
}
// GetInt returns an integer setting or the fallback if not found or invalid.
func (p *Provider) GetInt(key string, fallback int) int {
s := p.GetString(key, "")
if s == "" {
return fallback
}
v, err := strconv.Atoi(s)
if err != nil {
return fallback
}
return v
}
// GetBool returns a boolean setting or the fallback if not found.
func (p *Provider) GetBool(key string, fallback bool) bool {
s := p.GetString(key, "")
if s == "" {
return fallback
}
v, err := strconv.ParseBool(s)
if err != nil {
return fallback
}
return v
}
// Convenience accessors for commonly used settings.
func (p *Provider) MaintenanceMode() bool { return p.GetBool("maintenance_mode", false) }
func (p *Provider) RegistrationEnabled() bool { return p.GetBool("registration_enabled", false) }
func (p *Provider) RequireEmailVerification() bool {
return p.GetBool("require_email_verification", true)
}
func (p *Provider) RequireDomainVerification() bool {
return p.GetBool("require_domain_verification", true)
}
func (p *Provider) TwoFactorRequired() bool { return p.GetBool("two_factor_required", false) }
// HostedFormsEnabled reports whether forms get a public share page at /f/{key}.
func (p *Provider) HostedFormsEnabled() bool { return p.GetBool("hosted_forms_enabled", true) }
// AbuseReportEmail is the contact behind the "Report abuse" link on hosted
// form pages. Empty falls back to the admin email.
func (p *Provider) AbuseReportEmail() string { return p.GetString("abuse_report_email", "") }
func (p *Provider) DefaultRateLimitHourly() int { return p.GetInt("default_rate_limit_hourly", 100) }
func (p *Provider) DefaultRateLimitDaily() int { return p.GetInt("default_rate_limit_daily", 1000) }
func (p *Provider) MaxBatchSize() int { return p.GetInt("max_batch_size", 100) }
func (p *Provider) MaxAttachmentSizeMB() int { return p.GetInt("max_attachment_size_mb", 10) }
func (p *Provider) GlobalBounceThreshold() int { return p.GetInt("global_bounce_threshold", 5) }
func (p *Provider) SMTPTimeoutSeconds() int { return p.GetInt("smtp_timeout_seconds", 30) }
func (p *Provider) RetentionDays() int { return p.GetInt("retention_days", 30) }
// EmailBodyRetentionDays / EmailAttachmentRetentionDays default to the record
// retention so content is purged with the row unless an admin sets a shorter window.
func (p *Provider) EmailBodyRetentionDays() int {
return p.GetInt("email_body_retention_days", p.RetentionDays())
}
func (p *Provider) EmailAttachmentRetentionDays() int {
return p.GetInt("email_attachment_retention_days", p.RetentionDays())
}
func (p *Provider) AuditLogRetentionDays() int { return p.GetInt("audit_log_retention_days", 90) }
func (p *Provider) MessageRetentionDays() int {
return p.GetInt("message_retention_days", 365)
}
func (p *Provider) MessageSpamRetentionDays() int {
return p.GetInt("message_spam_retention_days", 30)
}
func (p *Provider) WebhookDeliveryRetentionDays() int {
return p.GetInt("webhook_delivery_retention_days", 30)
}
func (p *Provider) TrackingEventRetentionDays() int {
return p.GetInt("tracking_event_retention_days", 90)
}
func (p *Provider) LoginRateLimitCount() int {
return p.GetInt("login_rate_limit_count", 10)
}
func (p *Provider) LoginRateLimitWindowMinutes() int {
return p.GetInt("login_rate_limit_window_minutes", 15)
}
func (p *Provider) EmailContentVisibility() bool {
return p.GetBool("email_content_visibility", false)
}
func (p *Provider) CustomHeadersEnabled() bool {
return p.GetBool("custom_headers_enabled", false)
}
func (p *Provider) PasswordResetEnabled() bool {
return p.GetBool("password_reset_enabled", false)
}