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

Split API #218

Closed
wants to merge 3 commits into from
Closed
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
7 changes: 4 additions & 3 deletions udagram-api/package.json → udagram-api-feed/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
"dependencies": {
"@types/bcrypt": "^3.0.0",
"@types/jsonwebtoken": "^8.3.2",
"aws-sdk": "^2.429.0",
"aws-sdk": "^2.1649.0",
"bcrypt": "^5.0.1",
"body-parser": "^1.18.3",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"email-validator": "^2.0.4",
"express": "^4.16.4",
"express": "^4.19.2",
"jsonwebtoken": "^8.5.1",
"pg": "^8.0.3",
"reflect-metadata": "^0.1.13",
Expand Down
File renamed without changes.
12 changes: 12 additions & 0 deletions udagram-api-feed/src/controllers/v0/index.router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {Router, Request, Response} from 'express';
import {FeedRouter} from './feed/routes/feed.router';

const router: Router = Router();

router.use('/feed', FeedRouter);

router.get('/', async (req: Request, res: Response) => {
res.send(`V0`);
});

export const IndexRouter: Router = router;
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {FeedItem} from './feed/models/FeedItem';
import {User} from './users/models/User';


export const V0_USER_MODELS = [User];
export const V0_FEED_MODELS = [FeedItem];
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import {IndexRouter} from './controllers/v0/index.router';

import bodyParser from 'body-parser';
import {config} from './config/config';
import {V0_FEED_MODELS, V0_USER_MODELS} from './controllers/v0/model.index';
import {V0_FEED_MODELS} from './controllers/v0/model.index';


(async () => {
await sequelize.addModels(V0_FEED_MODELS);
await sequelize.addModels(V0_USER_MODELS);

console.debug("Initialize database connection...");
await sequelize.sync();
Expand Down Expand Up @@ -45,7 +44,7 @@ import {V0_FEED_MODELS, V0_USER_MODELS} from './controllers/v0/model.index';

// Start the Server
app.listen( port, () => {
console.log( `server running ${config.url}` );
console.log( `feed server running ${config.url}` );
console.log( `press CTRL+C to stop server` );
} );
})();
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions udagram-api-user/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
26 changes: 26 additions & 0 deletions udagram-api-user/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"env": {
"browser": true,
"es6": true,
"node": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
}
}
42 changes: 42 additions & 0 deletions udagram-api-user/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Specifies intentionally untracked files to ignore when using Git
# http://git-scm.com/docs/gitignore

*~
*.sw[mnpcod]
*.log
*.tmp
*.tmp.*
log.txt
*.sublime-project
*.sublime-workspace
.vscode/
npm-debug.log*

.idea/
.ionic/
.sourcemaps/
.sass-cache/
.tmp/
.versions/
coverage/
www/
node_modules/
tmp/
temp/
platforms/
plugins/
plugins/android.json
plugins/ios.json
$RECYCLE.BIN/
postgres_dev/
logfile

