Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as cloud from "@pulumi/cloud";
import * as nodeFetchModule from "node-fetch";
import { canonicalUrl, hostname} from "./support";
import * as express from "express";
// Pending sites to be processed
let sites = new cloud.Topic("examples-sites-to-process");
// Documents and associated metadata for crawled sites
let documents = new cloud.Table("examples-documents");
// Front end API and console
let frontEnd = new cloud.HttpServer("examples-crawler-front-end", () => {
const app = express();
app.post("/queue", async (req, res) => {
let url = req.body.toString();
console.log(`Pushing ${url} to processing queue`);
await sites.publish(url);
res.status(200).json("success");
});
app.get("/documents/stats", async (_, res) => res.json({count: (await documents.scan()).length}));
return app;
});
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
import * as pulumi from "@pulumi/pulumi";
import * as cloud from "@pulumi/cloud";
import * as cache from "./cache";
// Create a web server.
let endpoint = new cloud.HttpEndpoint("urlshortener");
// Create a table `urls`, with `name` as primary key.
let urlTable = new cloud.Table("urls", "name");
// Create a cache of frequently accessed urls.
let urlCache = new cache.Cache("urlcache");
// Serve all files in the www directory to the root.
endpoint.static("/", "www");
// GET /url lists all URLs currently registered.
endpoint.get("/url", async (req, res) => {
try {
let items = await urlTable.scan();
res.status(200).json(items);
console.log(`GET /url retrieved ${items.length} items`);
} catch (err) {
res.status(500).json(err.stack);
console.log(`GET /url error: ${err.stack}`);
var v: TemplateStringsArray
import * as cloud from "@pulumi/cloud";
import { authMiddleware } from "./middleware";
import * as express from "express";
import * as bodyParser from "body-parser";
type AsyncRequestHandler = (req: express.Request, res: express.Response, next: express.NextFunction) => Promise;
const asyncMiddleware = (fn: AsyncRequestHandler) =>
(req: express.Request, res: express.Response, next: express.NextFunction) => {
Promise.resolve(fn(req, res, next)).catch(next);
};
let todos = new cloud.Table("examples-todo");
let server = new cloud.HttpServer("examples-todo", () => {
const app = express();
// Serve all files in the 'www' folder under '/'
// 'index.html' will be automatically served as '/' as well as '/index.html'.
app.use("/", express.static("www"));
// GET/POST todo handlers
app.get("/todo/:id", authMiddleware, asyncMiddleware(async (req, res) => {
console.log("GET /todo/" + req.params["id"]);
try {
let item = await todos.get({ id: req.params["id"] });
res.status(200).json(item.value);
} catch (err) {
res.status(500).json(err);
}
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as cloud from "@pulumi/cloud";
// A poll function takes an opaque token generated from the last execution (or undefined), and
// returns any new items since the last invocation, along with a new token to be used with the
// next invocation.
export type PollFunction = (lastToken?: string) => Promise<{ items: T[]; nextToken: string; }>;
let pollMarkers = new cloud.Table("__pollMarkers");
// poll represents a stream of items which are derived from polling at a given rate
// using a user-provided polling function.
export function poll(name: string, rate: cloud.timer.IntervalRate, poller: PollFunction): cloud.Stream {
let topic = new cloud.Topic(name);
cloud.timer.interval(name, rate, async () => {
console.log(`Starting polling...`);
console.log(`Getting pollMarker for ${name}`);
let pollMarker = await pollMarkers.get({id: name});
console.log(`pollMarker is ${JSON.stringify(pollMarker, null, "")}`);
let lastToken: string | undefined;
if (pollMarker !== undefined) {
lastToken = pollMarker.lastToken;
const FanoutGraphqlAwsApp = (
name: string,
options: IFanoutGraphqlAwsAppOptions,
) => {
const webSocketOverHttpStorage = {
connections: new cloud.Table(`${name}-connections`),
subscriptions: new cloud.Table(`${name}-subscriptions`),
}
// AWS Resources for serving GraphQL API over HTTP
const httpLambdaFunction = new aws.lambda.CallbackFunction(`${name}-fn-graphql`, {
callback: FanoutGraphqlAppLambdaCallback({
grip: options.grip,
pubsub: options.pubsub,
tables: {
notes: new cloud.Table(`${name}-notes`),
...webSocketOverHttpStorage,
},
}),
timeout: 30,
});
const routes: awsx.apigateway.Route[] = [
const FanoutGraphqlAwsApp = (
name: string,
options: IFanoutGraphqlAwsAppOptions,
) => {
const webSocketOverHttpStorage = {
connections: new cloud.Table(`${name}-connections`),
subscriptions: new cloud.Table(`${name}-subscriptions`),
}
// AWS Resources for serving GraphQL API over HTTP
const httpLambdaFunction = new aws.lambda.CallbackFunction(`${name}-fn-graphql`, {
callback: FanoutGraphqlAppLambdaCallback({
grip: options.grip,
pubsub: options.pubsub,
tables: {
notes: new cloud.Table(`${name}-notes`),
...webSocketOverHttpStorage,
},
}),
timeout: 30,
});
const routes: awsx.apigateway.Route[] = [
{
const FanoutGraphqlAwsApp = (
name: string,
options: IFanoutGraphqlAwsAppOptions,
) => {
const webSocketOverHttpStorage = {
connections: new cloud.Table(`${name}-connections`),
subscriptions: new cloud.Table(`${name}-subscriptions`),
}
// AWS Resources for serving GraphQL API over HTTP
const httpLambdaFunction = new aws.lambda.CallbackFunction(`${name}-fn-graphql`, {
callback: FanoutGraphqlAppLambdaCallback({
grip: options.grip,
pubsub: options.pubsub,
tables: {
notes: new cloud.Table(`${name}-notes`),
...webSocketOverHttpStorage,
},
}),
timeout: 30,
});
const routes: awsx.apigateway.Route[] = [
{
eventHandler: httpLambdaFunction,
method: "GET",
path: "/",
},
{
eventHandler: httpLambdaFunction,
method: "POST",
path: "/",
},
constructor(name: string, stream: cloud.Stream) {
this.topic = new cloud.Topic(name);
this.table = new cloud.Table(name);
stream.subscribe(name, async (item) => {
let value = JSON.stringify(item);
console.log(utils.toShortString(`Adding item to digest table: ${value}`));
await this.table.insert({ id: value });
});
this.collect = async () => {
console.log(`Collecting digest...`);
let items = await this.table.scan();
let ret: T[] = [];
for (let item of items) {
ret.push(JSON.parse(item.id));
await this.table.delete({ id: item.id });
console.log(utils.toShortString(`Moved item from table to digest: ${item.id}`));