diff --git a/Tag_test.go b/Tag_test.go index cd547a89..f94cb4ad 100644 --- a/Tag_test.go +++ b/Tag_test.go @@ -17,7 +17,7 @@ package main import ( "testing" - "github.com/astaxie/beego" + "github.com/casbin/casnode/conf" "github.com/casbin/casnode/object" "github.com/casbin/casnode/service" ) @@ -26,7 +26,7 @@ var adapter *object.Adapter func TestTopicTag(t *testing.T) { topics := []*object.Topic{} - adapter = object.NewAdapter(beego.AppConfig.String("driverName"), beego.AppConfig.String("dataSourceName"), beego.AppConfig.String("dbName")) + adapter = object.NewAdapter(conf.GetConfigString("driverName"), conf.GetConfigString("dataSourceName"), conf.GetConfigString("dbName")) err := adapter.Engine.Table("topic").Find(&topics) if err != nil { panic(err) diff --git a/casdoor/adapter.go b/casdoor/adapter.go index 3345c884..a75339ba 100644 --- a/casdoor/adapter.go +++ b/casdoor/adapter.go @@ -17,7 +17,7 @@ package casdoor import ( "runtime" - "github.com/astaxie/beego" + "github.com/casbin/casnode/conf" _ "github.com/go-sql-driver/mysql" "xorm.io/xorm" ) @@ -34,14 +34,14 @@ type Session struct { } func InitCasdoorAdapter() { - casdoorDbName := beego.AppConfig.String("casdoorDbName") + casdoorDbName := conf.GetConfigString("casdoorDbName") if casdoorDbName == "" { return } - adapter = NewAdapter(beego.AppConfig.String("driverName"), beego.AppConfig.String("dataSourceName"), beego.AppConfig.String("casdoorDbName")) + adapter = NewAdapter(conf.GetConfigString("driverName"), conf.GetConfigDataSourceName(), conf.GetConfigString("casdoorDbName")) - CasdoorOrganization = beego.AppConfig.String("casdoorOrganization") + CasdoorOrganization = conf.GetConfigString("casdoorOrganization") } // Adapter represents the MySQL adapter for policy storage. diff --git a/conf/conf.go b/conf/conf.go new file mode 100644 index 00000000..a18fec1c --- /dev/null +++ b/conf/conf.go @@ -0,0 +1,106 @@ +// Copyright 2023 The Casbin Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package conf + +import ( + "fmt" + "os" + "runtime" + "strconv" + "strings" + + "github.com/astaxie/beego" +) + +func GetConfigString(key string) string { + if value, ok := os.LookupEnv(key); ok { + return value + } + + res := beego.AppConfig.String(key) + if res == "" { + if key == "staticBaseUrl" { + res = "https://cdn.casbin.org" + } + } + + return res +} + +func GetConfigBool(key string) (bool, error) { + value := GetConfigString(key) + if value == "true" { + return true, nil + } else if value == "false" { + return false, nil + } + return false, fmt.Errorf("value %s cannot be converted into bool", value) +} + +func GetConfigInt64(key string) (int64, error) { + value := GetConfigString(key) + num, err := strconv.ParseInt(value, 10, 64) + return num, err +} + +func GetConfigDataSourceName() string { + dataSourceName := GetConfigString("dataSourceName") + + runningInDocker := os.Getenv("RUNNING_IN_DOCKER") + if runningInDocker == "true" { + // https://stackoverflow.com/questions/48546124/what-is-linux-equivalent-of-host-docker-internal + if runtime.GOOS == "linux" { + dataSourceName = strings.ReplaceAll(dataSourceName, "localhost", "172.17.0.1") + } else { + dataSourceName = strings.ReplaceAll(dataSourceName, "localhost", "host.docker.internal") + } + } + + return dataSourceName +} + +func GetLanguage(language string) string { + if language == "" || language == "*" { + return "en" + } + + if len(language) < 2 { + return "en" + } else { + return language[0:2] + } +} + +func IsDemoMode() bool { + return strings.ToLower(GetConfigString("isDemoMode")) == "true" +} + +func GetConfigBatchSize() int { + res, err := strconv.Atoi(GetConfigString("batchSize")) + if err != nil { + res = 100 + } + return res +} + +func GetConfigRealDataSourceName(driverName string) string { + var dataSourceName string + if driverName != "mysql" { + dataSourceName = GetConfigDataSourceName() + } else { + dataSourceName = GetConfigDataSourceName() + GetConfigString("dbName") + } + return dataSourceName +} diff --git a/controllers/account.go b/controllers/account.go index 8b51a2b7..cf02b4ec 100644 --- a/controllers/account.go +++ b/controllers/account.go @@ -18,7 +18,7 @@ import ( _ "embed" "strings" - "github.com/astaxie/beego" + "github.com/casbin/casnode/conf" "github.com/casbin/casnode/object" "github.com/casbin/casnode/util" "github.com/casdoor/casdoor-go-sdk/casdoorsdk" @@ -32,11 +32,11 @@ func init() { } func InitAuthConfig() { - casdoorEndpoint := strings.TrimRight(beego.AppConfig.String("casdoorEndpoint"), "/") - clientId := beego.AppConfig.String("clientId") - clientSecret := beego.AppConfig.String("clientSecret") - casdoorOrganization := beego.AppConfig.String("casdoorOrganization") - casdoorApplication := beego.AppConfig.String("casdoorApplication") + casdoorEndpoint := strings.TrimRight(conf.GetConfigString("casdoorEndpoint"), "/") + clientId := conf.GetConfigString("clientId") + clientSecret := conf.GetConfigString("clientSecret") + casdoorOrganization := conf.GetConfigString("casdoorOrganization") + casdoorApplication := conf.GetConfigString("casdoorApplication") casdoorsdk.InitConfig(casdoorEndpoint, clientId, clientSecret, JwtPublicKey, casdoorOrganization, casdoorApplication) } diff --git a/discuzx/adapter.go b/discuzx/adapter.go index 0d219883..60d0bf1b 100644 --- a/discuzx/adapter.go +++ b/discuzx/adapter.go @@ -17,7 +17,7 @@ package discuzx import ( "runtime" - "github.com/astaxie/beego" + "github.com/casbin/casnode/conf" _ "github.com/go-sql-driver/mysql" "xorm.io/xorm" ) @@ -25,7 +25,7 @@ import ( var adapter *Adapter func InitAdapter() { - adapter = NewAdapter(beego.AppConfig.String("driverName"), beego.AppConfig.String("dataSourceName"), dbName) + adapter = NewAdapter(conf.GetConfigString("driverName"), conf.GetConfigString("dataSourceName"), dbName) } // Adapter represents the MySQL adapter for policy storage. diff --git a/discuzx/casdoor_init.go b/discuzx/casdoor_init.go index bea3ccb7..83758f48 100644 --- a/discuzx/casdoor_init.go +++ b/discuzx/casdoor_init.go @@ -15,7 +15,6 @@ package discuzx import ( - "github.com/astaxie/beego" "github.com/casbin/casnode/object" ) @@ -27,6 +26,6 @@ var ( func init() { object.InitConfig() - CasdoorOrganization = beego.AppConfig.String("casdoorOrganization") - CasdoorApplication = beego.AppConfig.String("casdoorApplication") + CasdoorOrganization = conf.GetConfigString("casdoorOrganization") + CasdoorApplication = conf.GetConfigString("casdoorApplication") } diff --git a/main.go b/main.go index 974da00f..ff349377 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,7 @@ import ( "github.com/astaxie/beego/plugins/cors" _ "github.com/astaxie/beego/session/redis" "github.com/casbin/casnode/casdoor" + "github.com/casbin/casnode/conf" "github.com/casbin/casnode/object" "github.com/casbin/casnode/routers" "github.com/casbin/casnode/service" @@ -53,12 +54,12 @@ func main() { beego.InsertFilter("*", beego.BeforeRouter, routers.Static) beego.InsertFilter("*", beego.BeforeRouter, routers.AutoSigninFilter) - if beego.AppConfig.String("redisEndpoint") == "" { + if conf.GetConfigString("redisEndpoint") == "" { beego.BConfig.WebConfig.Session.SessionProvider = "file" beego.BConfig.WebConfig.Session.SessionProviderConfig = "./tmp" } else { beego.BConfig.WebConfig.Session.SessionProvider = "redis" - beego.BConfig.WebConfig.Session.SessionProviderConfig = beego.AppConfig.String("redisEndpoint") + beego.BConfig.WebConfig.Session.SessionProviderConfig = conf.GetConfigString("redisEndpoint") } beego.BConfig.WebConfig.Session.SessionGCMaxLifetime = 3600 * 24 * 30 diff --git a/object/adapter.go b/object/adapter.go index 24c7c8bf..f43790db 100644 --- a/object/adapter.go +++ b/object/adapter.go @@ -19,6 +19,7 @@ import ( "runtime" "github.com/astaxie/beego" + "github.com/casbin/casnode/conf" _ "github.com/go-sql-driver/mysql" "xorm.io/xorm" ) @@ -43,11 +44,11 @@ func InitConfig() { } func InitAdapter() { - adapter = NewAdapter(beego.AppConfig.String("driverName"), beego.AppConfig.String("dataSourceName"), beego.AppConfig.String("dbName")) + adapter = NewAdapter(conf.GetConfigString("driverName"), conf.GetConfigDataSourceName(), conf.GetConfigString("dbName")) adapter.createTable() - CasdoorOrganization = beego.AppConfig.String("casdoorOrganization") - CasdoorApplication = beego.AppConfig.String("casdoorApplication") + CasdoorOrganization = conf.GetConfigString("casdoorOrganization") + CasdoorApplication = conf.GetConfigString("casdoorApplication") } // Adapter represents the MySQL adapter for policy storage. diff --git a/object/avatar.go b/object/avatar.go index 4e446524..de656856 100644 --- a/object/avatar.go +++ b/object/avatar.go @@ -17,10 +17,10 @@ package object import ( "fmt" - "github.com/astaxie/beego" + "github.com/casbin/casnode/conf" ) -var CasdoorStorageEndpoint = beego.AppConfig.String("casdoorStorageEndpoint") +var CasdoorStorageEndpoint = conf.GetConfigString("casdoorStorageEndpoint") func getUserAvatar(username string) string { return fmt.Sprintf("%scasdoor/avatar/%s/%s.png", CasdoorStorageEndpoint, CasdoorOrganization, username) diff --git a/object/conf.go b/object/conf.go index 6d9da469..8c462692 100644 --- a/object/conf.go +++ b/object/conf.go @@ -14,7 +14,7 @@ package object -import "github.com/astaxie/beego" +import "github.com/casbin/casnode/conf" var ( DefaultPageNum = 20 @@ -45,8 +45,8 @@ var ( DefaultTopTopicTime = 10 // minutes OnlineMemberExpiedTime = 10 // minutes DefaultUploadFileQuota = 50 - Domain = beego.AppConfig.String("domain") // domain - AutoSyncPeriodSecond = -1 // auto sync is disabled if < 30 + Domain = conf.GetConfigString("domain") // domain + AutoSyncPeriodSecond = -1 // auto sync is disabled if < 30 DefaultCronJobs = []*CronJob{ { diff --git a/object/notification.go b/object/notification.go index 15833e2f..8e954409 100644 --- a/object/notification.go +++ b/object/notification.go @@ -19,7 +19,7 @@ import ( "strconv" "sync" - "github.com/astaxie/beego" + "github.com/casbin/casnode/conf" "github.com/casbin/casnode/service" "github.com/casbin/casnode/util" ) @@ -308,12 +308,12 @@ func AddTopicNotification(objectId int, author, content string) { func sendRemindMail(title string, content string, topicId string, sender string, receiver string, domain string) error { fromName := "" - conf := GetFrontConfById("forumName") - if conf != nil { - fromName = conf.Value + frontConfig := GetFrontConfById("forumName") + if frontConfig != nil { + fromName = frontConfig.Value } if fromName == "" { - fromName = beego.AppConfig.String("appname") + fromName = conf.GetConfigString("appname") } return service.SendRemindMail(fromName, title, content, topicId, sender, receiver, domain) diff --git a/object/proxy.go b/object/proxy.go index 55c147b0..3e24d5c9 100644 --- a/object/proxy.go +++ b/object/proxy.go @@ -20,7 +20,7 @@ import ( "net/http" "time" - "github.com/astaxie/beego" + "github.com/casbin/casnode/conf" "golang.org/x/net/proxy" ) @@ -48,7 +48,7 @@ func isAddressOpen(address string) bool { } func getProxyHttpClient() *http.Client { - httpProxy := beego.AppConfig.String("httpProxy") + httpProxy := conf.GetConfigString("httpProxy") if httpProxy == "" { return &http.Client{} } diff --git a/object/reply.go b/object/reply.go index 71314605..07a93d83 100644 --- a/object/reply.go +++ b/object/reply.go @@ -18,7 +18,7 @@ import ( "fmt" "time" - "github.com/astaxie/beego" + "github.com/casbin/casnode/conf" "github.com/casdoor/casdoor-go-sdk/casdoorsdk" ) @@ -39,7 +39,7 @@ type Reply struct { GitterMessageId string `xorm:"varchar(100)" json:"gitterMessageId"` } -var enableNestedReply, _ = beego.AppConfig.Bool("enableNestedReply") +var enableNestedReply, _ = conf.GetConfigBool("enableNestedReply") // GetReplyCount returns all replies num so far, both deleted and not deleted. func GetReplyCount() int { diff --git a/object/util.go b/object/util.go index 1261ed32..c2360b60 100644 --- a/object/util.go +++ b/object/util.go @@ -17,7 +17,7 @@ package object import ( "strconv" - "github.com/astaxie/beego" + "github.com/casbin/casnode/conf" "github.com/casdoor/casdoor-go-sdk/casdoorsdk" ) @@ -42,7 +42,7 @@ func SetUserField(user *casdoorsdk.User, field string, value string) { } func getInitScore() int { - score, err := strconv.Atoi(beego.AppConfig.String("initScore")) + score, err := strconv.Atoi(conf.GetConfigString("initScore")) if err != nil { panic(err) } diff --git a/routers/filter_ssr.go b/routers/filter_ssr.go index a3e7351e..347e5bc9 100644 --- a/routers/filter_ssr.go +++ b/routers/filter_ssr.go @@ -10,8 +10,8 @@ import ( "sync" "time" - "github.com/astaxie/beego" "github.com/astaxie/beego/context" + "github.com/casbin/casnode/conf" ) // var chromeCtx ctx.Context @@ -69,11 +69,11 @@ func InitChromeDp() { isChromeInit = true isChromeInstalled = isChromeFound() if isChromeInstalled { - chromeCtxNum, _ := beego.AppConfig.Int("chromeCtxNum") + chromeCtxNum, _ := conf.GetConfigInt64("chromeCtxNum") if chromeCtxNum <= 0 { chromeCtxNum = 1 // default } - chromeCtxPool = NewSsrPool(chromeCtxNum) + chromeCtxPool = NewSsrPool(int(chromeCtxNum)) } go chromeCtxPool.Run() // start ssr_pool } diff --git a/routers/ssr_pool.go b/routers/ssr_pool.go index f1591b20..c415e23c 100644 --- a/routers/ssr_pool.go +++ b/routers/ssr_pool.go @@ -8,9 +8,9 @@ import ( "sync" "time" - "github.com/astaxie/beego" "github.com/astaxie/beego/context" "github.com/astaxie/beego/logs" + "github.com/casbin/casnode/conf" "github.com/chromedp/chromedp" ) @@ -49,7 +49,7 @@ func NewSsrPool(cap int) *SsrPool { } func render(chromeCtx ctx.Context, url string) (string, error) { - cacheExpireSeconds, err := beego.AppConfig.Int64("cacheExpireSeconds") + cacheExpireSeconds, err := conf.GetConfigInt64("cacheExpireSeconds") if err != nil { return "", err }