env.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package env
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "os"
  7. "sort"
  8. "github.com/joho/godotenv"
  9. )
  10. const KEIJI_USERNAME = "KEIJI_USERNAME"
  11. const KEIJI_PASSWORD = "KEIJI_PASSWORD"
  12. const IMAGE_STORE = "IMAGE_STORE"
  13. const WEB_ROOT = "WEB_ROOT"
  14. const DOMAIN_NAME = "DOMAIN_NAME"
  15. const HOST_PORT = "HOST_PORT"
  16. const HOST_ADDR = "HOST_ADDR"
  17. const USE_SSL = "USE_SSL"
  18. const CHAIN = "CHAIN"
  19. const KEY = "KEY"
  20. const SQLITE_PATH = "SQLITE_PATH"
  21. // SQLitePath returns the configured database path, preserving the historical
  22. // working-directory default when no path is configured.
  23. func SQLitePath() string {
  24. if p := os.Getenv(SQLITE_PATH); p != "" {
  25. return p
  26. }
  27. return "sqlite.db"
  28. }
  29. var OPTION_VARS = map[string]string{
  30. SQLITE_PATH: "#SQLite database path (default: sqlite.db in the working directory)",
  31. IMAGE_STORE: "#the location for keiji to store the images uploaded (string)",
  32. WEB_ROOT: "#the location to pull HTML and various web assets from. Only if using 'keiji -content fs' (string)",
  33. CHAIN: "#the path to the SSL public key chain (string)",
  34. KEY: "#the path to the SSL private key (string)",
  35. }
  36. var REQUIRED_VARS = map[string]string{
  37. HOST_PORT: "#the port to run the server on (int)",
  38. HOST_ADDR: "#the address for the server to listen on (string)",
  39. DOMAIN_NAME: "#the servers domain name, i.e. 'aetherial.dev', or 'localhost' (string)",
  40. USE_SSL: "#chose to use SSL or not (boolean)",
  41. KEIJI_USERNAME: "#the administrator username to login with",
  42. KEIJI_PASSWORD: "#the password for the administrator accounit",
  43. }
  44. type EnvNotSet struct {
  45. NotSet string
  46. }
  47. func (e *EnvNotSet) Error() string {
  48. return fmt.Sprintf("Environment variable: '%s' was not set.", e.NotSet)
  49. }
  50. /*
  51. Write out a blank .env configuration with the the required configuration (uncommented) and the
  52. optional configuration (commented out)
  53. :param path: the path to write the template to
  54. */
  55. func WriteTemplate(wtr io.Writer) error {
  56. outReqArr := make([]string, len(REQUIRED_VARS))
  57. outOptVar := make([]string, len(OPTION_VARS))
  58. i := 0
  59. for k := range REQUIRED_VARS {
  60. outReqArr[i] = k
  61. i++
  62. }
  63. i = 0
  64. for k := range OPTION_VARS {
  65. outOptVar[i] = k
  66. i++
  67. }
  68. sort.Strings(outReqArr)
  69. sort.Strings(outOptVar)
  70. var out string
  71. out = out + "####### Required Configuration #######\n"
  72. for i := range outReqArr {
  73. k := REQUIRED_VARS[outReqArr[i]]
  74. v := outReqArr[i]
  75. fmt.Println(k, v)
  76. out = out + fmt.Sprintf("%s\n%s=\n", v, k)
  77. }
  78. out = out + "\n####### Optional Configuration #######\n"
  79. for i := range outOptVar {
  80. out = out + fmt.Sprintf("# %s\n# %s=\n", OPTION_VARS[outOptVar[i]], outOptVar[i])
  81. }
  82. msg := []byte(out)
  83. _, err := io.Copy(wtr, bytes.NewBuffer(msg))
  84. if err != nil {
  85. return err
  86. }
  87. return nil
  88. }
  89. /*
  90. verify all environment vars passed in are set
  91. :param vars: array of strings to verify
  92. */
  93. func LoadAndVerifyEnv(path string, vars map[string]string) []string {
  94. err := godotenv.Load(path)
  95. if err != nil {
  96. fmt.Printf("Non-fatal error raised: %s\n", err.Error())
  97. }
  98. missing := []string{}
  99. for k := range vars {
  100. if os.Getenv(k) == "" {
  101. missing = append(missing, k)
  102. }
  103. }
  104. return missing
  105. }