Zum Hauptinhalt springen

Dateninitialisierung

Wenn Sie Casdoor zusammen mit anderen Diensten als eine komplette Anwendung bereitstellen, möchten Sie den Benutzern möglicherweise eine Out-of-the-Box-Funktion bieten. Das bedeutet, dass Benutzer die Anwendung direkt ohne jegliche Konfiguration verwenden können.

In einer solchen Situation können Sie die Dateninitialisierung verwenden, um Ihren Dienst über eine Konfigurationsdatei in Casdoor zu registrieren. Diese Datei kann vordefiniert sein oder dynamisch von Ihrem eigenen Dienst generiert werden.

Hier geben wir ein Tutorial für das Importieren oder Exportieren von Konfigurationsdaten.

Konfigurationsdaten importieren

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.

Wenn Sie das offizielle Docker-Image von Casdoor verwenden, finden Sie hier einige Skripte, die Ihnen helfen können, init_data.json in den Container einzubinden.

Eine Vorlage für init_data.json wird bereitgestellt unter: init_data.json.template. Benennen Sie es in init_data.json um, bevor Sie es verwenden.

Für Docker

Wenn Sie Casdoor mit Docker bereitstellen, können Sie den Befehl volume verwenden, um init_data.json in den Container einzubinden.

docker run ... -v /path/to/init_data.json:/init_data.json

Für Kubernetes

Wenn Sie Casdoor mit Kubernetes bereitstellen, können Sie die configmap verwenden, um init_data.json zu speichern.

apiVersion: v1
kind: ConfigMap
metadata:
name: casdoor-init-data
data:
init_data.json:

Sie können die Daten in Casdoor pods einbinden, indem Sie die configmap mounten. Sie können Ihre deployment wie folgt ändern:

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

Konfigurationsdaten exportieren

You can export all Casdoor configuration data to a file for backup or migration purposes. There are two methods available:

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.

Referenzen

Alle von der Dateninitialisierung unterstützten Casdoor-Objekte sind wie folgt:

ObjektGo-StrukturDokumentation
Organisationenstructdoc
Anwendungenstructdoc
Benutzerstructdoc
Zertifikatestructdoc
Anbieterstructdoc
ldapsstructDokumentation
ModelleStruktur
Berechtigungenstructdoc
ZahlungenstructDokumentation
ProdukteStrukturDokumentation
RessourcenStrukturDokumentation
RollenStrukturDokumentation
SynchronisiererStrukturDokumentation
TokenStrukturDokumentation
WebhooksStrukturDokumentation
GruppenStrukturDokumentation
AdapterStrukturDokumentation
DurchsetzerStruktur
Plänestructdoc
PreisgestaltungstructDokumentation
EinladungenStrukturDokumentation
Aufzeichnungenstruct
SitzungenStruktur
AbonnementsStrukturdoc
TransaktionenStruktur

Wenn Sie immer noch verwirrt sind, wie Sie diese Vorlage ausfüllen sollen, können Sie die RESTful-API aufrufen oder den Debug-Modus Ihres Browsers verwenden, um die Antwort von GetXXX auf diese Objekte zu sehen. Die Antworten sind im gleichen Format wie init_data.json.