Skip to content
This repository has been archived by the owner on Jun 24, 2019. It is now read-only.

Add Docker #51

Open
wants to merge 13 commits into
base: development
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions .devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// See https://aka.ms/vscode-remote/devcontainer.json for format details.
{
"dockerComposeFile": "docker-compose.yml",
"service": "coding-coach-api",
"workspaceFolder": "/",
"extensions": []
}
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ docker-compose*
README.md
LICENSE
.vscode
test
test
dist
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
"language": "typescriptreact",
"autoFix": true
}
]
],
"azureFunctions.projectSubpath": "src"
}
56 changes: 50 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@
FROM node:lts
RUN apt-get update
RUN apt-get install dos2unix
RUN yarn global add [email protected]
RUN dos2unix /usr/local/share/.config/yarn/global/node_modules/azurite/bin/azurite
ENTRYPOINT azurite
FROM mcr.microsoft.com/azure-functions/node:2.0

USER root

# Based on instructiions at https://docs.microsoft.com/en-us/dotnet/core/linux-prerequisites?tabs=netcore2x
# Install depency for dotnet core 2.
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
curl libunwind8 gettext apt-transport-https && \
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] https://packages.microsoft.com/repos/microsoft-debian-stretch-prod stretch main" > /etc/apt/sources.list.d/dotnetdev.list' && \
apt-get update

# Install the .Net Core framework, set the path, and show the version of core installed.
RUN apt-get install -y dotnet-sdk-2.0.0 && \
export PATH=$PATH:$HOME/dotnet && \
dotnet --version

# Not sure if its hacky, but needs to install yarn
RUN apt-get update && apt-get install -y apt-transport-https
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install yarn

RUN useradd -m node && echo "node:node" | chpasswd && adduser node sudo
RUN mkdir /home/.npm
RUN mkdir /home/.cache
RUN mkdir /home/.azurefunctions
RUN chown -R node /home
RUN chown -R node /home/.npm
RUN chown -R node /home/.cache
RUN chown -R node /home/.azurefunctions
RUN chown -R node /home/.dotnet

# Good idea to switch back to the node user.
USER node

RUN mkdir -p /home/node/app

ENV NPM_CONFIG_PREFIX=/home/node/.npm
ENV PATH=$PATH:/home/node/.npm/bin
WORKDIR /home/node/app
ENV AzureWebJobsScriptRoot=/home/node/app
COPY package.json /home/node/app
RUN npm i -g copyfiles onchange rimraf typescript ttypescript
RUN npm i -g azure-functions-core-tools@core --unsafe-perm true
COPY . /home/node/app
EXPOSE 7071
# CMD [ "npm", "run", "start"]
93 changes: 18 additions & 75 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,75 +1,18 @@
### Basics requirements

- Docker, yarn

# Serverless Azure Functions Node.js Template

This starter template allows quickly creating a Node.js-based service to Azure Functions. It relies on the `serverless-azure-functions` plugin, and therefore, before you can deploy it, you simply need to run `npm install` in order to acquire it (this dependency is already saved in the `package.json` file).

### Setting up your Azure credentials

Once the `serverless-azure-functions` plugin is installed, it expects to find your Azure credentials via a set of well-known environment variables. These will be used to actually authenticate with your Azure account, so that the Serverless CLI can generate the necessary Azure resources on your behalf when you request a deployment (see below).

The following environment variables must be set, with their respective values:

- *azureSubId* - ID of the Azure subscription you want to create your service within
- *azureServicePrincipalTenantId* - ID of the tenant that your service principal was created within
- *azureServicePrincipalClientId* - ID of the service principal you want to use to authenticate with Azure
- *azureServicePrincipalPassword* - Password of the service principal you want to use to authenticate with Azure

For details on how to create a service principal and/or acquire your Azure account's subscription/tenant ID, refer to the [Azure credentials](https://serverless.com/framework/docs/providers/azure/guide/credentials/) documentation.

### Install container

`docker-compose up -d`

### Install azure function core tools

`npm install -g azure-functions-core-tools`

### Run localy

`yarn run start`

