diff --git a/internal/consts/consts.go b/internal/consts/consts.go index 92c574a4..3940c660 100644 --- a/internal/consts/consts.go +++ b/internal/consts/consts.go @@ -20,6 +20,8 @@ // Don't judge me. package consts +import "regexp" + // FlagNames is used for storing the names of the various flags used for // initializing and storing urfavecli flag variables. type FlagNames struct { @@ -75,3 +77,6 @@ func GetEnvNames() FlagNames { DbDatabase: "GTS_DB_DATABASE", } } + +var IPV4Regex = regexp.MustCompile(`^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$`) +var HostnameRegex = regexp.MustCompile(`^(?:[a-z0-9]+(?:-[a-z0-9]+)*\.)+[a-z]{2,}$`) diff --git a/internal/db/const.go b/internal/db/const.go deleted file mode 100644 index ca4ab392..00000000 --- a/internal/db/const.go +++ /dev/null @@ -1,44 +0,0 @@ -/* - GoToSocial - Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org - - 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 . -*/ - -package db - -import "regexp" - -const ( - /* - general db defaults - */ - - // default database to use in whatever db implementation we have - defaultDatabase string = "gotosocial" - // default address should in most cases be overwritten - defaultAddress string = "localhost" - - /* - implementation-specific defaults - */ - - // widely-recognised default postgres port - postgresDefaultPort int = 5432 - // default user should in most cases be overwritten - postgresDefaultUser string = "postgres" -) - -var ipv4Regex = regexp.MustCompile(`^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$`) -var hostnameRegex = regexp.MustCompile(`^(?:[a-z0-9]+(?:-[a-z0-9]+)*\.)+[a-z]{2,}$`) diff --git a/internal/db/postgres.go b/internal/db/postgres.go index 14c8d3d7..1eb3eb4b 100644 --- a/internal/db/postgres.go +++ b/internal/db/postgres.go @@ -28,6 +28,7 @@ import ( "github.com/go-fed/activity/streams/vocab" "github.com/go-pg/pg" + "github.com/gotosocial/gotosocial/internal/consts" "github.com/sirupsen/logrus" ) @@ -100,23 +101,22 @@ func derivePGOptions(config *Config) (*pg.Options, error) { return nil, fmt.Errorf("expected db type of %s but got %s", dbTypePostgres, config.Type) } - // use sensible default port - var port int = config.Port - if port == 0 { - port = postgresDefaultPort + // validate port + if config.Port == 0 { + return nil, errors.New("no port set") } // validate address if config.Address == "" { - config.Address = defaultAddress + return nil, errors.New("no address set") } - if !hostnameRegex.MatchString(config.Address) && !ipv4Regex.MatchString(config.Address) && config.Address != "localhost" { + if !consts.HostnameRegex.MatchString(config.Address) && !consts.IPV4Regex.MatchString(config.Address) && config.Address != "localhost" { return nil, fmt.Errorf("address %s was neither an ipv4 address nor a valid hostname", config.Address) } // validate username if config.User == "" { - config.User = postgresDefaultUser + return nil, errors.New("no user set") } // validate that there's a password @@ -126,7 +126,7 @@ func derivePGOptions(config *Config) (*pg.Options, error) { // validate database if config.Database == "" { - config.Database = defaultDatabase + return nil, errors.New("no database set") } // We can rely on the pg library we're using to set