config

package
v1.14.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2026 License: GPL-3.0 Imports: 44 Imported by: 0

Documentation

Overview

Package config manages the environment variable configurations.

Index

Constants

View Source
const (
	// ConfigDir is the subdirectory for the home user ".config".
	ConfigDir = "defacto2-app"
	// MinimumFiles is the minimum number of unique filenames expected in an asset subdirectory.
	MinimumFiles = 40_000
	// SessionHours is the default number of hours for the session cookie to remain active.
	SessionHours = 3
	// StdHTTP is the standard port used for a legacy unencrypted HTTP connection.
	StdHTTP Port = 80
	// StdHTTPS is the standard port used for a HTTP web connection.
	StdHTTPS Port = 443
	// StdCustom is the default port number used by this application for an unencrypted HTTP connection.
	StdCustom = 1323
)
View Source
const (
	PortMax = 65534 // PortMax is the highest valid port number.
	PortSys = 1024  // PortSys is the lowest valid port number that does not require system access.
)
View Source
const AbsPreview = "AbsPreview" // AbsPreview means the absolute preview assets directory.
View Source
const AbsThumbnail = "AbsThumbnail" // AbsThumbnail means the absolute thumbnail assets directory.
View Source
const DirWriteWriteBlock = 0o770 // Directory permissions.
View Source
const (
	Timeout = 1 * time.Minute
)

Variables

View Source
var (
	ErrPortMax = fmt.Errorf("http port value must be between 1-%d", PortMax)
	ErrPortSys = fmt.Errorf("http port values between 1-%d require system access", PortSys)
)
View Source
var (
	ErrNoAccounts = errors.New("config: production server has no google oauth2 user accounts to allow admin logins")
	ErrNoOAuth2   = errors.New("config: production server requires a google, oauth2 client id to allow admin logins")
	ErrSession    = errors.New("config: production server has user sessions that never expire")
	ErrNoDir      = errors.New("config: directory does not exist on the host file system")
	ErrTouch      = errors.New("config: directory does not permit file writing")
	ErrNotDir     = errors.New("config: directory points to a file")
	ErrNotFile    = errors.New("config: file path points to a directory")
	ErrNoPort     = errors.New("config: no port, server cannot start without a configured http or a tls port")
	ErrNoPath     = errors.New("config: no path, path or name cannot be empty")
	ErrFormat     = errors.New("config: unsupported format")
	ErrPSVersion  = errors.New("postgres did not return a version value")
)

Functions

func CheckDir

func CheckDir(name dir.Directory, desc string) error

CheckDir runs checks against the named directory, including whether it exists, is a directory, and contains a minimum number of files. Problems will either log warnings or fatal errors.

func Configured added in v1.14.0

func Configured(name string, value any) string

Configured returns both an about text and configured description for the named configuration.

The name is the configuration field such as "AbsLog" for D2_DIR_LOG. The value must be result of the configuration field.

func CustomErrorHandler added in v1.12.0

func CustomErrorHandler(sl *slog.Logger, c *echo.Context, err error)

CustomErrorHandler handles edge case HTTP errors including issues such as missing template files, attempts at browsing restricted directories, etc.

The returned result will always be a text only HTTP response, as there is no ability to access HTML rendered pages.

func DownloadDir added in v0.8.2

func DownloadDir(sl *slog.Logger, src, dest, extra dir.Directory) error

DownloadDir on startup check the download directory for any invalid or unknown files.

func FileID added in v1.7.0

func FileID(r io.Reader) bool

FileID will return true if there are less than 10 lines of text and the maximum width of each line is no more than 45 characters. This is not a guarantee of a FILE_ID.DIZ but it is true for many situations.

func Help added in v1.14.0

func Help(o OAuth2s) string

Help returns human readable help about the authorizations.

func RecordCount added in v0.11.0

func RecordCount(ctx context.Context, exec boil.ContextExecutor) int64

RecordCount returns the number of records in the database.

func Remove added in v1.7.0

func Remove(diz, txt string) (string, error)

Remove deletes either the named diz or txt file from an identical pair. The file deleted depends on whether the pair is identified as a FILE_ID.DIZ or a longer text file. If successful, the base name of the removed file is returned.

func RemoveDir

func RemoveDir(sl *slog.Logger, name, path, root string) error

RemoveDir check the directory for invalid names. If any are found, they are printed to stderr. Any directory that matches the name ".stfolder" is removed.

func RemoveDownload

