casnode/object/adapter.go

197 lines
3.8 KiB
Go
Raw Normal View History

2020-05-31 23:02:22 +08:00
// Copyright 2020 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 object
import (
2020-11-08 11:06:46 +08:00
"fmt"
2020-05-31 23:02:22 +08:00
"runtime"
2021-08-26 00:22:28 +08:00
"github.com/astaxie/beego"
2020-05-31 23:02:22 +08:00
_ "github.com/go-sql-driver/mysql"
"xorm.io/xorm"
)
2022-08-09 20:10:21 +08:00
var (
adapter *Adapter
CasdoorOrganization string
CasdoorApplication string
)
2020-05-31 23:02:22 +08:00
2020-11-08 11:06:46 +08:00
type Session struct {
SessionKey string `xorm:"char(64) notnull pk"`
SessionData []uint8 `xorm:"blob"`
SessionExpiry int `xorm:"notnull"`
}
2021-11-20 00:29:24 +08:00
func InitConfig() {
err := beego.LoadAppConfig("ini", "../conf/app.conf")
if err != nil {
panic(err)
}
}
2020-05-31 23:02:22 +08:00
func InitAdapter() {
2021-06-01 20:51:13 +08:00
adapter = NewAdapter(beego.AppConfig.String("driverName"), beego.AppConfig.String("dataSourceName"), beego.AppConfig.String("dbName"))
adapter.createTable()
2021-08-26 09:51:30 +08:00
CasdoorOrganization = beego.AppConfig.String("casdoorOrganization")
CasdoorApplication = beego.AppConfig.String("casdoorApplication")
2020-05-31 23:02:22 +08:00
}
// Adapter represents the MySQL adapter for policy storage.
type Adapter struct {
driverName string
dataSourceName string
2021-06-01 20:51:13 +08:00
dbName string
Engine *xorm.Engine
2020-05-31 23:02:22 +08:00
}
// finalizer is the destructor for Adapter.
func finalizer(a *Adapter) {
2021-06-01 20:51:13 +08:00
err := a.Engine.Close()
2020-05-31 23:02:22 +08:00
if err != nil {
panic(err)
}
}
// NewAdapter is the constructor for Adapter.
2021-06-01 20:51:13 +08:00
func NewAdapter(driverName string, dataSourceName string, dbName string) *Adapter {
2020-05-31 23:02:22 +08:00
a := &Adapter{}
a.driverName = driverName
a.dataSourceName = dataSourceName
2021-06-01 20:51:13 +08:00
a.dbName = dbName
2020-05-31 23:02:22 +08:00
// Open the DB, create it if not existed.
a.open()
// Call the destructor when the object is released.
runtime.SetFinalizer(a, finalizer)
return a
}
func (a *Adapter) createDatabase() error {
2021-06-01 20:51:13 +08:00
Engine, err := xorm.NewEngine(a.driverName, a.dataSourceName)
2020-05-31 23:02:22 +08:00
if err != nil {
return err
}
2021-06-01 20:51:13 +08:00
defer Engine.Close()
2020-05-31 23:02:22 +08:00
2021-06-01 20:51:13 +08:00
_, err = Engine.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s default charset utf8 COLLATE utf8_general_ci", a.dbName))
2020-05-31 23:02:22 +08:00
return err
}
func (a *Adapter) open() {
2021-06-01 20:51:13 +08:00
if a.driverName != "postgres" {
if err := a.createDatabase(); err != nil {
panic(err)
}
2020-05-31 23:02:22 +08:00
}
2021-06-01 20:51:13 +08:00
Engine, err := xorm.NewEngine(a.driverName, a.dataSourceName+a.dbName)
2020-05-31 23:02:22 +08:00
if err != nil {
panic(err)
}
2021-06-01 20:51:13 +08:00
a.Engine = Engine
2020-05-31 23:02:22 +08:00
}
func (a *Adapter) close() {
2021-06-01 20:51:13 +08:00
a.Engine.Close()
a.Engine = nil
2020-05-31 23:02:22 +08:00
}
func (a *Adapter) createTable() {
2021-06-01 20:51:13 +08:00
err := a.Engine.Sync2(new(Session))
2020-11-08 11:06:46 +08:00
if err != nil {
panic(err)
}
2021-06-01 20:51:13 +08:00
err = a.Engine.Sync2(new(Topic))
2020-05-31 23:02:22 +08:00
if err != nil {
panic(err)
}
2020-06-01 10:33:16 +08:00
2021-06-01 20:51:13 +08:00
err = a.Engine.Sync2(new(Reply))
2020-06-06 11:21:06 +08:00
if err != nil {
panic(err)
}
2021-06-01 20:51:13 +08:00
err = a.Engine.Sync2(new(Poster))
if err != nil {
panic(err)
}
err = a.Engine.Sync2(new(Translator))
if err != nil {
panic(err)
}
2021-06-01 20:51:13 +08:00
err = a.Engine.Sync2(new(Node))
2020-06-01 10:33:16 +08:00
if err != nil {
panic(err)
}
2020-06-30 12:13:47 +08:00
2021-06-01 20:51:13 +08:00
err = a.Engine.Sync2(new(Favorites))
2020-06-30 12:13:47 +08:00
if err != nil {
panic(err)
}
2020-07-07 21:40:15 +08:00
2021-06-01 20:51:13 +08:00
err = a.Engine.Sync2(new(Tab))
2020-07-07 21:40:15 +08:00
if err != nil {
panic(err)
}
2021-06-01 20:51:13 +08:00
err = a.Engine.Sync2(new(Notification))
if err != nil {
panic(err)
}
2021-06-01 20:51:13 +08:00
err = a.Engine.Sync2(new(BasicInfo))
if err != nil {
panic(err)
}
2021-06-01 20:51:13 +08:00
err = a.Engine.Sync2(new(Plane))
if err != nil {
panic(err)
}
2021-06-01 20:51:13 +08:00
err = a.Engine.Sync2(new(ConsumptionRecord))
if err != nil {
panic(err)
}
2021-06-01 20:51:13 +08:00
err = a.Engine.Sync2(new(BrowseRecord))
if err != nil {
panic(err)
}
2021-06-01 20:51:13 +08:00
err = a.Engine.Sync2(new(UploadFileRecord))
if err != nil {
panic(err)
}
2021-08-21 21:25:08 +08:00
err = a.Engine.Sync2(new(SensitiveWord))
if err != nil {
panic(err)
}
err = a.Engine.Sync2(new(FrontConf))
if err != nil {
panic(err)
}
2020-05-31 23:02:22 +08:00
}