feat: support configuring the database using environment variables (#577)

Signed-off-by: yehong <239859435@qq.com>
This commit is contained in:
yehong 2023-06-27 15:57:06 +08:00 committed by GitHub
parent 2dee05c7f3
commit 74e0fd937b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 150 additions and 43 deletions

View File

@ -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)

View File

@ -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.

106
conf/conf.go Normal file
View File

@ -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
}

View File

@ -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)
}

View File

@ -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.

View File

@ -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")
}

View File

@ -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

View File

@ -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.

View File

@ -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)

View File

@ -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{
{

View File

@ -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)

View File

@ -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{}
}

View File

@ -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 {

View File

@ -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)
}

View File

@ -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
}

View File

@ -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
}