func RemoveDownload(sl *slog.Logger, basename, path string, backup, extra dir.Directory) error

RemoveDownload checks the download files for invalid names and extensions. If any are found, they are removed without warning. Basename must be the name of the file with a valid file extension.

Valid file extensions are none, .chiptune, .txt, and .zip.

func RemoveImage

func RemoveImage(sl *slog.Logger, basename, path string, backup dir.Directory) error

RemoveImage checks the image files for invalid names and extensions. If any are found, they are moved to the destDir without warning. Basename must be the name of the file with a valid file extension.

Valid file extensions are .png and .webp, and basename must be a valid uuid or cfid with the correct length.

func RenameDownload

func RenameDownload(sl *slog.Logger, basename, absPath string) error

RenameDownload rename the download file if the basename uses an invalid coldfusion uuid.

func StaticOriginal

func StaticOriginal() string

StaticOriginal returns the path to the image directory.

func StaticThumb

func StaticThumb() string

StaticThumb returns the path to the thumbnail directory.

func TempInfo added in v1.14.0

func TempInfo(sl *slog.Logger)

TempInfo is used to print the temporary directory and its disk usage.

func Validate

func Validate(port uint16) error

Validate returns an error if the HTTP or TLS port is invalid.

Types

type Absdown added in v1.8.0

type Absdown Directory

func (Absdown) Help added in v1.8.0

func (a Absdown) Help() string

func (Absdown) Issue added in v1.8.0

func (a Absdown) Issue() string

func (Absdown) LogValue added in v1.8.0

func (a Absdown) LogValue() slog.Value

func (Absdown) String added in v1.8.0

func (a Absdown) String() string

type Absextra added in v1.8.0

type Absextra Directory

func (Absextra) Help added in v1.8.0

func (a Absextra) Help() string

func (Absextra) Issue added in v1.8.0

func (a Absextra) Issue() string

func (Absextra) LogValue added in v1.8.0

func (a Absextra) LogValue() slog.Value

func (Absextra) String added in v1.8.0

func (a Absextra) String() string

type Abslog added in v1.8.0

type Abslog Directory

func (Abslog) Help added in v1.8.0

func (a Abslog) Help() string

func (Abslog) Issue added in v1.8.0

func (a Abslog) Issue() string

func (Abslog) LogValue added in v1.8.0

func (a Abslog) LogValue() slog.Value

func (Abslog) String added in v1.8.0

func (a Abslog) String() string

type Absorphan added in v1.8.0

type Absorphan Directory

func (Absorphan) Help added in v1.8.0

func (a Absorphan) Help() string

func (Absorphan) Issue added in v1.8.0

func (a Absorphan) Issue() string

func (Absorphan) LogValue added in v1.8.0

func (a Absorphan) LogValue() slog.Value

func (Absorphan) String added in v1.8.0

func (a Absorphan) String() string

type Absprev added in v1.8.0

type Absprev Directory

func (Absprev) Help added in v1.8.0

func (a Absprev) Help() string

func (Absprev) Issue added in v1.8.0

func (a Absprev) Issue() string

func (Absprev) LogValue added in v1.8.0

func (a Absprev) LogValue() slog.Value

func (Absprev) String added in v1.8.0

func (a Absprev) String() string

type Absthumb added in v1.8.0

type Absthumb Directory

func (Absthumb) Help added in v1.8.0

func (a Absthumb) Help() string

func (Absthumb) Issue added in v1.8.0

func (a Absthumb) Issue() string

func (Absthumb) LogValue added in v1.8.0

func (a Absthumb) LogValue() slog.Value

func (Absthumb) String added in v1.8.0

func (a Absthumb) String() string

type Abstlscrt added in v1.8.0

type Abstlscrt File

func (Abstlscrt) Help added in v1.8.0

func (a Abstlscrt) Help() string

func (Abstlscrt) Issue added in v1.8.0

func (a Abstlscrt) Issue() string

func (Abstlscrt) LogValue added in v1.8.0

func (a Abstlscrt) LogValue() slog.Value

func (Abstlscrt) String added in v1.8.0

func (a Abstlscrt) String() string

type Abstlskey added in v1.8.0

type Abstlskey File

func (Abstlskey) Help added in v1.8.0

func (a Abstlskey) Help() string

func (Abstlskey) Issue added in v1.8.0

func (a Abstlskey) Issue() string

func (Abstlskey) LogValue added in v1.8.0

func (a Abstlskey) LogValue() slog.Value

func (Abstlskey) String added in v1.8.0

