Skip to content

Commit

Permalink
Inline all server-side routes with static imports
Browse files Browse the repository at this point in the history
  • Loading branch information
aral committed Mar 10, 2021
1 parent b72f3e3 commit c6f2ba9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
30 changes: 21 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import chokidar from 'chokidar'
import chalk from 'chalk'

import https from '@small-tech/https'
import expressWebSocket from '@small-tech/express-ws'
import crossPlatformHostname from '@small-tech/cross-platform-hostname'

import clr from './lib/clr.js'
Expand All @@ -35,8 +36,6 @@ import errors from './lib/errors.js'
import Util from './lib/Util.js'

import ensureDomainsAreReachable from './lib/ensure-domains-are-reachable.js'
import addHttpsGetRoutes from './lib/add-https-get-routes.js'
import createWebSocketServer from './lib/create-websocket-server.js'
import initialiseDatabase from './lib/initialise-database.js'

// Middleware
Expand All @@ -48,6 +47,12 @@ import gitServer from './middleware/git-server.js'
import error404 from './middleware/error-404.js'
import error500 from './middleware/error-500.js'

// Routes
import { httpsRoutes } from './routes/https/index.js'
import { wssRoutes } from './routes/wss/index.js'

import { setPublicKeys } from './lib/public-keys.js'

// For compatibility with legacy CommonJS code.
import { createRequire } from 'module'
const __dirname = new URL('.', import.meta.url).pathname
Expand Down Expand Up @@ -225,6 +230,10 @@ class Place {
const placeKeysPath = path.join(this.placePath, 'public-keys.json')
Place.publicKeys = JSON.parse(fs.readFileSync(placeKeysPath, 'utf-8'))

// Make it available for other modules to
setPublicKeys(Place.publicKeys)


//
// Create the Express app. We will configure it later.
//
Expand All @@ -247,7 +256,7 @@ class Place {

// The app configuration is handled in an asynchronous method
// as there is a chance that we will have to wait for generated content.
async configureApp () {
configureApp () {

// Express.js security with HTTP headers.
this.app.use(helmet())
Expand All @@ -273,8 +282,9 @@ class Place {
this.app.get(this.stats.route, this.stats.view)

// Add HTTPS GET routes.
const httpsGetRoutesDirectory = path.join(__dirname, 'routes', 'https')
await addHttpsGetRoutes(httpsGetRoutesDirectory, this.app)
Object.keys(httpsRoutes).forEach(routePath => {
this.app.get(routePath, httpsRoutes[routePath])
})

// Middleware: static routes.
this.app.use(express.static(this.pathToServe))
Expand All @@ -284,10 +294,12 @@ class Place {
this.log(` 🗄️ ❨Place❩ Serving source code repositories at /source/…`)

// Create WebSocket server, add WebSocket routes, and integrate with Express app.
const wssRoutesDirectory = path.join(__dirname, 'routes', 'wss')
await createWebSocketServer(this.app, this.server, wssRoutesDirectory)
expressWebSocket(this.app, this.server, { perMessageDeflate: false })
Object.keys(wssRoutes).forEach(routePath => {
this.app.ws(routePath, wssRoutes[routePath])
})

// Note: ensure error roots remain added last.
// Note: please ensure that error roots remain added last.

// 404 (Not Found) support.
this.app.use(error404(this.pathToServe))
Expand Down Expand Up @@ -411,7 +423,7 @@ class Place {

// Before starting the server, we have to configure the app. We do this here
// instead of earlier in the constructor since the process is asynchronous.
await this.configureApp()
this.configureApp()

// Create the file watcher to watch for changes on dynamic routes.
this.createFileWatcher()
Expand Down
9 changes: 9 additions & 0 deletions lib/public-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let _keys = null

export function setPublicKeys (keys) {
_keys = keys
}

export function getPublicKeys () {
return _keys
}
11 changes: 11 additions & 0 deletions routes/https/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import hostname from './hostname.js'
import keys from './keys.js'
import privateSocket from './private-socket.js'
import test500Error from './test-500-error.js'

export const httpsRoutes = {
'/hostname': hostname,
'/keys': keys,
'/private-socket': privateSocket,
'/test-500-error': test500Error
}
10 changes: 3 additions & 7 deletions routes/https/private-socket.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Place from '../../index.js'
import { getPublicKeys } from '../../lib/public-keys.js'

import nacl from 'tweetnacl'
import naclUtil from 'tweetnacl-util'
import sealedBox from 'tweetnacl-sealedbox-js'

// Check every minute and prune sign-in paths that haven’t been used in the last ten seconds.
Expand All @@ -18,12 +17,9 @@ setInterval(() => {
console.log('After prune', db.privateRoutes)
}, 1 /* minute */ * 60 * 1000)


// const publicEncryptionKey = new X25519PublicKey(Buffer.from(Place.publicKeys.encryption, 'hex'))

const publicEncryptionKey = Buffer.from(Place.publicKeys.encryption, 'hex')

export default (request, response) => {
const publicEncryptionKey = Buffer.from(getPublicKeys().encryption, 'hex')

// Generate a new private path fragment. This is the
// hexadecimal representation of a 32-byte random buffer.
const randomBytes = nacl.randomBytes(32)
Expand Down
6 changes: 6 additions & 0 deletions routes/wss/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import privateToken from './private_token.js'

export const wssRoutes = {
'/private/:token': privateToken
}

0 comments on commit c6f2ba9

Please sign in to comment.