forked from particle-iot/spark-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request particle-iot#26 from AntonPuko/oauth
move app setup to app.js; seperate test settings
- Loading branch information
Showing
9 changed files
with
160 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// @flow | ||
|
||
import type { | ||
$Application, | ||
$Request, | ||
$Response, | ||
Middleware, | ||
NextFunction, | ||
} from 'express'; | ||
import type { Settings } from './types'; | ||
|
||
import bodyParser from 'body-parser'; | ||
import express from 'express'; | ||
import morgan from 'morgan'; | ||
|
||
import api from './views/api_v1'; | ||
import eventsV1 from './views/EventViews001'; | ||
|
||
// Routing | ||
import routeConfig from './lib/RouteConfig'; | ||
import UsersController from './lib/controllers/UsersController'; | ||
import WebhookController from './lib/controllers/WebhookController'; | ||
|
||
export default (settings: Settings): $Application => { | ||
const app = express(); | ||
|
||
const setCORSHeaders: Middleware = ( | ||
request: $Request, | ||
response: $Response, | ||
next: NextFunction, | ||
): mixed => { | ||
if (request.method === 'OPTIONS') { | ||
response.set({ | ||
'Access-Control-Allow-Headers': | ||
'X-Requested-With, Content-Type, Accept, Authorization', | ||
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', | ||
'Access-Control-Allow-Origin': '*', | ||
'Access-Control-Max-Age': '300', | ||
}); | ||
return response.sendStatus(204); | ||
} | ||
response.set({ 'Access-Control-Allow-Origin': '*' }); | ||
return next(); | ||
}; | ||
|
||
if (settings.logRequests) { | ||
app.use(morgan('combined')); | ||
} | ||
|
||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({ extended: true })); | ||
app.use(setCORSHeaders); | ||
|
||
routeConfig( | ||
app, | ||
[ | ||
new UsersController(settings.usersRepository), | ||
new WebhookController(settings.webhookRepository), | ||
], | ||
settings, | ||
); | ||
|
||
eventsV1.loadViews(app); | ||
api.loadViews(app); | ||
|
||
const noRouteMiddleware: Middleware = ( | ||
request: $Request, | ||
response: $Response, | ||
): mixed => response.sendStatus(404); | ||
|
||
app.use(noRouteMiddleware); | ||
|
||
return app; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// @flow | ||
|
||
import path from 'path'; | ||
import WebhookFileRepository from '../src/lib/repository/WebhookFileRepository'; | ||
import UsersFileRepository from '../src/lib/repository/UsersFileRepository'; | ||
|
||
export default { | ||
accessTokenLifetime: 7776000, // 90 days, | ||
baseUrl: 'http://localhost', | ||
coreFlashTimeout: 90000, | ||
coreKeysDir: path.join(__dirname, '__test_data__/core_keys'), | ||
coreRequestTimeout: 30000, | ||
coreSignalTimeout: 30000, | ||
isCoreOnlineTimeout: 2000, | ||
loginRoute: '/oauth/token', | ||
logRequests: false, | ||
maxHooksPerDevice: 10, | ||
maxHooksPerUser: 20, | ||
usersRepository: new UsersFileRepository( | ||
path.join(__dirname, '__test_data__/users'), | ||
), | ||
webhookRepository: new WebhookFileRepository( | ||
path.join(__dirname, '__test_data__/webhooks'), | ||
), | ||
|
||
/** | ||
* Your server crypto keys! | ||
*/ | ||
cryptoSalt: 'aes-128-cbc', | ||
serverKeyFile: "default_key.pem", | ||
serverKeyPassFile: null, | ||
serverKeyPassEnvVar: null, | ||
|
||
PORT: 5683, | ||
HOST: "localhost", | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// @flow | ||
|
||
import createApp from '../src/app'; | ||
import settings from './settings'; | ||
|
||
export default createApp(settings); |