Skip to content

Commit

Permalink
Merge pull request pulumi#357 from pulumi/mikhailshilkov/fix-url-shor…
Browse files Browse the repository at this point in the history
…tener

Fix the URL shortener example
  • Loading branch information
stack72 authored Aug 6, 2019
2 parents 733582d + 093e48e commit 21a5ad2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
9 changes: 3 additions & 6 deletions azure-ts-serverless-url-shortener-global/cosmosclient.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import * as cosmos from "@azure/cosmos";

export async function getContainer(endpoint: string, masterKey: string, region: string) {
let connectionPolicy = new cosmos.ConnectionPolicy();
connectionPolicy.PreferredLocations = [region];

const client = new cosmos.CosmosClient({
endpoint,
auth: { masterKey: masterKey },
connectionPolicy,
key: masterKey,
connectionPolicy: { preferredLocations: [region] },
});

const { database: db } = await client.databases.createIfNotExists({ id: "thedb" });
const db = client.database("thedb");
const { container } = await db.containers.createIfNotExists({ id: "urls" });
return container;
}
42 changes: 24 additions & 18 deletions azure-ts-serverless-url-shortener-global/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import * as pulumi from "@pulumi/pulumi";
import * as azure from "@pulumi/azure";
import { Container } from "@azure/cosmos";
import * as azure from "@pulumi/azure";
import * as pulumi from "@pulumi/pulumi";
import { getContainer } from "./cosmosclient";

const config = new pulumi.Config();
// Read a list of target locations from the config file:
// Expecting a comma-separated list, e.g., "westus,eastus,westeurope"
const locations = config.require("locations").split(',');
const locations = config.require("locations").split(",");
// The first location is considered primary
const primaryLocation = locations[0];

let resourceGroup = new azure.core.ResourceGroup("UrlShorterner", {
const resourceGroup = new azure.core.ResourceGroup("UrlShorterner", {
location: primaryLocation,
});

// Cosmos DB with a single write region (primary location) and multiple read replicas
let cosmosdb = new azure.cosmosdb.Account("UrlStore", {
const cosmosdb = new azure.cosmosdb.Account("UrlStore", {
resourceGroupName: resourceGroup.name,
location: primaryLocation,
geoLocations: locations.map((location, failoverPriority) => ({ location, failoverPriority })),
Expand All @@ -27,19 +27,26 @@ let cosmosdb = new azure.cosmosdb.Account("UrlStore", {
},
});

// Define a database under the Cosmos DB Account
const database = new azure.cosmosdb.SqlDatabase("Database", {
resourceGroupName: resourceGroup.name,
accountName: cosmosdb.name,
name: "thedb",
});

// Traffic Manager as a global HTTP endpoint
const profile = new azure.trafficmanager.Profile("UrlShortEndpoint", {
resourceGroupName: resourceGroup.name,
trafficRoutingMethod: 'Performance',
trafficRoutingMethod: "Performance",
dnsConfigs: [{
// Subdomain must be globally unique, so we default it with the full resource group name
relativeName: resourceGroup.name,
ttl: 60,
}],
monitorConfigs: [{
protocol: 'HTTP',
protocol: "HTTP",
port: 80,
path: '/api/ping',
path: "/api/ping",
}]
});

Expand All @@ -57,7 +64,7 @@ const fn = new azure.appservice.HttpEventSubscription("AddUrl", {
container = container || await getContainer(endpoint, masterKey, primaryLocation);

await container.items.create(request.body);
return { status: 200, body: 'Short URL saved' };
return { status: 200, body: "Short URL saved" };
};
}
});
Expand All @@ -78,23 +85,22 @@ for (const location of locations) {
return async (_, request: azure.appservice.HttpRequest) => {
container = container || await getContainer(endpoint, masterKey, location);

const key = request.params['key'];
if (key === 'ping') {
const key = request.params["key"];
if (key === "ping") {
// Handle traffic manager live pings
return { status: 200, body: 'Ping ACK' };
return { status: 200, body: "Ping ACK" };
}

try {
const response = await container.item(key.toString()).read();

return response.body && response.body.url
const response = await container.item(key, undefined).read();
return response.resource && response.resource.url
// HTTP redirect for known URLs
? { status: 301, headers: { "location": response.body.url }, body: '' }
? { status: 301, headers: { location: response.resource.url }, body: "" }
// 404 for malformed documents
: { status: 404, body: '' };
: { status: 404, body: "" };
} catch (e) {
// Cosmos SDK throws an error for non-existing documents
return { status: 404, body: '' }
return { status: 404, body: "" }
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion azure-ts-serverless-url-shortener-global/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"typescript": "^3.1.1"
},
"dependencies": {
"@azure/cosmos": "latest",
"@azure/cosmos": "^3.1.0",
"@pulumi/pulumi": "latest",
"@pulumi/azure": "latest"
}
Expand Down

0 comments on commit 21a5ad2

Please sign in to comment.