Skip to content
This repository has been archived by the owner on Sep 29, 2024. It is now read-only.

Commit

Permalink
New: Project scaffolding
Browse files Browse the repository at this point in the history
  • Loading branch information
molant committed Apr 5, 2018
1 parent b34cc88 commit c08c5d4
Show file tree
Hide file tree
Showing 17 changed files with 11,326 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.vscode/
dist/
node_modules/
src/rev-manifest.json
10 changes: 10 additions & 0 deletions App_data/jobs/triggered/notify/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { Subscription } = require('../../../../db/db');

const init = async () => {
// Get all subscriptions here via `Subscription.find()
// More info in http://mongoosejs.com/docs/api.html#find_find

console.log('Job executed correctly');
};

init();
3 changes: 3 additions & 0 deletions App_data/jobs/triggered/notify/run.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cd D:\home\site\wwwroot\App_Data\jobs\triggered\notify
D:\home\site\wwwroot\bin\node.exe index.js
exit $LASTEXITCODE
3 changes: 3 additions & 0 deletions App_data/jobs/triggered/notify/settings.job
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"schedule": "0 0 4 * * *"
}
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,42 @@
# pushnotifications-demo
Demo for cross browsers push notifications with server side code

Demo for cross browsers push notifications.

## How to use

This demo requires access to a mongodb instance and a few environment variables:

* `connectionString`: The connection string to your `mongodb`
* `publickey`: The public key to share
* `privatekey`: The private key to use

If you are using VS Code you can set them in your `launch.json` file as follows:

```js
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch demo",
"program": "${workspaceFolder}\\index.js",
"env": {
"connectionString": "YOUR CONNECTION STRING",
"publickey": "YOUR PUBLIC KEY",
"privatekey": "YOUR PRIVATE KEY",
}
}
]
}
```

Additionally you can also modify `index.js` and `db/db.js` and set the values there explicitely:

```js
const connectionString = process.env.connectionString || 'YOUR CONNECTION STRING';
const public = process.env.publickey || 'PUBLIC KEY';
const private = process.env.privatekey || 'PRIVATE KEY';
```

To execute the site just run `node index.js` and a server will start in port `4000`
Binary file added bin/chakracore.dll
Binary file not shown.
Binary file added bin/node.exe
Binary file not shown.
31 changes: 31 additions & 0 deletions db/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const connectionString = process.env.connectionString || 'YOUR CONNECTION STRING';

const mongoose = require('mongoose');
mongoose.Promise = global.Promise;

const { Schema } = mongoose;

const SubscriptionSchema = new Schema({
endPoint: {
type: String,
index: true
},
hash: { type: String },
key: { type: String }
});

const Subscription = mongoose.model('Subscription', SubscriptionSchema);

const connect = async () => {
try {
await mongoose.connect(connectionString);
} catch (e) {

}
}

connect();

module.exports = {
Subscription
};
128 changes: 128 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
const gulp = require('gulp');
const plugins = require('gulp-load-plugins')();
const del = require('del');

const dirs = {
dist: 'dist',
compresseable: 'dist/**/*.{css,html,ico,js,svg,txt,xml,webmanifest}',
src: 'src'
};

// ---------------------------------------------------------------------
// | Helper tasks |
// ---------------------------------------------------------------------

gulp.task('clean', () => {
return del(dirs.dist);
});

// Copies all the files that are not taken care but optimizations into `dist`
gulp.task('copy:others', () => {
return gulp.src(`${dirs.src}/**/*.ico`)
.pipe(gulp.dest(`${dirs.dist}`));
});

// Optimizes JS using uglify saving the results into `dist`
gulp.task('optimize:js', () => {
return gulp.src(`${dirs.src}/**/*.js`)
.pipe(plugins.uglify())
.pipe(gulp.dest(dirs.dist));
});

// Optimizes CSS using clean css saving the results into `dist`
gulp.task('optimize:css', () => {
return gulp.src(`${dirs.src}/**/*.css`)
.pipe(plugins.cleanCss())
.pipe(gulp.dest(dirs.dist));
});