.DS_Store
Thumbs.db
UserInterfaceState.xcuserstate
node_modules
venv/
# Elastic Beanstalk Files
.elasticbeanstalk/*
!.elasticbeanstalk/*.cfg.yml
!.elasticbeanstalk/*.global.yml
1 change: 1 addition & 0 deletions udagram-api-user/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
unsafe-perm=true
Binary file added udagram-api-user/mock/xander0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added udagram-api-user/mock/xander1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added udagram-api-user/mock/xander2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions udagram-api-user/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "udagram-api",
"version": "2.0.0",
"description": "",
"main": "src/server.js",
"scripts": {
"start": "node .",
"tsc": "tsc",
"dev": "ts-node-dev --respawn --transpile-only ./src/server.ts",
"prod": "tsc && node ./www/server.js",
"clean": "rm -rf www/ || true",
"build": "npm run clean && tsc && cp -rf src/config www/config && cp .npmrc www/.npmrc && cp package.json www/package.json && cd www && zip -r Archive.zip . && cd ..",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Gabriel Ruttner",
"license": "ISC",
"dependencies": {
"@types/bcrypt": "^3.0.0",
"@types/jsonwebtoken": "^8.3.2",
"aws-sdk": "^2.1649.0",
"bcrypt": "^5.0.1",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"email-validator": "^2.0.4",
"express": "^4.19.2",
"jsonwebtoken": "^8.5.1",
"pg": "^8.0.3",
"reflect-metadata": "^0.1.13",
"sequelize": "^5.21.4",
"sequelize-typescript": "^0.6.9"
},
"devDependencies": {
"@types/bluebird": "^3.5.26",
"@types/cors": "^2.8.6",
"@types/express": "^4.16.1",
"@types/node": "^11.11.6",
"@types/sequelize": "^4.27.44",
"@types/validator": "^10.9.0",
"@typescript-eslint/eslint-plugin": "^2.19.2",
"@typescript-eslint/parser": "^2.19.2",
"chai": "^4.2.0",
"chai-http": "^4.2.1",
"eslint": "^6.8.0",
"eslint-config-google": "^0.14.0",
"mocha": "^6.1.4",
"ts-node-dev": "^1.0.0-pre.32",
"typescript": "^3.3.4000"
}
}
35 changes: 35 additions & 0 deletions udagram-api-user/src/aws.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import AWS = require('aws-sdk');
import {config} from './config/config';


// Configure AWS
const credentials = new AWS.SharedIniFileCredentials({profile: config.aws_profile});
AWS.config.credentials = credentials;

export const s3 = new AWS.S3({
signatureVersion: 'v4',
region: config.aws_region,
params: {Bucket: config.aws_media_bucket},
});

// Generates an AWS signed URL for retrieving objects
export function getGetSignedUrl( key: string ): string {
const signedUrlExpireSeconds = 60 * 5;

return s3.getSignedUrl('getObject', {
Bucket: config.aws_media_bucket,
Key: key,
Expires: signedUrlExpireSeconds,
});
}

// Generates an AWS signed URL for uploading objects
export function getPutSignedUrl( key: string ): string {
const signedUrlExpireSeconds = 60 * 5;

return s3.getSignedUrl('putObject', {
Bucket: config.aws_media_bucket,
Key: key,
Expires: signedUrlExpireSeconds,
});
}
14 changes: 14 additions & 0 deletions udagram-api-user/src/config/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const config = {
'username': process.env.POSTGRES_USERNAME,
'password': process.env.POSTGRES_PASSWORD,
'database': process.env.POSTGRES_DB,
'host': process.env.POSTGRES_HOST,
'dialect': 'postgres',
'aws_region': process.env.AWS_REGION,
'aws_profile': process.env.AWS_PROFILE,
'aws_media_bucket': process.env.AWS_BUCKET,
'url': process.env.URL,
'jwt': {
'secret': process.env.JWT_SECRET,
},
};
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {Router, Request, Response} from 'express';
import {FeedRouter} from './feed/routes/feed.router';
import {UserRouter} from './users/routes/user.router';

const router: Router = Router();

router.use('/feed', FeedRouter);
router.use('/users', UserRouter);

router.get('/', async (req: Request, res: Response) => {
Expand Down
4 changes: 4 additions & 0 deletions udagram-api-user/src/controllers/v0/model.index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {User} from './users/models/User';


export const V0_USER_MODELS = [User];
13 changes: 13 additions & 0 deletions udagram-api-user/src/sequelize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {Sequelize} from 'sequelize-typescript';
import {config} from './config/config';


export const sequelize = new Sequelize({
'username': config.username,
'password': config.password,
'database': config.database,
'host': config.host,

'dialect': config.dialect,
'storage': ':memory:',
});
50 changes: 50 additions & 0 deletions udagram-api-user/src/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import cors from 'cors';
import express from 'express';
import {sequelize} from './sequelize';

import {IndexRouter} from './controllers/v0/index.router';

import bodyParser from 'body-parser';
import {config} from './config/config';
import {V0_USER_MODELS} from './controllers/v0/model.index';


(async () => {
await sequelize.addModels(V0_USER_MODELS);

console.debug("Initialize database connection...");
await sequelize.sync();

const app = express();
const port = process.env.PORT || 8080;

app.use(bodyParser.json());

// We set the CORS origin to * so that we don't need to
// worry about the complexities of CORS this lesson. It's
// something that will be covered in the next course.
app.use(cors({
allowedHeaders: [
'Origin', 'X-Requested-With',
'Content-Type', 'Accept',
'X-Access-Token', 'Authorization',
],
methods: 'GET,HEAD,OPTIONS,PUT,PATCH,POST,DELETE',
preflightContinue: true,
origin: '*',
}));

app.use('/api/v0/', IndexRouter);

// Root URI call
app.get( '/', async ( req, res ) => {
res.send( '/api/v0/' );
} );


// Start the Server
app.listen( port, () => {
console.log( `user server running ${config.url}` );
console.log( `press CTRL+C to stop server` );
} );
})();
67 changes: 67 additions & 0 deletions udagram-api-user/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"compilerOptions": {
/* Basic Options */
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./www", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
"strictNullChecks": false, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"baseUrl": "/", /* Base directory to resolve non-absolute module names. */
"paths": {
"*": [
"node_modules/*"
]
}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
"emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
},
"include": [
"src/**/*"
]
}
Loading
Loading