1
0
mirror of https://github.com/lus/pasty.git synced 2023-08-10 21:13:09 +03:00

make config compatibility layer more abstract

This commit is contained in:
Lukas Schulte Pelkum 2023-06-17 18:20:25 +02:00
parent 18839a2021
commit 05a27a00c0
No known key found for this signature in database
GPG Key ID: AB3985CECFAFC962
5 changed files with 92 additions and 39 deletions

View File

@ -29,6 +29,7 @@ func main() {
} }
// Load the configuration // Load the configuration
config.Compatibility()
cfg, err := config.Load() cfg, err := config.Load()
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("Could not load the configuration.") log.Fatal().Err(err).Msg("Could not load the configuration.")
@ -46,10 +47,6 @@ func main() {
zerolog.SetGlobalLevel(level) zerolog.SetGlobalLevel(level)
} }
// Run the configuration compatibility layer
// TODO: Remove this at a later state
configCompatibilityLayer(cfg)
// Determine the correct storage driver to use // Determine the correct storage driver to use
var driver storage.Driver var driver storage.Driver
switch strings.TrimSpace(strings.ToLower(cfg.StorageDriver)) { switch strings.TrimSpace(strings.ToLower(cfg.StorageDriver)) {
@ -78,17 +75,16 @@ func main() {
}() }()
// Start the web server // Start the web server
log.Info().Str("address", cfg.WebAddress).Msg("Starting the web server...") log.Info().Str("address", cfg.Address).Msg("Starting the web server...")
webServer := &web.Server{ webServer := &web.Server{
Address: cfg.WebAddress, Address: cfg.Address,
Storage: driver, Storage: driver,
HastebinSupport: cfg.HastebinSupport, PasteIDLength: cfg.PasteIDLength,
PasteIDLength: cfg.IDLength, PasteIDCharset: cfg.PasteIDCharset,
PasteIDCharset: cfg.IDCharacters, PasteLengthCap: cfg.PasteLengthCap,
PasteLengthCap: cfg.LengthCap,
ModificationTokensEnabled: cfg.ModificationTokens, ModificationTokensEnabled: cfg.ModificationTokens,
ModificationTokenLength: cfg.ModificationTokenLength, ModificationTokenLength: cfg.ModificationTokenLength,
ModificationTokenCharset: cfg.ModificationTokenCharacters, ModificationTokenCharset: cfg.ModificationTokenCharset,
} }
if cfg.Reports.Enabled { if cfg.Reports.Enabled {
webServer.ReportClient = &reports.Client{ webServer.ReportClient = &reports.Client{
@ -117,10 +113,3 @@ func main() {
signal.Notify(shutdownChan, os.Interrupt) signal.Notify(shutdownChan, os.Interrupt)
<-shutdownChan <-shutdownChan
} }
func configCompatibilityLayer(cfg *config.Config) {
// Print a notice if the (now removed) Hastebin support has been enabled
if cfg.HastebinSupport {
log.Warn().Msg("You have enabled the legacy 'Hastebin support' feature. This feature has been removed.")
}
}

View File

@ -0,0 +1,69 @@
package config
import (
"github.com/joho/godotenv"
"github.com/rs/zerolog/log"
"os"
)
var removedKeys = []string{
"PASTY_HASTEBIN_SUPPORT",
"PASTY_STORAGE_FILE_PATH",
"PASTY_STORAGE_MONGODB_CONNECTION_STRING",
"PASTY_STORAGE_MONGODB_DATABASE",
"PASTY_STORAGE_MONGODB_COLLECTION",
"PASTY_STORAGE_S3_ENDPOINT",
"PASTY_STORAGE_S3_ACCESS_KEY_ID",
"PASTY_STORAGE_S3_SECRET_ACCESS_KEY",
"PASTY_STORAGE_S3_SECRET_TOKEN",
"PASTY_STORAGE_S3_SECURE",
"PASTY_STORAGE_S3_REGION",
"PASTY_STORAGE_S3_BUCKET",
}
var keyRedirects = map[string][]string{
"PASTY_ADDRESS": {"PASTY_WEB_ADDRESS"},
"PASTY_STORAGE_DRIVER": {"PASTY_STORAGE_TYPE"},
"PASTY_POSTGRES_DSN": {"PASTY_STORAGE_POSTGRES_DSN"},
"PASTY_PASTE_ID_LENGTH": {"PASTY_ID_LENGTH"},
"PASTY_PASTE_ID_CHARSET": {"PASTY_ID_CHARACTERS"},
"PASTY_PASTE_LENGTH_CAP": {"PASTY_LENGTH_CAP"},
"PASTY_REPORTS_ENABLED": {"PASTY_REPORTS_ENABLED"},
"PASTY_REPORTS_WEBHOOK_URL": {"PASTY_REPORT_WEBHOOK"},
"PASTY_REPORTS_WEBHOOK_TOKEN": {"PASTY_REPORT_WEBHOOK_TOKEN"},
"PASTY_MODIFICATION_TOKEN_CHARSET": {"PASTY_MODIFICATION_TOKEN_CHARACTERS"},
"PASTY_MODIFICATION_TOKENS": {"PASTY_DELETION_TOKENS"},
"PASTY_MODIFICATION_TOKEN_MASTER": {"PASTY_DELETION_TOKEN_MASTER"},
"PASTY_MODIFICATION_TOKEN_LENGTH": {"PASTY_DELETION_TOKEN_LENGTH"},
}
// Compatibility runs several compatibility measurements.
// This is used to redirect legacy config keys to their new equivalent or print warnings about deprecated ones.
func Compatibility() {
_ = godotenv.Overload()
for _, key := range removedKeys {
if isSet(key) {
log.Warn().Msgf("You have set the '%s' environment variable. This variable has been discontinued and has no further effect.", key)
}
}
for newKey, oldKeys := range keyRedirects {
if !isSet(newKey) {
for _, oldKey := range oldKeys {
if isSet(oldKey) {
if err := os.Setenv(newKey, os.Getenv(oldKey)); err != nil {
continue
}
log.Warn().Msgf("You have set the '%s' environment variable. This variable has been renamed to '%s'. The value has been propagated, but please consider changing your configuration to avoid further complications.", oldKey, newKey)
break
}
}
}
}
}
func isSet(key string) bool {
_, ok := os.LookupEnv(key)
return ok
}

View File

@ -8,17 +8,16 @@ import (
type Config struct { type Config struct {
LogLevel string `default:"info" split_words:"true"` LogLevel string `default:"info" split_words:"true"`
WebAddress string `default:":8080" split_words:"true"` Address string `default:":8080" split_words:"true"`
StorageDriver string `default:"sqlite" split_words:"true"` StorageDriver string `default:"sqlite" split_words:"true"`
HastebinSupport bool `default:"false" split_words:"true"` // TODO: Legacy PasteIDLength int `default:"6" split_words:"true"`
IDLength int `default:"6" split_words:"true"` PasteIDCharset string `default:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" split_words:"true"`
IDCharacters string `default:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" split_words:"true"`
ModificationTokens bool `default:"true" split_words:"true"` ModificationTokens bool `default:"true" split_words:"true"`
ModificationTokenMaster string `split_words:"true"` ModificationTokenMaster string `split_words:"true"`
ModificationTokenLength int `default:"12" split_words:"true"` ModificationTokenLength int `default:"12" split_words:"true"`
ModificationTokenCharacters string `default:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" split_words:"true"` ModificationTokenCharset string `default:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" split_words:"true"`
RateLimit string `default:"30-M" split_words:"true"` RateLimit string `default:"30-M" split_words:"true"`
LengthCap int `default:"50000" split_words:"true"` PasteLengthCap int `default:"50000" split_words:"true"`
AutoDelete *AutoDeleteConfig `split_words:"true"` AutoDelete *AutoDeleteConfig `split_words:"true"`
Reports *ReportConfig Reports *ReportConfig
Postgres *PostgresConfig Postgres *PostgresConfig

View File

@ -23,10 +23,6 @@ type Server struct {
// If this is set to nil, the report system will be considered deactivated. // If this is set to nil, the report system will be considered deactivated.
ReportClient *reports.Client ReportClient *reports.Client
// Whether the Hastebin support should be enabled.
// If this is set to 'false', the Hastebin specific endpoints will not be registered.
HastebinSupport bool
// The length of newly generated paste IDs. // The length of newly generated paste IDs.
PasteIDLength int PasteIDLength int
// The charset to use when generating new paste IDs. // The charset to use when generating new paste IDs.

View File

@ -16,7 +16,7 @@ func Middleware(next http.Handler) http.Handler {
} }
}() }()
next.ServeHTTP(writer, request) next.ServeHTTP(proxy, request)
} }
return http.HandlerFunc(fn) return http.HandlerFunc(fn)
} }