Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

před 9 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package env
  2. /*
  3. env
  4. ===
  5. Ease of Accessing Environment Varaibles.
  6. ## Installation
  7. ```shell
  8. $ go get -v github.com/goanywhere/env
  9. ```
  10. ## Usage
  11. Add the application settings to file `.env` right under the root of your project:
  12. ```shell
  13. MY_SECRET_KEY=YOURSECRETKEY
  14. Case_Will_Be_IgNoreD=YOURSECRETKEYGOESHERE
  15. ```
  16. You can double/single quote string values:
  17. ```shell
  18. PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----HkVN9…-----END DSA PRIVATE KEY-----"
  19. ```
  20. You can use `export` in front of each line just like your shell settings, so that you can `source` the file in your terminal directly:
  21. ```shell
  22. export USERNAME=account@goanywhere.io
  23. export PASSWORD=AccountPasswordGoesHere
  24. ```
  25. All set now, you are good to Go :-)
  26. ``` go
  27. package main
  28. import (
  29. "github.com/goanywhere/env"
  30. "github.com/goanywhere/rex"
  31. )
  32. func index (ctx *rex.Context) {
  33. ctx.HTML("index.html")
  34. }
  35. func main() {
  36. // Override default 5000 port here.
  37. env.Set("port", "9394")
  38. app := rex.New()
  39. app.Get("/", index)
  40. app.Serve()
  41. }
  42. ```
  43. You will now have the HTTP server running on `0.0.0.0:9394`.
  44. `env` supports namespace (case-insensitive, same as the key).
  45. ``` go
  46. import (
  47. "fmt"
  48. "github.com/goanywhere/env"
  49. )
  50. func main() {
  51. env.Set("debug", "false", "production")
  52. fmt.Printf("debug: %s", env.Get("debug", "production"))
  53. }
  54. ```
  55. `env` also supports custom struct for you to access the reflected values (the key is case-insensitive).
  56. ``` go
  57. package main
  58. import (
  59. "fmt"
  60. "github.com/goanywhere/env"
  61. )
  62. type Spec struct {
  63. App string
  64. }
  65. func main() {
  66. var spec Spec
  67. env.Set("app", "myapplication")
  68. env.Map(&spec)
  69. fmt.Printf("App: %s", spec.App) // output: "App: myapplication"
  70. }
  71. ```
  72. We also includes dotenv supports:
  73. ``` text
  74. test1 = value1
  75. test2 = 'value2'
  76. test3 = "value3"
  77. export test4=value4
  78. ```
  79. ``` go
  80. package main
  81. import (
  82. "fmt"
  83. "github.com/goanywhere/env"
  84. )
  85. func main() {
  86. // Load '.env' from current working directory.
  87. env.Load(".env")
  88. fmt.Printf("<test: %s>", env.Get("test")) // output: "value"
  89. fmt.Printf("<test2: %s>", env.Get("test2")) // output: "value2"
  90. }
  91. ```
  92. ## NOTES
  93. Sensitive settings should **ONLY** be accessible on the machines that need access to them. **NEVER** commit them to a repository (even a private one) that is not needed by every development machine and server.
  94. */