|
| 1 | +# StoryBooker adapter for Azure services |
| 2 | + |
| 3 | +Create service adapters for Azure services. |
| 4 | + |
| 5 | +## Auth |
| 6 | + |
| 7 | +The Azure EasyAuth provides quick way to setup auth for Azure Functions |
| 8 | + |
| 9 | +```ts |
| 10 | +import { |
| 11 | + AzureEasyAuthService, |
| 12 | + type AuthServiceAuthorise, |
| 13 | +} from "@storybooker/azure/easy-auth"; |
| 14 | + |
| 15 | +const authorize: AuthServiceAuthorise = async (permission, { user }) => { |
| 16 | + // check permission against user (roles) |
| 17 | + return true; // or false |
| 18 | +}; |
| 19 | +const auth = new AzureEasyAuthService(authorise); |
| 20 | + |
| 21 | +// use as auth in StoryBooker options. |
| 22 | +``` |
| 23 | + |
| 24 | +## Database |
| 25 | + |
| 26 | +Azure provides 2 options which can be used as database for StoryBooker. |
| 27 | + |
| 28 | +### Data Tables |
| 29 | + |
| 30 | +```ts |
| 31 | +import { AzureDataTablesDatabaseService } from "@storybooker/azure/data-tables"; |
| 32 | + |
| 33 | +const connectionString = process.env["AZURE_STORAGE_CONNECTION_STRING"]; |
| 34 | +const database = new AzureDataTablesDatabaseService(connectionString); |
| 35 | + |
| 36 | +// use as database in StoryBooker options. |
| 37 | +``` |
| 38 | + |
| 39 | +### Cosmos DB |
| 40 | + |
| 41 | +```ts |
| 42 | +import { AzureCosmosDatabaseService } from "@storybooker/azure/cosmos-db"; |
| 43 | + |
| 44 | +const connectionString = process.env["AZURE_COSMOS_DB_CONNECTION_STRING"]; |
| 45 | +const database = new AzureCosmosDatabaseService(connectionString); |
| 46 | + |
| 47 | +// use as database in StoryBooker options. |
| 48 | +``` |
| 49 | + |
| 50 | +## Storage |
| 51 | + |
| 52 | +The Azure Storage provides BlobStorage which can be used as storage for StoryBooker. |
| 53 | + |
| 54 | +```ts |
| 55 | +import { AzureBlobStorageService } from "@storybooker/azure/blob-storage"; |
| 56 | + |
| 57 | +const connectionString = process.env["AZURE_STORAGE_CONNECTION_STRING"]; |
| 58 | +const storage = new AzureBlobStorageService(connectionString); |
| 59 | + |
| 60 | +// use as storage in StoryBooker options. |
| 61 | +``` |
| 62 | + |
| 63 | +## Hosting StoryBooker in Azure Functions |
| 64 | + |
| 65 | +> For deploying: |
| 66 | +> |
| 67 | +> - Set Azure Functions runtime to `Node` and version to `22 LTS` or higher. |
| 68 | +> - Set environment variable in deployment for `AzureWebJobsStorage` if not already done. |
| 69 | +
|
| 70 | +Create following files in your Azure Functions project. |
| 71 | + |
| 72 | +### `index.js` |
| 73 | + |
| 74 | +```js |
| 75 | +// @ts-check |
| 76 | + |
| 77 | +import { AzureBlobStorageService } from "@storybooker/azure/blob-storage"; |
| 78 | +import { AzureDataTablesDatabaseService } from "@storybooker/azure/data-tables"; |
| 79 | +import { AzureEasyAuthService } from "@storybooker/azure/easy-auth"; |
| 80 | +import { registerStoryBookerRouter } from "@storybooker/azure/functions"; |
| 81 | + |
| 82 | +const storageConnectionString = process.env["AzureWebJobsStorage"]; |
| 83 | +if (!storageConnectionString) { |
| 84 | + throw new Error( |
| 85 | + `The storage connectionString is required to connect with Azure Storage resource.`, |
| 86 | + ); |
| 87 | +} |
| 88 | + |
| 89 | +registerStoryBookerRouter({ |
| 90 | + auth: new AzureEasyAuthService(), // optional auth adapter |
| 91 | + database: new AzureDataTablesDatabaseService(storageConnectionString), |
| 92 | + storage: new AzureBlobStorageService(storageConnectionString), |
| 93 | +}); |
| 94 | +``` |
| 95 | + |
| 96 | +### `package.json` |
| 97 | + |
| 98 | +```json |
| 99 | +{ |
| 100 | + "name": "your-storybooker", |
| 101 | + "type": "module", |
| 102 | + "main": "index.js", |
| 103 | + "dependencies": { |
| 104 | + "@azure/functions": "^4.0.0", |
| 105 | + "@azure/data-tables": "^13.0.0", |
| 106 | + "@azure/storage-blob": "^12.0.0", |
| 107 | + "@storybooker/azure": "latest" |
| 108 | + } |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +### `host.json` |
| 113 | + |
| 114 | +```json |
| 115 | +{ |
| 116 | + "version": "2.0", |
| 117 | + "extensionBundle": { |
| 118 | + "id": "Microsoft.Azure.Functions.ExtensionBundle", |
| 119 | + "version": "[4.*, 5.0.0)" |
| 120 | + }, |
| 121 | + "extensions": { "http": { "routePrefix": "" } } |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +### `local.settings.json` (for local dev only) |
| 126 | + |
| 127 | +> Must not be committed to source control (git). |
| 128 | +
|
| 129 | +```json |
| 130 | +{ |
| 131 | + "IsEncrypted": false, |
| 132 | + "Values": { |
| 133 | + "FUNCTIONS_WORKER_RUNTIME": "node", |
| 134 | + "AzureWebJobsStorage": "UseDevelopmentStorage=true" |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
0 commit comments