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.

48 lines
1.1 KiB

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