### Basics end-points (Create and List users)

- Intall Postman: https://www.getpostman.com/
- Import this collection: `coach-code-api.postman_collection.json`

### Deploying the service

Once your Azure credentials are set, you can immediately deploy your service via the following command:

```shell
serverless deploy
```

This will create the necessary Azure resources to support the service and events that are defined in your `serverless.yml` file.

### Invoking and inspecting a function

With the service deployed, you can test it's functions using the following command:

```shell
serverless invoke -f hello
```

Additionally, if you'd like to view the logs that a function generates (either via the runtime, or create by your handler by calling `context.log`), you can simply run the following command:

```shell
serverless logs -f hello
```

### Cleaning up

Once you're finished with your service, you can remove all of the generated Azure resources by simply running the following command:

```shell
serverless remove
```

### Issues / Feedback / Feature Requests?

If you have any issues, comments or want to see new features, please file an issue in the project repository:

https://github.com/serverless/serverless-azure-functions
# Coding Coach API

## Summary
Coding Coach is a free site to link mentors with mentees. The backend is a typescript app built on top of Azure Functions

## Setup
### 1. Within a Docker container (Recommended)
1. Install Docker for your OS as described (here)[https://docs.docker.com/install/]
2. `git clone` this repository
3. `cd` to the project folder
4. Run `docker-compose up`

### 2. Without a Docker Container
If you prefer not to develop within Docker,
1. Ensure you have Nodejs and Azure Functions Core Tools installed as described (here)[] and (here)[]
2. `git clone` this repository
3. `cd` to the project folder
4. Run `yarn start`
7 changes: 7 additions & 0 deletions azurite/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

FROM node:lts
RUN apt-get update
RUN apt-get install dos2unix
RUN yarn global add [email protected]
RUN dos2unix /usr/local/share/.config/yarn/global/node_modules/azurite/bin/azurite
ENTRYPOINT azurite
13 changes: 11 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
version: "2.1"

services:
coding-coach-azurite:
coding-coach-api:
build: .
command: npm run start
volumes:
- ./:/home/node/app
# - /home/node/node_modules
working_dir: /home/node/app
ports:
- '7071:7071'
coding-coach-azurite:
build: ./azurite
ports:
- 10000:10000
- 10001:10001
- 10002:10002
expose:
- 10000
- 10001
- 10002
- 10002
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
"watch": "tsc -w & onchange \"$npm_package_config_azureFunctions_rootDir/**/*.json\" \"$npm_package_config_azureFunctions_rootDir/**/*.csproj\" -- yarn build:configFiles",
"clean": "rimraf $npm_package_config_azureFunctions_outDir",
"test": "jest",
"lint": "eslint src --ext .ts,.tsx",
"lint": "tslint -p tsconfig.json",
"publish:update": "yarn test && yarn build:production && echo \"Publishing '$npm_package_config_azureFunctions_outDir' content to already deployed function app '$npm_package_name'\" && func azure functionapp publish $npm_package_name --prefix $npm_package_config_azureFunctions_outDir"
},
"dependencies": {
"@azure/functions": "^1.0.1-beta2",
"@babel/polyfill": "^7.4.4",
"@graphql-modules/core": "^0.7.0",
"@graphql-modules/di": "^0.7.0",
"@types/node": "^10.12.24",
Expand All @@ -39,22 +40,24 @@
"uuid": "^3.3.2"
},
"devDependencies": {
"@babel/cli": "^7.4.4",
"@babel/core": "^7.4.5",
"@babel/preset-env": "^7.4.5",
"@fluffy-spoon/substitute": "^1.70.0",
"@types/async-retry": "^1.4.1",
"@types/jest": "^24.0.0",
"@typescript-eslint/eslint-plugin": "^1.7.0",
"@typescript-eslint/parser": "^1.7.0",
"async-retry": "^1.2.3",
"azurite": "^2.7.0",
"copyfiles": "^2.1.0",
"eslint": "^5.16.0",
"husky": "^2.1.0",
"jest": "23.6.0",
"onchange": "^5.2.0",
"rimraf": "^2.6.3",
"testcontainers": "^1.1.9",
"ts-jest": "^23.10.5",
"ts-node": "^8.3.0",
"ts-transformer-imports": "^0.4.1",
"tslint": "^5.17.0",
"ttypescript": "^1.5.6"
},
"config": {
Expand Down
2 changes: 1 addition & 1 deletion src/container.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'reflect-metadata';
import { GraphQLModule } from '@graphql-modules/core';
import { ApplicationModule } from '@modules/application-module';
import 'reflect-metadata';

const Container = new GraphQLModule({
imports: [ApplicationModule],
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/HttpTrigger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AzureFunction, Context, HttpRequest } from '@azure/functions';

const httpTrigger: AzureFunction = async function(
context: Context,
req: HttpRequest
req: HttpRequest,
): Promise<void> {
context.log('HTTP trigger function processed a request.');
const name = req.query.name || (req.body && req.body.name);
Expand All @@ -14,8 +14,8 @@ const httpTrigger: AzureFunction = async function(
};
} else {
context.res = {
status: 400,
body: 'Please pass a name on the query string or in the request body',
status: 400,
};
}
};
Expand Down
10 changes: 5 additions & 5 deletions src/handlers/add-mentee/add-mentee-handler.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import 'reflect-metadata';
import { Context, HttpRequest } from '@azure/functions';
import { Inject, Injectable } from '@graphql-modules/di';
import {
IMentorRepository,
MentorEntity,
} from '@repositories/mentor-repository';
import { Inject, Injectable } from '@graphql-modules/di';
import 'reflect-metadata';

@Injectable()
class AddMentee {
constructor(
@Inject('IMentorRepository') private mentorRepository: IMentorRepository
@Inject('IMentorRepository') private mentorRepository: IMentorRepository,
) {}
index = async (context: Context, req: HttpRequest): Promise<void> => {
public index = async (context: Context, req: HttpRequest): Promise<void> => {
context.log('JavaScript HTTP trigger function processed a request.');

const mentorId = req.query.mentorId;
Expand All @@ -24,7 +24,7 @@ class AddMentee {
context.res = {
status: '200',
};
};
}
}

export { AddMentee };
4 changes: 2 additions & 2 deletions src/handlers/add-mentee/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'reflect-metadata';
import { AzureFunction } from '@azure/functions';
import { AddMentee } from './add-mentee-handler';
import { Container } from '@container';
import 'reflect-metadata';
import { AddMentee } from './add-mentee-handler';

const index: AzureFunction = Container.injector.get(AddMentee).index;
export { index };
16 changes: 8 additions & 8 deletions src/handlers/add-user/add-user-handler.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import 'reflect-metadata';
import uuidv1 from 'uuid/v1';
import { Context, HttpRequest } from '@azure/functions';
import { Inject, Injectable } from '@graphql-modules/di';
import { IUserRepository, User } from '@repositories/user-repository';
import 'reflect-metadata';
import uuidv1 from 'uuid/v1';

@Injectable()
class AddUser {
constructor(@Inject('IUserRepository') private userRepository: IUserRepository) {
// Nothing to do here
}

index = async (context: Context, req: HttpRequest): Promise<void> => {
public index = async (context: Context, req: HttpRequest): Promise<void> => {
const user = new User({
// @TODO: for now using an uuid, but we want to use auth0 Ids to setup this value
userId: uuidv1(),
Expand All @@ -20,16 +20,16 @@ class AddUser {
await this.userRepository.save(user);

context.res = {
headers: {
'Content-Type': 'application/json',
},
body: {
success: true,
message: 'Successfully added',
success: true,
user,
},
headers: {
'Content-Type': 'application/json',
},
};
};
}
}

export { AddUser };
4 changes: 2 additions & 2 deletions src/handlers/add-user/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'reflect-metadata';
import { AzureFunction } from '@azure/functions';
import { AddUser } from './add-user-handler';
import { Container } from '@container';
import 'reflect-metadata';
import { AddUser } from './add-user-handler';

const index: AzureFunction = Container.injector.get(AddUser).index;

Expand Down
Loading