func (a Abstlskey) String() string

type Config

type Config struct {
	AbsLog         Abslog     `env:"D2_DIR_LOG"`                // absolute directory path to store log files
	AbsDownload    Absdown    `env:"D2_DIR_DOWNLOAD"`           // absolute directory path to the artifact file downloads
	AbsPreview     Absprev    `env:"D2_DIR_PREVIEW"`            // absolute directory path to the preview images
	AbsThumbnail   Absthumb   `env:"D2_DIR_THUMBNAIL"`          // absolute directory path to the thumbnail images
	AbsExtra       Absextra   `env:"D2_DIR_EXTRA"`              // absolute directory path to the artifact extras
	AbsOrphaned    Absorphan  `env:"D2_DIR_ORPHANED"`           // absolute directory path to the retired artifacts
	DatabaseURL    Connection `env:"D2_DATABASE_URL"`           // url to the database
	SessionKey     Sessionkey `env:"D2_SESSION_KEY,unset"`      // key or random value used for cookie encryption
	SessionMaxAge  Hours      `env:"D2_SESSION_MAX_AGE"`        // number of hours to keep a cookie session
	GoogleClientID GoogleAuth `env:"D2_GOOGLE_CLIENT_ID,unset"` // google oauth2 clien id
	GoogleIDs      GoogleID   `env:"D2_GOOGLE_IDS,unset"`       // google accounts
	HTTPPort       PortHTTP   `env:"D2_HTTP_PORT"`              // port value for serving the website using http
	TLSPort        PortTLS    `env:"D2_TLS_PORT"`               // port value for serving the website using https
	TLSCert        Abstlscrt  `env:"D2_TLS_CERT"`               // absolute path to the tls certificate file
	TLSKey         Abstlskey  `env:"D2_TLS_KEY"`                // absolute path to the tls key file
	MatchHost      Matchhost  `env:"D2_MATCH_HOST"`             // restrict connections to specific internet hosts or ip addresses
	MaxProcs       Threads    `env:"D2_MAX_PROCS"`              // retrict the number of os threads the server can use
	Quiet          Toggle     `env:"D2_QUIET"`                  // suppress startup terminal text
	Compression    Toggle     `env:"D2_COMPRESSION"`            // enable gzip compression when serving webpages
	ProdMode       Toggle     `env:"D2_PROD_MODE"`              // production mode, save log files and always recover from panics
	ReadOnly       Toggle     `env:"D2_READ_ONLY"`              // read only mode, disables all post, put, delete http requests
	NoCrawl        Toggle     `env:"D2_NO_CRAWL"`               // always insert a http header to ask search engines not to crawl the site
	LogAll         Toggle     `env:"D2_LOG_ALL"`                // log all client requests, both errors and successful responses
	GoogleAccounts OAuth2s    // GoogleAccounts is the data store for the GoogleIDs.
}

Config options for the Defacto2 server using the caarlos0/env package.

caarlos0/env:https://github.com/caarlos0/env

func (Config) Addresses

func (c Config) Addresses(sl *slog.Logger) error

Addresses returns a list of urls that the server is accessible from.

func (*Config) Assets added in v0.8.2

func (c *Config) Assets(ctx context.Context, sl *slog.Logger, exec boil.ContextExecutor) error

Assets on startup checks the file system directories for any invalid or unknown files. These specifically match the base filename against the UUID column in the database. When there is no matching UUID, the file is considered orphaned and these are moved to the orphaned directory without warning.

There are no checks on the 3 directories that get scanned.

func (*Config) Checks

func (c *Config) Checks(ctx context.Context, sl *slog.Logger) error

Checks runs a number of sanity checks for the environment variable configurations.

func (Config) Configurations added in v1.14.0

func (c Config) Configurations(sl *slog.Logger)

Configurations prints the application settings to the logger.

func (*Config) Fixer added in v0.11.0

func (c *Config) Fixer(ctx context.Context, sl *slog.Logger, d time.Time) error

Fixer is used to fix any known issues with the file assets and the database entries.

func (Config) Help added in v1.14.0

func (c Config) Help() map[string]string

Help returns a mapped inventory of description texts for documentation/CLI usage.

func (*Config) ImageDirs added in v1.6.0

func (c *Config) ImageDirs(sl *slog.Logger) error

ImageDirs on startup check the image directories for any invalid or unknown files.

func (*Config) LogStore

func (c *Config) LogStore() error

LogStore determines the local storage path for all log files created by this web application.

