import * as pulumi from "@pulumi/pulumi" ; import * as cloud from "@pulumi/cloud" ; import { Config , Output } from "@pulumi/pulumi" ; let nginx = new cloud . Service ( "examples-nginx" , { containers : { nginx : { image : "nginx" , memory : 128 , ports : [ { port : 80 } ] , } , } , replicas : 2 , } ) ; export let nginxEndpoint = nginx . defaultEndpoint ; let simpleNginx = new cloud . Service ( "examples-simple-nginx" , { image : "nginx" , memory : 128 , ports : [ { port : 80 } ] , replicas : 2 , } ) ; export let simpleNginxEndpoint = simpleNginx . defaultEndpoint ; let cachedNginx = new cloud . Service ( "examples-cached-nginx" , { containers : { nginx : { build : { context : "./app" , cacheFrom : true , } , memory : 128 , ports : [ { port : 80 } ] , } , } , replicas : 2 , } ) ; let multistageCachedNginx = new cloud . Service ( "examples-multistage-cached-nginx" , { containers : { nginx : { build : { context : "./app" , dockerfile : "./app/Dockerfile-multistage" , cacheFrom : { stages : [ "build" ] } , } , memory : 128 , ports : [ { port : 80 } ] , } , } , replicas : 2 , } ) ; let customWebServer = new cloud . Service ( "mycustomservice" , { containers : { webserver : { memory : 128 , ports : [ { port : 80 , targetPort : 8080 } ] , function : ( ) => { let rand = Math . random ( ) ; let http = require ( "http" ) ; http . createServer ( ( req , res ) => { res . end ( `Hello, world! (from ${ rand })` ) ; } ) . listen ( 8080 ) ; } , } , } , replicas : 2 , } ) ; let config = new Config ( "containers" ) ; let redisPassword = config . require ( "redisPassword" ) ; /**
 * A simple Cache abstration, built on top of a Redis container Service.
 */ class Cache { get : ( key : string ) => Promise < string > ; set : ( key : string , value : string ) => Promise < void > ; constructor ( name , memory = 128 ) { let redis = new cloud . Service ( name , { containers : { redis : { image : "redis:alpine" , memory : memory , ports : [ { port : 6379 } ] , command : [ "redis-server" , "--requirepass" , redisPassword ] , } , } , } ) ; this . get = ( key ) => { return redis . getEndpoint ( "redis" , 6379 ) . then ( endpoint => { let client = require ( "redis" ) . createClient ( endpoint . port , endpoint . hostname , { password : redisPassword } ) ; console . log ( client ) ; return new Promise < string > ( ( resolve , reject ) => { client . get ( key , ( err , v ) => { if ( err ) { reject ( err ) ; } else { resolve ( v ) ; } } ) ; } ) ; } ) ; } ; this . set = ( key , value ) => { return redis . getEndpoint ( "redis" , 6379 ) . then ( endpoint => { let client = require ( "redis" ) . createClient ( endpoint . port , endpoint . hostname , { password : redisPassword } ) ; console . log ( client ) ; return new Promise < void > ( ( resolve , reject ) => { client . set ( key , value , ( err , v ) => { if ( err ) { reject ( err ) ; } else { resolve ( ) ; } } ) ; } ) ; } ) ; } ; } } let cache = new Cache ( "examples-mycache" ) ; let helloTask = new cloud . Task ( "examples-hello-world" , { image : "hello-world" , memory : 20 , } ) ; let builtService = new cloud . Service ( "examples-nginx2" , { containers : { nginx : { build : "./app" , memory : 128 , ports : [ { port : 80 } ] , } , } , replicas : 2 , waitForSteadyState : false , } ) ; let api = new cloud . API ( "examples-containers" ) ; api . get ( "/test" , async ( req , res ) => { try { res . json ( { nginx : await nginx . getEndpoint ( ) , nginx2 : await builtService . getEndpoint ( ) , } ) ; } catch ( err ) { console . error ( errorJSON ( err ) ) ; res . status ( 500 ) . json ( errorJSON ( err ) ) ; } } ) ; function errorJSON ( err ) { const result = Object . create ( null ) ; Object . getOwnPropertyNames ( err ) . forEach ( key => result [ key ] = err [ key ] ) ; return result ; } api . get ( "/" , async ( req , res ) => { try { const fetch = ( await import ( "node-fetch" ) ) . default ; console . log ( "handling /" ) ; let page = await cache . get ( "page" ) ; if ( page ) { res . setHeader ( "X-Powered-By" , "redis" ) ; res . end ( page ) ; return ; } let endpoint = await nginx . getEndpoint ( "nginx" , 80 ) ; let resp = await fetch ( `http://${ endpoint . hostname }:${ endpoint . port }/` ) ; let buffer = await resp . buffer ( ) ; console . log ( buffer . toString ( ) ) ; await cache . set ( "page" , buffer . toString ( ) ) ; res . setHeader ( "X-Powered-By" , "nginx" ) ; res . end ( buffer ) ; } catch ( err ) { console . error ( errorJSON ( err ) ) ; res . status ( 500 ) . json ( errorJSON ( err ) ) ; } } ) ; api . get ( "/run" , async ( req , res ) => { try { await helloTask . run ( ) ; res . json ( { success : true } ) ; } catch ( err ) { console . error ( errorJSON ( err ) ) ; res . status ( 500 ) . json ( errorJSON ( err ) ) ; } } ) ; api . get ( "/custom" , async ( req , res ) => { try { const fetch = ( await import ( "node-fetch" ) ) . default ; let endpoint = await customWebServer . getEndpoint ( ) ; let resp = await fetch ( `http://${ endpoint . hostname }:${ endpoint . port }/` ) ; let buffer = await resp . buffer ( ) ; console . log ( buffer . toString ( ) ) ; await cache . set ( "page" , buffer . toString ( ) ) ; res . setHeader ( "X-Powered-By" , "custom web server" ) ; res . end ( buffer ) ; } catch ( err ) { console . error ( errorJSON ( err ) ) ; res . status ( 500 ) . json ( errorJSON ( err ) ) ; } } ) ; const awsConfig = new pulumi . Config ( "cloud-aws" ) ; if ( awsConfig . getBoolean ( "useFargate" ) ) { api . proxy ( "/nginx" , nginx . defaultEndpoint ) ; } export let frontendURL = api . publish ( ) . url ;