This Go application demonstrates data validation and storage using Dapr and Azure services. It validates data for individuals and organizations and stores it in both a Dapr state store and Azure Cosmos DB.
The application defines two data structures: Individual
and Organization
. It performs validation based on predefined rules:
- For an
Individual
, the name must be present, and the personal number must be an 11-digit number. - For an
Organization
, both the name and organization number must be present.
After validation, the data is stored in a Dapr state store and Azure Cosmos DB.
- Dapr CLI
- Go programming environment
- Azure account and Azure Key Vault setup
- Azure Cosmos DB account
Individual
andOrganization
structs: Represent data structures for individuals and organizations.IsValid
methods: Validate the data according to specified rules.saveData
function: Handles the storage of validated data in the Dapr state store.getKeyVaultToken
function: Retrieves secrets from Azure Key Vault.getCosmosDBClient
andstoreDataInCosmosDB
functions: Handle connections and data storage in Azure Cosmos DB.
To run this application:
- Ensure Dapr is initialized and running.
- Configure Azure Key Vault with necessary secrets for Cosmos DB.
- Set appropriate environment variables for Azure authentication.
- Execute the following command:
dapr run --app-id myapp --dapr-http-port 3500 go run main.go
The validation logic within the IsValid
methods of the Individual
and Organization
structs ensures adherence to the required format before storage.
The following flowchart describes the workflow of the application:
flowchart LR
A[Start] --> B[Validate Data]
B -->|Valid| C[Save in Dapr State Store]
B -->|Invalid| D[Reject Data]
C --> E[Store in Azure Cosmos DB]
E --> F[End]
Dapr is used for state management. The saveData
function communicates with the Dapr sidecar to store data in the configured state store.
- Azure Key Vault: Used to securely store and access tokens and credentials.
- Azure Cosmos DB: Serves as the data storage solution, where validated data is stored.
Future versions of this application may include:
- Enhanced validation rules.
- Integration with additional Dapr components and Azure services.
- Scalability and performance improvements.