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.

54 lines
1.3 KiB

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