package internal import ( "net/url" "regexp" "time" ) const ( // InvalidConfigValue is the constant value for invalid config values // which must be changed for production configurations before successful // startup InvalidConfigValue = "INVALID CONFIG VALUE - PLEASE CHANGE THIS VALUE" // DebugMode is the default debug mode DefaultDebug = false // DefaultData is the default data directory for storage DefaultData = "./data" // DefaultStore is the default data store used for accounts, sessions, etc DefaultStore = "bitcask://spyda.db" // DefaultBaseURL is the default Base URL for the app used to construct feed URLs DefaultBaseURL = "http://0.0.0.0:8000" // DefaultAdminXXX is the default admin user / pod operator DefaultAdminUser = "admin" DefaultAdminPass = "admiN" DefaultAdminName = "Administrator" DefaultAdminEmail = "support@spyda.dev" // DefaultName is the default instance name DefaultName = "spyda.dev" // DefaultMetaxxx are the default set of tags used on non-specific views DefaultMetaTitle = "" DefaultMetaAuthor = "spyda.dev" DefaultMetaKeywords = "spider, crawler, search, engine, web, index, spyda, find, lookup" DefaultMetaDescription = " 🕸 spyda is a privacy first search engine and web crawler." // DefaultTheme is the default theme to use ('light' or 'dark') DefaultTheme = "dark" // DefaultCookieSecret is the server's default cookie secret DefaultCookieSecret = InvalidConfigValue // DefaultResultsPerPage is the server's default results per page to display DefaultResultsPerPage = 10 // DefaultSessionCacheTTL is the server's default session cache ttl DefaultSessionCacheTTL = 1 * time.Hour // DefaultSessionExpiry is the server's default session expiry time DefaultSessionExpiry = 240 * time.Hour // 10 days // DefaultMagicLinkSecret is the jwt magic link secret DefaultMagicLinkSecret = InvalidConfigValue // Default SMTP configuration DefaultSMTPHost = "smtp.gmail.com" DefaultSMTPPort = 587 DefaultSMTPUser = InvalidConfigValue DefaultSMTPPass = InvalidConfigValue DefaultSMTPFrom = InvalidConfigValue // DefaultAPISessionTime is the server's default session time for API tokens DefaultAPISessionTime = 240 * time.Hour // 10 days // DefaultAPISigningKey is the default API JWT signing key for tokens DefaultAPISigningKey = InvalidConfigValue ) var ( // DefaultSearchPrompts are the set of default prompts for search(s) DefaultSearchPrompts = []string{ `What can we find for you today?`, `Whatcha look'n for?`, `Looking for something? Type some keywords and hit Go!`, `Looking for a local buinsess? Find it here!`, `Tired of search engines tracking you? Search freely and privately here!`, `Search the web...`, } ) func NewConfig() *Config { return &Config{ Debug: DefaultDebug, Name: DefaultName, Description: DefaultMetaDescription, Store: DefaultStore, Theme: DefaultTheme, BaseURL: DefaultBaseURL, AdminUser: DefaultAdminUser, AdminPass: DefaultAdminPass, CookieSecret: DefaultCookieSecret, MagicLinkSecret: DefaultMagicLinkSecret, SearchPrompts: DefaultSearchPrompts, ResultsPerPage: DefaultResultsPerPage, SessionExpiry: DefaultSessionExpiry, SMTPHost: DefaultSMTPHost, SMTPPort: DefaultSMTPPort, SMTPUser: DefaultSMTPUser, SMTPPass: DefaultSMTPPass, } } // Option is a function that takes a config struct and modifies it type Option func(*Config) error // WithDebug sets the debug mode lfag func WithDebug(debug bool) Option { return func(cfg *Config) error { cfg.Debug = debug return nil } } // WithData sets the data directory to use for storage func WithData(data string) Option { return func(cfg *Config) error { cfg.Data = data return nil } } // WithStore sets the store to use for accounts, sessions, etc. func WithStore(store string) Option { return func(cfg *Config) error { cfg.Store = store return nil } } // WithBaseURL sets the Base URL used for constructing feed URLs func WithBaseURL(baseURL string) Option { return func(cfg *Config) error { u, err := url.Parse(baseURL) if err != nil { return err } cfg.BaseURL = baseURL cfg.baseURL = u return nil } } // WithAdminUser sets the Admin user used for granting special features to func WithAdminUser(adminUser string) Option { return func(cfg *Config) error { cfg.AdminUser = adminUser return nil } } // WithAdminName sets the Admin name used to identify the pod operator func WithAdminName(adminName string) Option { return func(cfg *Config) error { cfg.AdminName = adminName return nil } } // WithAdminEmail sets the Admin email used to contact the pod operator func WithAdminEmail(adminEmail string) Option { return func(cfg *Config) error { cfg.AdminEmail = adminEmail return nil } } // WithFeedSources sets the feed sources to use for external feeds func WithFeedSources(feedSources []string) Option { return func(cfg *Config) error { cfg.FeedSources = feedSources return nil } } // WithName sets the instance's name func WithName(name string) Option { return func(cfg *Config) error { cfg.Name = name return nil } } // WithDescription sets the instance's description func WithDescription(description string) Option { return func(cfg *Config) error { cfg.Description = description return nil } } // WithTheme sets the default theme to use func WithTheme(theme string) Option { return func(cfg *Config) error { cfg.Theme = theme return nil } } // WithOpenRegistrations sets the open registrations flag func WithOpenRegistrations(openRegistrations bool) Option { return func(cfg *Config) error { cfg.OpenRegistrations = openRegistrations return nil } } // WithCookieSecret sets the server's cookie secret func WithCookieSecret(secret string) Option { return func(cfg *Config) error { cfg.CookieSecret = secret return nil } } // WithResultsPerPage sets the server's results per page func WithResultsPerPage(resultsPerPage int) Option { return func(cfg *Config) error { cfg.ResultsPerPage = resultsPerPage return nil } } // WithSessionCacheTTL sets the server's session cache ttl func WithSessionCacheTTL(cacheTTL time.Duration) Option { return func(cfg *Config) error { cfg.SessionCacheTTL = cacheTTL return nil } } // WithSessionExpiry sets the server's session expiry time func WithSessionExpiry(expiry time.Duration) Option { return func(cfg *Config) error { cfg.SessionExpiry = expiry return nil } } // WithTranscoderTimeout sets the video transcoding timeout func WithTranscoderTimeout(timeout time.Duration) Option { return func(cfg *Config) error { cfg.TranscoderTimeout = timeout return nil } } // WithMagicLinkSecret sets the MagicLinkSecert used to create password reset tokens func WithMagicLinkSecret(secret string) Option { return func(cfg *Config) error { cfg.MagicLinkSecret = secret return nil } } // WithSMTPBind sets the interface and port to bind to for SMTP func WithSMTPBind(smtpBind string) Option { return func(cfg *Config) error { cfg.SMTPBind = smtpBind return nil } } // WithPOP3Bind sets the interface and port to use for POP3 func WithPOP3Bind(pop3Bind string) Option { return func(cfg *Config) error { cfg.POP3Bind = pop3Bind return nil } } // WithSMTPHost sets the SMTPHost to use for sending email func WithSMTPHost(host string) Option { return func(cfg *Config) error { cfg.SMTPHost = host return nil } } // WithSMTPPort sets the SMTPPort to use for sending email func WithSMTPPort(port int) Option { return func(cfg *Config) error { cfg.SMTPPort = port return nil } } // WithSMTPUser sets the SMTPUser to use for sending email func WithSMTPUser(user string) Option { return func(cfg *Config) error { cfg.SMTPUser = user return nil } } // WithSMTPPass sets the SMTPPass to use for sending email func WithSMTPPass(pass string) Option { return func(cfg *Config) error { cfg.SMTPPass = pass return nil } } // WithSMTPFrom sets the SMTPFrom address to use for sending email func WithSMTPFrom(from string) Option { return func(cfg *Config) error { cfg.SMTPFrom = from return nil } } // WithMaxFetchLimit sets the maximum feed fetch limit in bytes func WithMaxFetchLimit(limit int64) Option { return func(cfg *Config) error { cfg.MaxFetchLimit = limit return nil } } // WithAPISessionTime sets the API session time for tokens func WithAPISessionTime(duration time.Duration) Option { return func(cfg *Config) error { cfg.APISessionTime = duration return nil } } // WithAPISigningKey sets the API JWT signing key for tokens func WithAPISigningKey(key string) Option { return func(cfg *Config) error { cfg.APISigningKey = key return nil } } // WithWhitelistedDomains sets the list of domains whitelisted and permitted for external iamges func WithWhitelistedDomains(whitelistedDomains []string) Option { return func(cfg *Config) error { for _, whitelistedDomain := range whitelistedDomains { re, err := regexp.Compile(whitelistedDomain) if err != nil { return err } cfg.whitelistedDomains = append(cfg.whitelistedDomains, re) } return nil } }