// Cache busts the static files in `dist` deleting the source file (also from `dist`)
gulp.task('revfiles', () => {
return gulp.src([`${dirs.dist}/{images,styles,scripts}/**/*`])
.pipe(plugins.rev())
.pipe(plugins.revDeleteOriginal())
.pipe(gulp.dest(`${dirs.dist}`))
.pipe(plugins.rev.manifest())
.pipe(gulp.dest(`${dirs.dist}`));
});

// Updates all the references to static assets using the manifest created in `revfiles`
gulp.task('revreplace', () => {
const manifest = gulp.src(`${dirs.dist}/rev-manifest.json`);

return gulp.src(`${dirs.dist}/**/*`)
.pipe(plugins.revReplace({
manifest
}))
.pipe(gulp.dest(dirs.dist));
});

// Optimizes HTML files using htmlmin saving the results into `dist`
gulp.task('optimize:html', () => {

const htmlminOptions = {
caseSensitive: true,
collapseBooleanAttributes: false,
collapseWhitespace: true,
minifyCSS: true,
minifyJS: true,
preserveLineBreak: true,
removeAttributeQuotes: false,
removeComments: true,
removeCommentsFromCDATA: false,
removeEmptyAttributes: false,
removeOptionalTags: false,
removeRedundantAttributes: false
};

return gulp.src(`${dirs.src}/**/*.html`)
.pipe(plugins.htmlmin(htmlminOptions))
.pipe(gulp.dest(dirs.dist));
});

// Optimizes images using imagemin and saving into `dist`
gulp.task('imagemin', () => {
return gulp.src(`${dirs.src}/**/*.{gif,ico,jpg,png,svg}`)
.pipe(plugins.imagemin())
.pipe(gulp.dest(dirs.dist));
});

// Adds the `sri` attribute for CSS and JS of all HTML files in `dist`
gulp.task('sri', () => {
return gulp.src(`${dirs.dist}/**/*.html`)
.pipe(plugins.sriHash())
.pipe(gulp.dest(dirs.dist));
});

// Compresses all the compreseable files in `dist` using zopfli
gulp.task('compress:zopfli', () => {
return gulp.src(dirs.compresseable)
.pipe(plugins.zopfli())
.pipe(gulp.dest(dirs.dist));
});

// Compresses all the compreseable files in `dist` using Brotli
gulp.task('compress:brotli', () => {
return gulp.src(dirs.compresseable)
.pipe(plugins.brotli.compress())
.pipe(gulp.dest(dirs.dist));
});

// ---------------------------------------------------------------------
// | Main tasks |
// ---------------------------------------------------------------------

gulp.task('build', gulp.series(
'clean',
'optimize:js',
'optimize:css',
'imagemin',
'optimize:html',
'revfiles',
'revreplace',
'sri',
'compress:zopfli',
'compress:brotli'
));

gulp.task('default', gulp.series('build'));
1 change: 1 addition & 0 deletions iisnode.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeProcessCommandLine: "D:\home\site\wwwroot\bin\node.exe"
53 changes: 53 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const express = require('express');

const { Subscription } = require('./db/db');

const app = express();
const port = process.env.PORT || 4000;
const public = process.env.publickey || 'PUBLIC KEY';
const private = process.env.privatekey || 'PRIVATE KEY';
const static = process.env.NODE_ENV === 'production' ? 'dist' : 'src';

// Express configuration
app.disable('x-powered-by');

app.use(express.static(static));

app.get('/api/key', (req, res) => {
res.send({
key: public
});
});

app.post('/api/subscribe', async (req, res) => {
try {
// Find if user is already subscribed searching by `endPoint`
const { key, userId } = req.body;
const exists = await Subscription.findOne({ endPoint: 'endPoint' });

if (exists) {
// Maybe refresh the subscription info?
res.status(400).send('Subscription already exists');

return;
}

const subscription = new Subscription({
subscription: '',
hash: '',
key: ''
});

await subscription.save();

res.status(200);

} catch (e) {
res.status(500)
.send(e.message);
}
});

app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
Loading

0 comments on commit c08c5d4

Please sign in to comment.