Skip to content

Commit d98536c

Browse files
author
Lee Siong Chan
committed
Initial commit
1 parent e44aba2 commit d98536c

11 files changed

+1944
-0
lines changed

.dockerignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build/
2+
node_modules/

.editorconfig

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# EditorConfig helps developers define and maintain consistent
2+
# coding styles between different editors and IDEs
3+
# editorconfig.org
4+
5+
root = true
6+
7+
[*]
8+
# Change these settings to your own preference
9+
indent_style = space
10+
indent_size = 4
11+
12+
# We recommend you to keep these unchanged
13+
end_of_line = lf
14+
charset = utf-8
15+
trim_trailing_whitespace = true
16+
insert_final_newline = true
17+
18+
[*rc]
19+
indent_style = space
20+
indent_size = 2
21+
22+
[*.{js,jsx,ts,tsx}]
23+
indent_style = space
24+
indent_size = 2
25+
26+
[*.json]
27+
indent_style = space
28+
indent_size = 2
29+
30+
[*.md]
31+
indent_style = space
32+
indent_size = 4
33+
trim_trailing_whitespace = false
34+
35+
[*.yaml]
36+
indent_style = space
37+
indent_size = 4
38+
trim_trailing_whitespace = false
39+
40+
[Dockerfile]
41+
indent_style = tab
42+
indent_size = 4

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build/
2+
node_modules/

.prettierrc

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"arrowParens": "always",
3+
"singleQuote": true,
4+
"trailingComma": "all"
5+
}
6+

Dockerfile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM node:10-alpine
2+
3+
WORKDIR /srv/app
4+
5+
COPY . ./
6+
7+
RUN set -ex; \
8+
yarn install --frozen-lockfile; \
9+
yarn cache clean; \
10+
yarn run build
11+
12+
EXPOSE 4000
13+
CMD ["yarn", "start"]

README.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# accounts
2+
3+
## How to use this image
4+
5+
### build and run using Docker CLI
6+
7+
```console
8+
$ docker build -t accounts-js .
9+
$ docker run -it accounts-js -e ACCOUNTS_DB=mongo -e ACCOUNTS_DB_URL=mongodb://db -e ACCOUNTS_SECRET=my_secret
10+
```
11+
12+
### or if you prefer Docker Compose
13+
14+
```yml
15+
version: "3"
16+
17+
services:
18+
app:
19+
build:
20+
context: .
21+
dockerfile: Dockerfile
22+
environment:
23+
- ACCOUNTS_DB=mongo
24+
- ACCOUNTS_DB_URL=mongodb://db
25+
- ACCOUNTS_SECRET=my_secret
26+
ports:
27+
- 4000:4000
28+
29+
db:
30+
image: mongo:4
31+
```
32+
33+
You can then run using Docker Compose command
34+
35+
```console
36+
$ docker-compose up -d
37+
```

package.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "accounts-graphql-server",
3+
"private": true,
4+
"version": "0.0.0",
5+
"license": "MIT",
6+
"scripts": {
7+
"build": "tsc",
8+
"start": "node build/index.js"
9+
},
10+
"dependencies": {
11+
"@accounts/database-manager": "^0.3.0-beta.29",
12+
"@accounts/graphql-api": "^0.3.0-beta.29",
13+
"@accounts/mongo": "^0.3.0-beta.29",
14+
"@accounts/password": "^0.3.0-beta.29",
15+
"@accounts/server": "^0.3.0-beta.29",
16+
"apollo-server": "^2.1.0",
17+
"graphql": "^0.13.2",
18+
"graphql-tools": "^3.1.1",
19+
"mongoose": "^5.3.2"
20+
},
21+
"devDependencies": {
22+
"@accounts/tslint-config-accounts": "^0.0.9",
23+
"@types/graphql": "^0.13.4",
24+
"@types/mongoose": "^5.2.18",
25+
"@types/node": "^10.11.5",
26+
"prettier": "^1.14.3",
27+
"tslint": "^5.11.0",
28+
"typescript": "^3.1.1"
29+
}
30+
}

src/index.ts

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import * as mongoose from 'mongoose';
2+
import MongoDBInterface from '@accounts/mongo';
3+
import { AccountsPassword } from '@accounts/password';
4+
import { AccountsServer } from '@accounts/server';
5+
import { ApolloServer, makeExecutableSchema } from 'apollo-server';
6+
import { DatabaseManager } from '@accounts/database-manager';
7+
import { createAccountsGraphQL, accountsContext } from '@accounts/graphql-api';
8+
9+
const start = async () => {
10+
// Create database connection
11+
let storage;
12+
if (process.env.ACCOUNTS_DB === 'mongo') {
13+
await mongoose.connect(process.env.ACCOUNTS_DB_URL, { useNewUrlParser: true });
14+
storage = new MongoDBInterface(mongoose.connection);
15+
}
16+
17+
// Build a storage for storing users
18+
// Create database manager (create user, find users, sessions etc) for accounts-js
19+
const accountsDb = new DatabaseManager({
20+
sessionStorage: storage,
21+
userStorage: storage,
22+
});
23+
24+
// Create accounts server that holds a lower level of all accounts operations
25+
const accountsServer = new AccountsServer(
26+
{ db: accountsDb, tokenSecret: process.env.ACCOUNTS_SECRET },
27+
{ password: new AccountsPassword() },
28+
);
29+
30+
// Creates resolvers, type definitions, and schema directives used by accounts-js
31+
const accountsGraphQL = createAccountsGraphQL(accountsServer, { extend: false });
32+
33+
const schema = makeExecutableSchema({
34+
resolvers: { Query: {}, ...accountsGraphQL.resolvers },
35+
schemaDirectives: accountsGraphQL.schemaDirectives,
36+
typeDefs: [accountsGraphQL.typeDefs],
37+
});
38+
39+
// Create the Apollo Server that takes a schema and configures internal stuff
40+
const server = new ApolloServer({
41+
schema,
42+
context: ({ req }) => accountsContext(req, { accountsServer }),
43+
});
44+
45+
server.listen(4000).then(({ url }) => {
46+
console.log(`🚀 Server ready at ${url}`);
47+
});
48+
};
49+
50+
start();

tsconfig.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["es2015", "esnext.asynciterable"],
4+
"module": "commonjs",
5+
"moduleResolution": "node",
6+
"outDir": "build",
7+
"target": "es2018"
8+
},
9+
"include": ["src"],
10+
"exclude": ["build", "node_modules"]
11+
}

tslint.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": ["@accounts/tslint-config-accounts"],
3+
"rules": {
4+
"no-submodule-imports": [true, "@accounts/server"],
5+
"no-implicit-dependencies": [true, "dev"]
6+
}
7+
}

0 commit comments

Comments
 (0)