データ初期化
Casdoorを他のサービスと一緒に完全なアプリケーションとしてデプロイする場合、ユーザーにすぐに使える機能を提供したいかもしれません。 これは、ユーザーが何の設定もせずにアプリケーションを直接使用できることを意味します。
このような状況では、設定ファイルを通じてCasdoorにサービスを登録するためにデータ初期化を使用できます。 このファイルは事前に定義されているか、または独自のサービスによって動的に生成されることがあります。
ここでは、設定データのインポートまたはエクスポートのチュートリアルを提供します。
設定データのインポート
By default, if there is a configuration file named init_data.json at the root directory of Casdoor, it will be used to initialize data in Casdoor. You can also specify a custom path for the initialization file by setting the initDataFile parameter in conf/app.conf:
initDataFile = /path/to/your/init_data.json
If no custom path is specified, Casdoor will look for init_data.json in the root directory where Casdoor runs.
Casdoorの公式Dockerイメージを使用している場合、init_data.jsonをコンテナにマウントするのに役立つスクリプトがいくつかあります。
init_data.jsonのテンプレートはこちらで提供されています:init_data.json.template。 使用する前にinit_data.jsonに名前を変更してください。
Docker用
Dockerを使ってCasdoorをデプロイする場合、volumeコマンドを使用してinit_data.jsonをコンテナにマウントできます。
docker run ... -v /path/to/init_data.json:/init_data.json
Kubernetes用
Kubernetesを使ってCasdoorをデプロイする場合、configmapを使用してinit_data.jsonを保存できます。
apiVersion: v1
kind: ConfigMap
metadata:
name: casdoor-init-data
data:
init_data.json:
Casdoorのpodsにデータをマウントするには、configmapをマウントします。 以下のようにdeploymentを変更できます:
apiVersion: apps/v1
kind: Deployment
...
spec:
template:
...
spec:
containers:
...
volumeMounts:
- mountPath: /init_data.json
name: casdoor-init-data-volume
subPath: init_data.json
volumes:
- configMap:
name: casdoor-init-data
name: casdoor-init-data-volume
設定データのエクスポート
You can export all Casdoor configuration data to a file for backup or migration purposes. There are two methods available:
Using the Binary (Recommended)
If you're running Casdoor from a binary, use the -export flag to dump the database to a JSON file:
# Export to default location (init_data_dump.json)
./casdoor -export
# Export to a custom path
./casdoor -export -exportPath /path/to/backup.json
The export runs after database initialization but before the server starts, then exits automatically. This method works with any deployment method (binary, Docker, Kubernetes) and doesn't require Go toolchain or source code access.
Using Go Test
If you have access to the source code, you can use the test method:
go test ./object -v -run TestDumpToFile
This will generate init_data_dump.json in the same directory.
Migrating Data
After exporting, rename init_data_dump.json to init_data.json and place it in the root directory of your target Casdoor installation. On startup, Casdoor will automatically import the data.
参照
データ初期化によってサポートされるすべてのCasdoorオブジェクトは以下の通りです:
| オブジェクト | Go ストラクト | ドキュメンテーション |
|---|---|---|
| 組織 | struct | doc |
| アプリケーション | struct | doc |
| ユーザー | struct | doc |
| 証明書 | struct | doc |
| プロバイダー | struct | doc |
| LDAP | struct | ドキュメント |
| モデル | ストラクト | |
| 権限 | struct | doc |
| 支払い | struct | ドキュメント |
| 製品 | ストラクト | ドキュメント |
| リソース | ストラクト | ドキュメント |
| ロール | ストラクト | ドキュメント |
| 同期器 | ストラクト | ドキュメント |
| トークン | ストラクト | ドキュメント |
| ウェブフック | ストラクト | ドキュメント |
| グループ | ストラクト | ドキュメント |
| アダプター | ストラクト | ドキュメント |
| エンフォーサー | ストラクト | |
| プラン | struct | doc |
| 価格設定 | struct | ドキュメント |
| 招待状 | ストラクト | ドキュメント |
| 記録 | struct | |
| セッション | ストラクト | |
| サブスクリプション | ストラクト | doc |
| トランザクション | ストラクト |
このテンプレートの記入にまだ混乱している場合は、RESTful APIを呼び出すか、ブラウザのデバッグモードを使用してこれらのオブジェクトに対するGetXXXのレスポンスを確認できます。 レスポンスはinit_data.jsonと同じフォーマットです。