Skip to content
This repository has been archived by the owner on Aug 28, 2021. It is now read-only.

Commit

Permalink
added CANONICAL_URL env var, used in api description etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
didimitrie committed Nov 22, 2018
1 parent 1604465 commit c8cac03
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .env-base
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
# between multiple accounts.
SERVER_NAME="Default Server"

# CANONICAL_URL: The url address (if you have any) where this server exists. For example,
# a canonical url can be "https://hestia.speckle.works". Leave blank otherwise.
CANONICAL_URL="http://localhost:3000"

# PUBLIC_STREAMS: Wether all streams created should be public (true) or private (false)
# by default.
PUBLIC_STREAMS=true
Expand Down
19 changes: 15 additions & 4 deletions app/api/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const passport = require( 'passport' )

module.exports = function ( app, express, urlRoot ) {
var r = new express.Router()
module.exports = function( app, express, urlRoot, plugins ) {
var r = new express.Router( )

// strict auth will return a 401 if no authorization header is present. pass means req.user exists
let mandatoryAuthorisation = passport.authenticate( 'jwt-strict', { session: false } )
Expand Down Expand Up @@ -153,13 +153,24 @@ module.exports = function ( app, express, urlRoot ) {
// generate routes doc
let routes = [ ]
r.stack.forEach( ( middleware ) => {
if ( middleware.route ) { routes.push( Object.keys( middleware.route.methods ).map( m => m.toUpperCase() ) + ': /api' + middleware.route.path ) }
if ( middleware.route ) { routes.push( { method: Object.keys( middleware.route.methods ).map( m => m.toUpperCase( ) )[ 0 ], route: process.env.CANONICAL_URL + '/api' + middleware.route.path } ) }
} )

let grouped = { projects: [ ], clients: [ ], streams: [ ], accounts: [ ], comments: [ ], objects: [ ] }
routes.forEach( r => {
if ( r.route.includes( 'projects' ) ) grouped.projects.push( r )
if ( r.route.includes( 'clients' ) ) grouped.clients.push( r )
if ( r.route.includes( 'comments' ) ) grouped.comments.push( r )
if ( r.route.includes( 'streams' ) ) grouped.streams.push( r )
if ( r.route.includes( 'accounts' ) ) grouped.accounts.push( r )
if ( r.route.includes( 'objects' ) ) grouped.objects.push( r )
} )

let serverDescription = {
serverName: process.env.SERVER_NAME,
version: '1.x.x',
api: routes,
api: grouped,
plugins: plugins
}

r.get( '/', ( req, res ) => res.json( serverDescription ) )
Expand Down
1 change: 1 addition & 0 deletions plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module.exports = ( ) => {
It will load from ${pl.serveFrom + '-dupe'} instead.` )
pl.serveFrom += '-dupe'
}
pl.canonicalUrl = process.env.CANONICAL_URL + pl.serveFrom
} )
winston.debug( `Found ${plugins.length} plugin(s):${plugins.map( p => ' ' + p.name )}` )

Expand Down
9 changes: 5 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ if ( cluster.isMaster ) {
plugins.forEach( plugin => {
app.use( plugin.serveFrom, express.static( path.join( __dirname, plugin.sourceDir ) ) )
} )

// expose an api
app.use( '/plugins', ( req, res ) => res.json( plugins ) )
app.use( '/api/plugins', ( req, res ) => res.json( plugins ) )

// Websockets & HTTP Servers
var http = require( 'http' )
Expand All @@ -118,9 +119,9 @@ if ( cluster.isMaster ) {

// Routes
// handle api versions gracefully
app.use( '/api/v0', ( req, res ) => res.status( 410 ).json( { error: 'The v0 API has been removed' } ) )
require( './app/api/index' )( app, express, '/api' )
require( './app/api/index' )( app, express, '/api/v1' )
app.use( '/api/v0', ( req, res ) => res.status( 410 ).json( { error: 'The v0 API has been removed.' } ) )
require( './app/api/index' )( app, express, '/api', plugins )
require( './app/api/index' )( app, express, '/api/v1', plugins )

/// /////////////////////////////////////////////////////////////////////
/// LAUNCH /////.
Expand Down

0 comments on commit c8cac03

Please sign in to comment.