数据初始化
如果你正在将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:
您可以通过挂载 configmap 将数据挂载到Casdoor的pods中。 您可以按照以下方式修改您的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 |
| ldaps | struct | 文档 |
| models | struct | |
| permissions | struct | doc |
| payments | struct | 文档 |
| products | struct | 文档 |
| resources | struct | 文档 |
| roles | struct | 文档 |
| syncers | struct | 文档 |
| tokens | struct | 文档 |
| webhooks | struct | 文档 |
| groups | struct | 文档 |
| adapters | struct | 文档 |
| enforcers | struct | |
| plans | struct | doc |
| pricings | struct | 文档 |
| invitations | struct | 文档 |
| records | struct | |
| sessions | struct | |
| subscriptions | struct | doc |
| transactions | struct |
如果你对填写这个模板仍然感到困惑,你可以调用RESTful API,或者使用浏览器的调试模式来查看GetXXX对这些对象的响应。 响应的格式与init_data.json相同。