Skip to content

Commit

Permalink
chore: add sample app
Browse files Browse the repository at this point in the history
It is a static file server that uses `fastify` and the
`fastify-auto-push` plugin.
  • Loading branch information
jinwoo committed Jan 18, 2018
1 parent a1ea9d9 commit 72337d9
Show file tree
Hide file tree
Showing 85 changed files with 1,776 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/.vscode
/build
/coverage
/node_modules
node_modules
30 changes: 30 additions & 0 deletions samples/static-page/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Sample code using the `fastify-auto-push` plugin

**This is not an official Google product.**

This application is a simple static file server built on top of `fastify` and
`fastify-auto-push`.

## Command-line flags

* `--port <number>` or `-p <number>`

Specifies the port number to be used. Defaults to 3000.

* `--http2` or `--h2`

Use HTTP/2 when specified, HTTP/1 otherwise.

* `--auto-push` or `--ap`

Enable auto-push. Works only with `--http2`.

## Copyright of the static contents

CC BY-SA

The files under the `static` directory are obtained from the [Wikipedia page
on Node.js](https://en.wikipedia.org/wiki/Node.js).

See https://en.wikipedia.org/wiki/Wikipedia:Copyrights for the Wikipedia's
copyright details.
19 changes: 19 additions & 0 deletions samples/static-page/certs/server.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-----BEGIN CERTIFICATE-----
MIIDEjCCAfoCCQCerbweIrWbljANBgkqhkiG9w0BAQsFADBLMQswCQYDVQQGEwJV
UzETMBEGA1UECAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzEP
MA0GA1UECgwGR29vZ2xlMB4XDTE3MTAwNjE3MjAzOVoXDTE4MTAwNjE3MjAzOVow
SzELMAkGA1UEBhMCVVMxEzARBgNVBAgMCkNhbGlmb3JuaWExFjAUBgNVBAcMDU1v
dW50YWluIFZpZXcxDzANBgNVBAoMBkdvb2dsZTCCASIwDQYJKoZIhvcNAQEBBQAD
ggEPADCCAQoCggEBALkntxB80rgJz8vVoHTIyS3EKL73RpaDXuEigtvo+PjHdpWn
fs+Q+jbmoPfqwmSdlUxL7XdtSCYjT1mp0j5cfxBTJe7w/ZBvPQBmQXjWpNOvA1oj
LRZShYyo35BQI17fVEJPd3cCn8mwmi1VZgyNXpa4oTi0SicE8/EfBiYWZnBFogLe
jR0YOBj9gobqgRzwqybb/Ex0PT5+r2bHlT5gY5bMUbLPALsAFsDZRXzNmc1In/AN
/Sf9M3Msyp8OcTNCHJXxiQrVamLzxhlSCRrbcslwy3QTiEtoGpskTrxPdggCaT9j
EF+Ttdr5GLpHKGs2+kgZJcrTR8vXQuWhLLDq8+sCAwEAATANBgkqhkiG9w0BAQsF
AAOCAQEADfyK/lF8zWacMCWTTo7pKJKCCp5alzP1lLyfJ3DbBOpizeQH0X1YK8UW
GVyEUFqtv0bdeL/OOzO3HZoc/H9SYnj0OdzJqKwpc6B9oATy2VgsFKJgy0TP9df8
zAY05V5ik9PzoAnBS4nN/sIXYxSVuVY+BszHPYgYWZaJM2Md2d8LJUE1EaP/irJr
Mt46FhDcbSsZyAQhScNQz2vLGmX17y6LCL+jNtNLSBxa6J8nDaTVqeOu2bvfl+ij
CDwXAUUMmhzv5K/MnauOB91NNQKsYjRYSURiI/SDlY0IhQ/YoS9G4g20vD4Bqm6V
BdyEQExykaJ/+Gy/DOj3UBbvznsevw==
-----END CERTIFICATE-----
17 changes: 17 additions & 0 deletions samples/static-page/certs/server.csr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-----BEGIN CERTIFICATE REQUEST-----
MIICqDCCAZACAQAwSzELMAkGA1UEBhMCVVMxEzARBgNVBAgMCkNhbGlmb3JuaWEx
FjAUBgNVBAcMDU1vdW50YWluIFZpZXcxDzANBgNVBAoMBkdvb2dsZTCCASIwDQYJ
KoZIhvcNAQEBBQADggEPADCCAQoCggEBALkntxB80rgJz8vVoHTIyS3EKL73RpaD
XuEigtvo+PjHdpWnfs+Q+jbmoPfqwmSdlUxL7XdtSCYjT1mp0j5cfxBTJe7w/ZBv
PQBmQXjWpNOvA1ojLRZShYyo35BQI17fVEJPd3cCn8mwmi1VZgyNXpa4oTi0SicE
8/EfBiYWZnBFogLejR0YOBj9gobqgRzwqybb/Ex0PT5+r2bHlT5gY5bMUbLPALsA
FsDZRXzNmc1In/AN/Sf9M3Msyp8OcTNCHJXxiQrVamLzxhlSCRrbcslwy3QTiEto
GpskTrxPdggCaT9jEF+Ttdr5GLpHKGs2+kgZJcrTR8vXQuWhLLDq8+sCAwEAAaAY
MBYGCSqGSIb3DQEJBzEJDAdub2RlLmpzMA0GCSqGSIb3DQEBCwUAA4IBAQAqthEt
FvKd02FjOVddUEiFu3AAYmN/29hJKt4qot05bnBzM4AgBd2GwFPdZSlaIZm05cNb
omNu+aHFUQEOjcSH185cA2SePjRhjN77AKklvXzzEXwjtiySzSWSTCkXhGs/Y+ve
CaE1QtH3uYENQwxatVBau78LVIcTJ5/ErHOX8cReOyDoLmqMKhvQxjmjecBJgvR7
IsbkdDTI65dklDxj8xqBEgLc7SK8Q1+6fzua93YjCK2mjpu1sTWmRfvunfImanQG
Pn9crmmVPt4SCmBhc+WvCUz8H6nm9fnisPg7DyYXkEg/fLLSXfM3MIDgbARfjt9v
I8JhT5qCCo8fW60i
-----END CERTIFICATE REQUEST-----
27 changes: 27 additions & 0 deletions samples/static-page/certs/server.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAuSe3EHzSuAnPy9WgdMjJLcQovvdGloNe4SKC2+j4+Md2lad+
z5D6Nuag9+rCZJ2VTEvtd21IJiNPWanSPlx/EFMl7vD9kG89AGZBeNak068DWiMt
FlKFjKjfkFAjXt9UQk93dwKfybCaLVVmDI1elrihOLRKJwTz8R8GJhZmcEWiAt6N
HRg4GP2ChuqBHPCrJtv8THQ9Pn6vZseVPmBjlsxRss8AuwAWwNlFfM2ZzUif8A39
J/0zcyzKnw5xM0IclfGJCtVqYvPGGVIJGttyyXDLdBOI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0QJ6U7GbsrnVIJ3khZizBOJH47BZtMr+qyIwVZ4T
tDo4iQKBgHDPlIbBgkF93FQ6AyIfiEB3g8MjLUzKlAZNwMUG4ux+V4dCq4rrG7ip
zop4+CsROTwwPi66XrQ5/uWYjswVktESriJ+WARu04BEjxukblyM4Yw/RHOEgv5P
xPDJRhPIrBH3Zf8plT0yAPdlwk/hPo6mbx8IGuSSI67lUJSm914a
-----END RSA PRIVATE KEY-----
85 changes: 85 additions & 0 deletions samples/static-page/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2018 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

const {ArgumentParser} = require('argparse');
const fastify = require('fastify');
const fastifyAutoPush = require('fastify-auto-push');
const fastifyStatic = require('fastify-static');
const fs = require('fs');
const path = require('path');
const {promisify} = require('util');

const {description, version} = require('./package.json');

const argParser = new ArgumentParser({
version,
description,
addHelp: true,
});
argParser.addArgument(['--port', '-p'], {
type: Number,
defaultValue: 3000,
help: 'Port number. Defaults to 3000.',
});
argParser.addArgument(['--http2', '--h2'], {
nargs: 0,
help: 'Use HTTP/2. Defaults to true.',
});
argParser.addArgument(['--auto-push', '--ap'], {
nargs: 0,
dest: 'autoPush',
help: 'Enable auto-push. Works only with --http2.',
});
const args = argParser.parseArgs();
if (args.autoPush && !args.http2) {
console.warn('--auto-push is supported only with --http2. Ignoring.');
args.autoPush = false;
}

const fsReadFile = promisify(fs.readFile);

const STATIC_DIR = path.join(__dirname, 'static');
const CERTS_DIR = path.join(__dirname, 'certs');

async function createServerOptions() {
const readCertFile = (filename) => {
return fsReadFile(path.join(CERTS_DIR, filename));
};
const [key, cert] = await Promise.all(
[readCertFile('server.key'), readCertFile('server.crt')]);
return {key, cert};
}

async function main() {
const {key, cert} = await createServerOptions();
const app = fastify({
https: {key, cert},
http2: args.http2,
});
if (args.autoPush) {
// Create and register AutoPush plugin. It should be registered as the first
// in the middleware chain.
app.register(fastifyAutoPush.staticServe, {root: STATIC_DIR});
} else {
app.register(fastifyStatic, {root: STATIC_DIR});
}
app.listen(args.port, (err) => {
if (err) throw err;
console.log(`Listening on port ${args.port}`);
});
}

main().catch((err) => {
console.error(err);
});
Loading

0 comments on commit 72337d9

Please sign in to comment.