func (*Config) MagicNumbers added in v0.10.0

func (c *Config) MagicNumbers(ctx context.Context, sl *slog.Logger, exec boil.ContextExecutor) error

MagicNumbers checks the magic numbers of the artifacts and replaces any missing or legacy values with the current method of detection. Previous detection methods were done using the `file` command line utility, which is a bit to verbose for our needs. MagicNumbers checks the magic numbers of the artifacts and replaces any missing or legacy values with the current method of detection.

func (Config) Names added in v0.8.0

func (c Config) Names() []string

Names returns a list of the field names in the Config struct.

func (*Config) Override

func (c *Config) Override()

Override the configuration settings fetched from the environment.

func (*Config) Previews added in v0.10.0

func (c *Config) Previews(ctx context.Context, sl *slog.Logger, exec boil.ContextExecutor) error

Previews on startup check the preview directory for any unnecessary preview images such as textfile artifacts.

func (Config) PurgeTemp added in v1.14.0

func (c Config) PurgeTemp() error

func (*Config) RepairArchives added in v1.14.0

func (c *Config) RepairArchives(ctx context.Context, sl *slog.Logger, exec boil.ContextExecutor) error

RepairArchives checks the download directory for any legacy and obsolete archives. Obsolete archives are those that use a legacy compression method that is not supported by Go or JS libraries used by the website.

func (*Config) TextFiles added in v1.5.18

func (c *Config) TextFiles(ctx context.Context, sl *slog.Logger, exec boil.ContextExecutor) error

TextFiles on startup check the extra directory for any readme text files that are duplicates of the diz text files.

func (Config) UseHTTP

func (c Config) UseHTTP() bool

UseHTTP returns true if the server is configured to use HTTP.

func (Config) UseLocal added in v1.12.0

func (c Config) UseLocal() bool

UseLocal returns true if the server is configured to use the local TSL-mode.

func (Config) UseTLS

func (c Config) UseTLS() bool

UseTLS returns true if the server is configured to use TLS.

func (*Config) Warn added in v1.14.0

func (c *Config) Warn(sl *slog.Logger, msg, name string, err error)

type Connection added in v1.8.0

type Connection string

func (Connection) LogValue added in v1.8.0

func (c Connection) LogValue() slog.Value

func (Connection) String added in v1.14.0

func (c Connection) String() string

type Directory added in v1.8.0

type Directory string // Directory contains an absolute path to a directory.

func (Directory) Check added in v1.8.0

func (d Directory) Check() error

func (Directory) Issue added in v1.8.0

func (d Directory) Issue() string

func (Directory) LogValue added in v1.8.0

func (d Directory) LogValue() slog.Value

func (Directory) String added in v1.8.0

func (d Directory) String() string

type File added in v1.8.0

type File string // File contains an absolute path to a file.

func (File) Check added in v1.8.0

func (f File) Check() error

func (File) Issue added in v1.8.0

func (f File) Issue() string

func (File) LogValue added in v1.8.0

func (f File) LogValue() slog.Value

func (File) String added in v1.8.0

func (f File) String() string

type GoogleAuth added in v1.14.0

type GoogleAuth string

func (GoogleAuth) Help added in v1.14.0

func (g GoogleAuth) Help() string

func (GoogleAuth) LogValue added in v1.14.0

func (g GoogleAuth) LogValue() slog.Value

func (GoogleAuth) String added in v1.14.0

func (g GoogleAuth) String() string

type GoogleID added in v1.14.0

type GoogleID string

func (GoogleID) Help added in v1.14.0

func (g GoogleID) Help() string

func (GoogleID) LogValue added in v1.14.0

func (g GoogleID) LogValue() slog.Value

func (GoogleID) String added in v1.14.0

func (g GoogleID) String() string

type Helper added in v1.14.0

type Helper interface {
	Help() string
}

Helper describes any config field capable of providing contextual guidance.

type Hours added in v1.8.0

type Hours int // Hours is a duration value

func (Hours) Int added in v1.8.0

func (h Hours) Int() int

func (Hours) LogValue added in v1.8.0

func (h Hours) LogValue() slog.Value

func (Hours) String added in v1.8.0

func (h Hours) String() string

type Issuer added in v1.14.0

type Issuer interface {
	Issue() string
}

Issuer describes any config field capable of reporting validation issues.

type Matchhost added in v1.8.0

type Matchhost string

func (Matchhost) Help added in v1.8.0

func (m Matchhost) Help() string

