Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add docker compose #2

Merged
merged 3 commits into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ generated
*.js
!jest.config.js

azure-functions-core-tools
2 changes: 1 addition & 1 deletion HttpTriggerFunction/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { HttpHandler } from "../handler";
describe("HttpCtrl", () => {
it("should return a string when the query parameter is provided", async () => {
const httpHandler = HttpHandler();
const response = await httpHandler({} as any, "param");
const response = await httpHandler({} as any, {} as any, "param");
expect(response.kind).toBe("IResponseSuccessJson");
});
});
17 changes: 14 additions & 3 deletions HttpTriggerFunction/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import * as express from "express";
import * as t from "io-ts";

import { Context } from "@azure/functions";
import { ServiceModel } from "io-functions-commons/dist/src/models/service";
import {
AzureUserAttributesMiddleware,
IAzureUserAttributes
} from "io-functions-commons/dist/src/utils/middlewares/azure_user_attributes";
import { ContextMiddleware } from "io-functions-commons/dist/src/utils/middlewares/context_middleware";
import { RequiredParamMiddleware } from "io-functions-commons/dist/src/utils/middlewares/required_param";
import {
Expand All @@ -16,6 +21,7 @@ import {

type IHttpHandler = (
context: Context,
userAttrs: IAzureUserAttributes,
param: string
) => Promise<
| IResponseSuccessJson<{
Expand All @@ -25,16 +31,21 @@ type IHttpHandler = (
>;

export function HttpHandler(): IHttpHandler {
return async (_, param: string) => {
return ResponseSuccessJson({ message: `Hello ${param} !` });
return async (ctx, userAttrs, param) => {
return ResponseSuccessJson({
headers: ctx.req?.headers,
message: `Hello ${param} !`,
user: userAttrs
});
};
}

export function HttpCtrl(): express.RequestHandler {
export function HttpCtrl(serviceModel: ServiceModel): express.RequestHandler {
const handler = HttpHandler();

const middlewaresWrap = withRequestMiddlewares(
ContextMiddleware(),
AzureUserAttributesMiddleware(serviceModel),
RequiredParamMiddleware("someParam", t.string)
);

Expand Down
30 changes: 29 additions & 1 deletion HttpTriggerFunction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,36 @@ import { AzureContextTransport } from "io-functions-commons/dist/src/utils/loggi
import { setAppContext } from "io-functions-commons/dist/src/utils/middlewares/context_middleware";
import createAzureFunctionHandler from "io-functions-express/dist/src/createAzureFunctionsHandler";

import { DocumentClient as DocumentDBClient } from "documentdb";
import {
SERVICE_COLLECTION_NAME,
ServiceModel
} from "io-functions-commons/dist/src/models/service";
import * as documentDbUtils from "io-functions-commons/dist/src/utils/documentdb";
import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";

import { HttpCtrl } from "./handler";

//
// CosmosDB initialization
//

const cosmosDbUri = getRequiredStringEnv("CUSTOMCONNSTR_COSMOSDB_URI");
const cosmosDbKey = getRequiredStringEnv("CUSTOMCONNSTR_COSMOSDB_KEY");
const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");

const documentDbDatabaseUrl = documentDbUtils.getDatabaseUri(cosmosDbName);
const servicesCollectionUrl = documentDbUtils.getCollectionUri(
documentDbDatabaseUrl,
SERVICE_COLLECTION_NAME
);

const documentClient = new DocumentDBClient(cosmosDbUri, {
masterKey: cosmosDbKey
});

const serviceModel = new ServiceModel(documentClient, servicesCollectionUrl);

// tslint:disable-next-line: no-let
let logger: Context["log"] | undefined;
const contextTransport = new AzureContextTransport(() => logger, {
Expand All @@ -21,7 +49,7 @@ const app = express();
secureExpressApp(app);

// Add express route
app.get("/some/path/:someParam", HttpCtrl());
app.get("/some/path/:someParam", HttpCtrl(serviceModel));

const azureFunctionHandler = createAzureFunctionHandler(app);

Expand Down
104 changes: 104 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
version: "3.2"

services:

functions:
image: functions-template:v2.0.0
build:
context: ./
dockerfile: docker/functions/Dockerfile
env_file:
- .env
working_dir: /usr/src/app
ports:
- "7071:7071"
networks:
- io-fn
depends_on:
- fnstorage
volumes:
- .:/usr/src/app
labels:
- "traefik.enable=true"
- "traefik.http.routers.functions.rule=Host(`localhost`)"
- "traefik.http.routers.functions.entrypoints=web"

- "traefik.http.middlewares.testHeader.headers.customrequestheaders.x-user-id=unused"
- "traefik.http.middlewares.testHeader.headers.customrequestheaders.x-user-groups=${REQ_USER_GROUPS}"
- "traefik.http.middlewares.testHeader.headers.customrequestheaders.x-subscription-id=${REQ_SERVICE_ID}"
- "traefik.http.middlewares.testHeader.headers.customrequestheaders.x-user-email=unused@example.com"
- "traefik.http.middlewares.testHeader.headers.customrequestheaders.x-user-note=unused"
- "traefik.http.middlewares.testHeader.headers.customrequestheaders.x-functions-key=unused"

# apply middleware to route
- "traefik.http.routers.functions.middlewares=testHeader"

fnstorage:
image: azurite
build:
context: ./
dockerfile: docker/azurite/Dockerfile
ports:
- "10000:10000"
- "10001:10001"
- "10002:10002"
networks:
- io-fn

storage:
image: azurite
command: ["sh", "-c", "node bin/azurite -l /opt/azurite/folder --blobPort 10003 --queuePort 10004 --tablePort 10005"]
ports:
- "10003:10003"
- "10004:10004"
- "10005:10005"
depends_on:
- fnstorage
networks:
- io-fn

cosmosdb:
image: cosmosdb
env_file:
- .env
build:
context: ./
dockerfile: docker/cosmosdb/Dockerfile
ports:
- ${COSMOSDB_PORT}:3000
networks:
- io-fn

fixtures:
image: fixtures
env_file:
- .env
build:
context: ./
dockerfile: docker/fixtures/Dockerfile
depends_on:
- cosmosdb
networks:
- io-fn

traefik:
image: traefik:v2.0
command: |-
--entrypoints.web.address=:80
--providers.docker=true
--providers.docker.network=io-fn
--log.level=ERROR
env_file:
- .env
ports:
- ${API_GATEWAY_PORT}:${API_GATEWAY_PORT}
networks:
- io-fn
volumes:
- /var/run/docker.sock:/var/run/docker.sock

networks:
io-fn:
driver: bridge
driver_opts:
com.docker.network.driver.mtu: 1450
23 changes: 23 additions & 0 deletions docker/azurite/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM node:10.14.2-alpine as builder

WORKDIR /opt/azurite

RUN apk update && apk upgrade && \
apk add --no-cache bash git openssh

RUN git clone https://github.com/Azure/Azurite /opt/azurite && \
git checkout legacy-master

RUN npm install

FROM node:10.14.2-alpine

COPY --from=builder /opt/azurite /opt/azurite

WORKDIR /opt/azurite

VOLUME /opt/azurite/folder

ENV executable azurite

CMD ["sh", "-c", "node bin/${executable} -l /opt/azurite/folder"]
7 changes: 7 additions & 0 deletions docker/cosmosdb/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM node:10.14.2-alpine

WORKDIR /opt/cosmosdb

RUN npm install -g @zeit/cosmosdb-server ts-node

CMD ["sh", "-c", "cosmosdb-server -p 3000"]
11 changes: 11 additions & 0 deletions docker/fixtures/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM node:10.14.2-alpine

WORKDIR /opt/cosmosdb

RUN npm install -g ts-node typescript && \
npm install documentdb @types/documentdb io-functions-commons \
io-ts@1.8.5 fp-ts@1.12.0 italia-ts-commons

COPY docker/fixtures/index.ts /opt/cosmosdb

CMD ["sh", "-c", "ts-node index"]
97 changes: 97 additions & 0 deletions docker/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* Insert fake data into CosmosDB database emulator.
*/
import {
CollectionMeta,
DocumentClient as DocumentDBClient,
UriFactory
} from "documentdb";
import { Either, left, right } from "fp-ts/lib/Either";
import {
Service,
SERVICE_COLLECTION_NAME,
ServiceModel
} from "io-functions-commons/dist/src/models/service";
import * as documentDbUtils from "io-functions-commons/dist/src/utils/documentdb";
import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";

const cosmosDbKey = getRequiredStringEnv("CUSTOMCONNSTR_COSMOSDB_KEY");
const cosmosDbUri = getRequiredStringEnv("CUSTOMCONNSTR_COSMOSDB_URI");
const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");

const documentDbDatabaseUrl = documentDbUtils.getDatabaseUri(cosmosDbName);
const servicesCollectionUrl = documentDbUtils.getCollectionUri(
documentDbDatabaseUrl,
SERVICE_COLLECTION_NAME
);

const documentClient = new DocumentDBClient(cosmosDbUri, {
masterKey: cosmosDbKey
});

function createDatabase(databaseName: string): Promise<Either<Error, void>> {
return new Promise(resolve => {
documentClient.createDatabase({ id: databaseName }, (err, _) => {
if (err) {
return resolve(left<Error, void>(new Error(err.body)));
}
resolve(right<Error, void>(void 0));
});
});
}

function createCollection(
collectionName: string,
partitionKey: string
): Promise<Either<Error, CollectionMeta>> {
return new Promise(resolve => {
const dbUri = UriFactory.createDatabaseUri(cosmosDbName);
documentClient.createCollection(
dbUri,
{
id: collectionName,
partitionKey: {
kind: "Hash",
paths: [`/${partitionKey}`]
}
},
(err, ret) => {
if (err) {
return resolve(left<Error, CollectionMeta>(new Error(err.body)));
}
resolve(right<Error, CollectionMeta>(ret));
}
);
});
}

const serviceModel = new ServiceModel(documentClient, servicesCollectionUrl);

const aService: Service = Service.decode({
authorizedCIDRs: [],
authorizedRecipients: [],
departmentName: "Deparment Name",
isVisible: true,
maxAllowedPaymentAmount: 100000,
organizationFiscalCode: "01234567890",
organizationName: "Organization name",
requireSecureChannels: false,
serviceId: process.env.REQ_SERVICE_ID,
serviceName: "MyServiceName"
}).getOrElseL(() => {
throw new Error("Cannot decode service payload.");
});

createDatabase(cosmosDbName)
.then(() => createCollection("message-status", "messageId"))
.then(() => createCollection("messages", "messageId"))
.then(() => createCollection("notification-status", "notificationId"))
.then(() => createCollection("notifications", "messageId"))
.then(() => createCollection("profiles", "fiscalCode"))
.then(() => createCollection("sender-services", "recipientFiscalCode"))
.then(() => createCollection("services", "serviceId"))
.then(() => serviceModel.create(aService, aService.serviceId))
// tslint:disable-next-line: no-console
.then(s => console.log(s.value))
// tslint:disable-next-line: no-console
.catch(console.error);
17 changes: 17 additions & 0 deletions docker/functions/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM node:10.14.1

WORKDIR /usr/src/app

RUN apt-get update && \
apt-get install -y lsb-release && \
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg && \
mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg && \
sh -c 'echo "deb [arch=amd64] http://packages.microsoft.com/debian/$(lsb_release -rs | cut -d'.' -f 1)/prod $(lsb_release -cs) main" > /etc/apt/sources.list.d/dotnetdev.list' && \
apt-get update

RUN apt-get install -y azure-functions-core-tools

ENV AzureWebJobsScriptRoot=/usr/src/app \
AzureFunctionsJobHost__Logging__Console__IsEnabled=true

CMD ["func", "start", "--javascript"]
18 changes: 18 additions & 0 deletions env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
COMPOSE_PROJECT_NAME=io-fn-template

COSMOSDB_PORT=3000
API_GATEWAY_PORT=80

FUNCTIONS_WORKER_RUNTIME=node
AzureWebJobsStorage=DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://fnstorage:10000/devstoreaccount1;QueueEndpoint=http://fnstorage:10001/devstoreaccount1;TableEndpoint=http://fnstorage:10002/devstoreaccount1;
FUNCTIONS_V2_COMPATIBILITY_MODE=true

REQ_USER_GROUPS=ApiProfileWrite
REQ_SERVICE_ID=MyServiceId

CUSTOMCONNSTR_COSMOSDB_URI=https://cosmosdb:3000
CUSTOMCONNSTR_COSMOSDB_KEY=dummykey
COSMOSDB_NAME=testdb

# needed to connect to cosmosdb server
NODE_TLS_REJECT_UNAUTHORIZED=0
Loading