You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

conf.go 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2023 The casbin Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package conf
  15. import (
  16. "os"
  17. "runtime"
  18. "strconv"
  19. "strings"
  20. "github.com/astaxie/beego"
  21. )
  22. func init() {
  23. // this array contains the beego configuration items that may be modified via env
  24. presetConfigItems := []string{"httpport", "appname"}
  25. for _, key := range presetConfigItems {
  26. if value, ok := os.LookupEnv(key); ok {
  27. err := beego.AppConfig.Set(key, value)
  28. if err != nil {
  29. panic(err)
  30. }
  31. }
  32. }
  33. }
  34. func GetConfigString(key string) string {
  35. if value, ok := os.LookupEnv(key); ok {
  36. return value
  37. }
  38. res := beego.AppConfig.String(key)
  39. if res == "" {
  40. if key == "staticBaseUrl" {
  41. res = "https://cdn.casbin.org"
  42. } else if key == "logConfig" {
  43. res = "{\"filename\": \"logs/casdoor.log\", \"maxdays\":99999, \"perm\":\"0770\"}"
  44. }
  45. }
  46. return res
  47. }
  48. func GetConfigBool(key string) bool {
  49. value := GetConfigString(key)
  50. if value == "true" {
  51. return true
  52. } else {
  53. return false
  54. }
  55. }
  56. func GetConfigInt64(key string) (int64, error) {
  57. value := GetConfigString(key)
  58. num, err := strconv.ParseInt(value, 10, 64)
  59. return num, err
  60. }
  61. func GetConfigDataSourceName() string {
  62. dataSourceName := GetConfigString("dataSourceName")
  63. runningInDocker := os.Getenv("RUNNING_IN_DOCKER")
  64. if runningInDocker == "true" {
  65. // https://stackoverflow.com/questions/48546124/what-is-linux-equivalent-of-host-docker-internal
  66. if runtime.GOOS == "linux" {
  67. dataSourceName = strings.ReplaceAll(dataSourceName, "localhost", "172.17.0.1")
  68. } else {
  69. dataSourceName = strings.ReplaceAll(dataSourceName, "localhost", "host.docker.internal")
  70. }
  71. }
  72. return dataSourceName
  73. }
  74. func GetLanguage(language string) string {
  75. if language == "" || language == "*" {
  76. return "en"
  77. }
  78. if len(language) != 2 || language == "nu" {
  79. return "en"
  80. } else {
  81. return language
  82. }
  83. }
  84. func IsDemoMode() bool {
  85. return strings.ToLower(GetConfigString("isDemoMode")) == "true"
  86. }
  87. func GetConfigBatchSize() int {
  88. res, err := strconv.Atoi(GetConfigString("batchSize"))
  89. if err != nil {
  90. res = 100
  91. }
  92. return res
  93. }
  94. func GetConfigRealDataSourceName(driverName string) string {
  95. var dataSourceName string
  96. if driverName != "mysql" {
  97. dataSourceName = GetConfigDataSourceName()
  98. } else {
  99. dataSourceName = GetConfigDataSourceName() + GetConfigString("dbName")
  100. }
  101. return dataSourceName
  102. }