A simple way to handle form submissions from static websites.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.2 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. package main
  2. import (
  3. "errors"
  4. "github.com/caarlos0/env/v6"
  5. )
  6. type config struct {
  7. Port int `env:"PORT" envDefault:"8080"`
  8. HoneyPots []string `env:"HONEYPOTS" envDefault:"_t_email" envSeparator:","`
  9. DefaultRecipient string `env:"EMAIL_TO"`
  10. AllowedRecipients []string `env:"ALLOWED_TO" envSeparator:","`
  11. Sender string `env:"EMAIL_FROM"`
  12. SmtpUser string `env:"SMTP_USER"`
  13. SmtpPassword string `env:"SMTP_PASS"`
  14. SmtpHost string `env:"SMTP_HOST"`
  15. SmtpPort int `env:"SMTP_PORT" envDefault:"587"`
  16. GoogleApiKey string `env:"GOOGLE_API_KEY"`
  17. Blacklist []string `env:"BLACKLIST" envSeparator:"," envDefault:"gambling,casino"`
  18. }
  19. func parseConfig() (config, error) {
  20. cfg := config{}
  21. if err := env.Parse(&cfg); err != nil {
  22. return cfg, errors.New("failed to parse config")
  23. }
  24. return cfg, nil
  25. }
  26. func checkRequiredConfig(cfg config) bool {
  27. if cfg.DefaultRecipient == "" {
  28. return false
  29. }
  30. if len(cfg.AllowedRecipients) < 1 {
  31. return false
  32. }
  33. if cfg.Sender == "" {
  34. return false
  35. }
  36. if cfg.SmtpUser == "" {
  37. return false
  38. }
  39. if cfg.SmtpPassword == "" {
  40. return false
  41. }
  42. if cfg.SmtpHost == "" {
  43. return false
  44. }
  45. return true
  46. }