func (Matchhost) LogValue added in v1.8.0

func (m Matchhost) LogValue() slog.Value

func (Matchhost) String added in v1.8.0

func (m Matchhost) String() string

type OAuth2s added in v1.8.0

type OAuth2s [][48]byte

OAuth2s is a slice of Google OAuth2 accounts that are allowed to login. Each account is a 48 byte slice of bytes that represents the SHA-384 hash of the unique Google ID.

func (OAuth2s) Help added in v1.8.0

func (o OAuth2s) Help() string

func (OAuth2s) LogValue added in v1.8.0

func (o OAuth2s) LogValue() slog.Value

func (OAuth2s) String added in v1.8.0

func (o OAuth2s) String() string

func (OAuth2s) Values added in v1.8.0

func (o OAuth2s) Values() [][48]byte

type Port added in v1.8.0

type Port uint16 // Port is a network port number.

func (Port) Check added in v1.8.0

func (p Port) Check() error

func (Port) LogValue added in v1.8.0

func (p Port) LogValue() slog.Value

func (Port) Value added in v1.8.0

func (p Port) Value() uint16

type PortHTTP added in v1.8.0

type PortHTTP Port

func (PortHTTP) Check added in v1.8.0

func (p PortHTTP) Check() error

func (PortHTTP) Help added in v1.8.0

func (p PortHTTP) Help() string

func (PortHTTP) LogValue added in v1.8.0

func (p PortHTTP) LogValue() slog.Value

func (PortHTTP) Value added in v1.8.0

func (p PortHTTP) Value() uint16

type PortTLS added in v1.8.0

type PortTLS Port

func (PortTLS) Check added in v1.8.0

func (p PortTLS) Check() error

func (PortTLS) Help added in v1.8.0

func (p PortTLS) Help() string

func (PortTLS) LogValue added in v1.8.0

func (p PortTLS) LogValue() slog.Value

func (PortTLS) Value added in v1.8.0

func (p PortTLS) Value() uint16

type Repack added in v1.14.0

type Repack struct {
	Source      string        // Source is the file extracted to a temporary directory and re-compressed.
	UID         string        // UID is the destination filename using a universal unique ID naming syntax.
	Destination dir.Directory // Destination is the directory to save the re-compressed file.
}

Repack are the source and destination arguments required by the ReArchive Repair method.

type Repair added in v0.9.0

type Repair int

Repair is a type of archive for the re-archive and recompress methods.

const (
	Zip Repair = iota // ZIP and PKZip archives
	LHA               // LHA and LZH archives
	Arc               // ARC archives
	Arj               // ARJ archives
)

func (Repair) NewArchive added in v1.14.0

func (r Repair) NewArchive(ctx context.Context, sl *slog.Logger, ra Repack) error

NewArchive extracts the source archive file and then repackages (rearchives) it to a DEFLATE zip archive. The original source file is always kept.

func (Repair) String added in v0.9.0

func (r Repair) String() string

type Sessionkey added in v1.8.0

type Sessionkey string

func (Sessionkey) Help added in v1.8.0

func (s Sessionkey) Help() string

func (Sessionkey) LogValue added in v1.8.0

func (s Sessionkey) LogValue() slog.Value

func (Sessionkey) String added in v1.8.0

func (s Sessionkey) String() string

type Threads added in v1.8.0

type Threads uint

func (Threads) Help added in v1.8.0

func (t Threads) Help() string

func (Threads) LogValue added in v1.8.0

func (t Threads) LogValue() slog.Value

func (Threads) String added in v1.8.0

func (t Threads) String() string

type Toggle added in v1.8.0

type Toggle bool // Toggle is a boolean value that returns a humanized string.

func (Toggle) Bool added in v1.8.0

func (t Toggle) Bool() bool

func (Toggle) LogValue added in v1.8.0

func (t Toggle) LogValue() slog.Value

Directories

Path Synopsis
Package fixarc checks for redundant SAE ARC files that require re-archiving.
Package fixarc checks for redundant SAE ARC files that require re-archiving.
Package fixarj checks for legacy ARJ files that require re-archiving.
Package fixarj checks for legacy ARJ files that require re-archiving.
Package fixlha checks for legacy LHA files that require re-archiving.
Package fixlha checks for legacy LHA files that require re-archiving.
Package fixzip checks for legacy ZIP files with out-of-date compression that require re-archiving.
Package fixzip checks for legacy ZIP files with out-of-date compression that require re-archiving.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL