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.

47 lines
1.1 KiB

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. }
  17. func parseConfig() (config, error) {
  18. cfg := config{}
  19. if err := env.Parse(&cfg); err != nil {
  20. return cfg, errors.New("failed to parse config")
  21. }
  22. return cfg, nil
  23. }
  24. func checkRequiredConfig(cfg config) bool {
  25. if cfg.DefaultRecipient == "" {
  26. return false
  27. }
  28. if len(cfg.AllowedRecipients) < 1 {
  29. return false
  30. }
  31. if cfg.Sender == "" {
  32. return false
  33. }
  34. if cfg.SmtpUser == "" {
  35. return false
  36. }
  37. if cfg.SmtpPassword == "" {
  38. return false
  39. }
  40. if cfg.SmtpHost == "" {
  41. return false
  42. }
  43. return true
  44. }