Browse Source

Remove more useless code borrowed from yarn's codebase

master
James Mills 2 months ago
parent
commit
09d1ea673e
Signed by: prologic GPG Key ID: AC4C014F1440EBD6
  1. 26
      cmd/yarns/main.go
  2. 15
      internal/config.go
  3. 30
      internal/email.go
  4. 56
      internal/options.go
  5. 3
      internal/server.go
  6. 2
      internal/static/css/yarns.min.css
  7. 2
      yarns.yml

26
cmd/yarns/main.go

@ -32,8 +32,6 @@ var (
baseURL string
// Pod Oeprator
adminUser string
adminPass string
adminName string
adminEmail string
@ -41,9 +39,7 @@ var (
resultsPerPage int
// Secrets
apiSigningKey string
cookieSecret string
magiclinkSecret string
cookieSecret string
// Email Setitngs
smtpHost string
@ -58,7 +54,6 @@ var (
// Timeouts
sessionExpiry time.Duration
sessionCacheTTL time.Duration
apiSessionTime time.Duration
)
func init() {
@ -77,8 +72,6 @@ func init() {
// Administration
flag.StringVarP(&adminName, "admin-name", "N", internal.DefaultAdminName, "default admin user name")
flag.StringVarP(&adminEmail, "admin-email", "E", internal.DefaultAdminEmail, "default admin user email")
flag.StringVarP(&adminUser, "admin-user", "A", internal.DefaultAdminUser, "default admin user to use")
flag.StringVarP(&adminPass, "admin-pass", "P", internal.DefaultAdminName, "default admin user name")
// Limits
flag.IntVarP(
@ -87,18 +80,10 @@ func init() {
)
// Secrets
flag.StringVar(
&apiSigningKey, "api-signing-key", internal.DefaultAPISigningKey,
"secret to use for signing api tokens",
)
flag.StringVar(
&cookieSecret, "cookie-secret", internal.DefaultCookieSecret,
"cookie secret to use secure sessions",
)
flag.StringVar(
&magiclinkSecret, "magiclink-secret", internal.DefaultMagicLinkSecret,
"magiclink secret to use for password reset tokens",
)
// Email Setitngs
flag.StringVar(&smtpHost, "smtp-host", internal.DefaultSMTPHost, "SMTP Host to use for email sending")
@ -122,10 +107,6 @@ func init() {
&sessionCacheTTL, "session-cache-ttl", internal.DefaultSessionCacheTTL,
"time-to-live for cached sessions",
)
flag.DurationVar(
&apiSessionTime, "api-session-time", internal.DefaultAPISessionTime,
"timeout for api tokens to expire",
)
}
func flagNameFromEnvironmentName(s string) string {
@ -189,8 +170,6 @@ func main() {
internal.WithBaseURL(baseURL),
// Administration
internal.WithAdminUser(adminUser),
internal.WithAdminPass(adminPass),
internal.WithAdminName(adminName),
internal.WithAdminEmail(adminEmail),
@ -198,9 +177,7 @@ func main() {
internal.WithResultsPerPage(resultsPerPage),
// Secrets
internal.WithAPISigningKey(apiSigningKey),
internal.WithCookieSecret(cookieSecret),
internal.WithMagicLinkSecret(magiclinkSecret),
// Email Setitngs
internal.WithSMTPHost(smtpHost),
@ -215,7 +192,6 @@ func main() {
// Timeouts
internal.WithSessionExpiry(sessionExpiry),
internal.WithSessionCacheTTL(sessionCacheTTL),
internal.WithAPISessionTime(apiSessionTime),
)
if err != nil {
log.WithError(err).Fatal("error creating server")

15
internal/config.go

@ -25,8 +25,6 @@ type Config struct {
Store string
Theme string
BaseURL string
AdminUser string
AdminPass string
AdminName string
AdminEmail string
@ -37,13 +35,10 @@ type Config struct {
SearchPrompts []string
ResultsPerPage int
APISessionTime time.Duration
SessionExpiry time.Duration
SessionCacheTTL time.Duration
APISigningKey string
CookieSecret string
MagicLinkSecret string
CookieSecret string
SMTPHost string
SMTPPort int
@ -79,14 +74,6 @@ func (c *Config) Validate() error {
return fmt.Errorf("error: COOKIE_SECRET is not configured")
}
if c.MagicLinkSecret == InvalidConfigValue {
return fmt.Errorf("error: MAGICLINK_SECRET is not configured")
}
if c.APISigningKey == InvalidConfigValue {
return fmt.Errorf("error: API_SIGNING_KEY is not configured")
}
return nil
}

30
internal/email.go

@ -14,21 +14,6 @@ import (
var (
ErrSendingEmail = errors.New("error: unable to send email")
passwordResetEmailTemplate = template.Must(template.New("email").Parse(`Hello {{ .Username }},
You have requested to have your password on {{ .Pod }} reset for your account.
**IMPORTANT:** If this was __NOT__ initiated by you, please ignore this email and contract support!
To reset your password, please visit the following link:
{{ .BaseURL}}/newPassword?token={{ .Token }}
Kind regards,
{{ .Pod}} Support
`))
supportRequestEmailTemplate = template.Must(template.New("email").Parse(`Hello {{ .AdminUser }},
{{ .Name }} <{{ .Email }} from {{ .Pod }} has sent the following support request:
@ -39,20 +24,12 @@ Kind regards,
Kind regards,
{{ .Pod}} Support
{{ .Instance }} Support
`))
)
type PasswordResetEmailContext struct {
Pod string
BaseURL string
Token string
Username string
}
type SupportRequestEmailContext struct {
Pod string
Instance string
AdminUser string
Name string
@ -103,8 +80,7 @@ func SendSupportRequestEmail(conf *Config, name, email, subject, message string)
conf.Name, subject,
)
ctx := SupportRequestEmailContext{
Pod: conf.Name,
AdminUser: conf.AdminUser,
Instance: conf.Name,
Name: name,
Email: email,

56
internal/options.go

@ -24,8 +24,6 @@ const (
DefaultBaseURL = "http://0.0.0.0:8000"
// DefaultAdminXXX is the default admin user / pod operator
DefaultAdminUser = "admin"
DefaultAdminPass = "admiN"
DefaultAdminName = "Administrator"
DefaultAdminEmail = "support@twtxt.net"
@ -53,9 +51,6 @@ const (
// 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
@ -65,12 +60,6 @@ const (
// DefaultMaxFetchLimit is the maximum fetch fetch limit in bytes
DefaultMaxFetchLimit = 1 << 24 // 16MB (or more than enough for several years)
// 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 (
@ -100,11 +89,8 @@ func NewConfig() *Config {
Store: DefaultStore,
Theme: DefaultTheme,
BaseURL: DefaultBaseURL,
AdminUser: DefaultAdminUser,
AdminPass: DefaultAdminPass,
CookieSecret: DefaultCookieSecret,
MagicLinkSecret: DefaultMagicLinkSecret,
CookieSecret: DefaultCookieSecret,
SeedURLS: DefaultSeedURLS,
@ -160,22 +146,6 @@ func WithBaseURL(baseURL string) Option {
}
}
// WithAdminUser sets the Admin username
func WithAdminUser(adminUser string) Option {
return func(cfg *Config) error {
cfg.AdminUser = adminUser
return nil
}
}
// WithAdminPass sets the Admin password
func WithAdminPass(adminPass string) Option {
return func(cfg *Config) error {
cfg.AdminPass = adminPass
return nil
}
}
// WithAdminName sets the Admin name used to identify the pod operator
func WithAdminName(adminName string) Option {
return func(cfg *Config) error {
@ -248,14 +218,6 @@ func WithSessionExpiry(expiry time.Duration) Option {
}
}
// 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
}
}
// WithSMTPHost sets the SMTPHost to use for sending email
func WithSMTPHost(host string) Option {
return func(cfg *Config) error {
@ -303,19 +265,3 @@ func WithMaxFetchLimit(limit int64) Option {
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
}
}

3
internal/server.go

@ -527,15 +527,12 @@ func NewServer(bind string, options ...Option) (*Server, error) {
// Log interesting configuration options
log.Infof("Instance Name: %s", server.config.Name)
log.Infof("Base URL: %s", server.config.BaseURL)
log.Infof("Admin User: %s", server.config.AdminUser)
log.Infof("Admin Name: %s", server.config.AdminName)
log.Infof("Admin Email: %s", server.config.AdminEmail)
log.Infof("SMTP Host: %s", server.config.SMTPHost)
log.Infof("SMTP Port: %d", server.config.SMTPPort)
log.Infof("SMTP User: %s", server.config.SMTPUser)
log.Infof("SMTP From: %s", server.config.SMTPFrom)
log.Infof("Max Fetch Limit: %s", humanize.Bytes(uint64(server.config.MaxFetchLimit)))
log.Infof("API Session Time: %s", server.config.APISessionTime)
server.initRoutes()

2
internal/static/css/yarns.min.css

File diff suppressed because one or more lines are too long

2
yarns.yml

@ -9,9 +9,7 @@ services:
- BASE_URL=https://search.twtxt.net
- ADMIN_USER=admin
- ADMIN_PASS=CHANGEME
- API_SIGNING_KEY=CHANGEME
- COOKIE_SECRET=CHANGEME
- MAGICLINK_SECRET=CHANGEME
volumes:
- yarns:/data
networks:

Loading…
Cancel
Save