Skip to content

Commit

Permalink
Save page visits with lambda function
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejmatu committed Apr 25, 2018
1 parent 15b3d5b commit 39db534
Show file tree
Hide file tree
Showing 10 changed files with 767 additions and 33 deletions.
128 changes: 128 additions & 0 deletions functions/addPageVisit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
(function(e, a) { for(var i in a) e[i] = a[i]; }(exports, /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 2);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";


const { MongoClient } = __webpack_require__(1);

// const DB_URL = 'mongodb://admin:[email protected]:39097/serverless-smoothielicious';
const DB_URL = 'mongodb://localhost:27017';
const DB_NAME = 'serverless-smoothielicious';

function connect() {
return MongoClient.connect(DB_URL);
}

module.exports.addPageVisit = () => {
return new Promise((resolve, reject) => {
connect().then(client => {
const db = client.db(DB_NAME);

db.collection('info').findOneAndUpdate({}, { $inc: { requests: 1 } }, { projection: { _id: 0 }, returnNewDocument: true }).then(result => resolve(result.value));
});
});
};

module.exports.getPageVisits = () => {
return new Promise((resolve, reject) => {
connect().then(client => {
const db = client.db(DB_NAME);

db.collection('info').findOne({}, { projection: { _id: 0 } }).then(result => resolve(result));
});
});
};

/***/ }),
/* 1 */
/***/ (function(module, exports) {

module.exports = require("mongodb");

/***/ }),
/* 2 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";


const db = __webpack_require__(0);

exports.handler = function (event, context, callback) {
db.addPageVisit().then(res => {
callback(null, {
statusCode: 200,
body: JSON.stringify(res)
});
}).catch(console.error);
};

/***/ })
/******/ ])));
129 changes: 129 additions & 0 deletions functions/getPageVisits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
(function(e, a) { for(var i in a) e[i] = a[i]; }(exports, /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 3);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";


const { MongoClient } = __webpack_require__(1);

// const DB_URL = 'mongodb://admin:[email protected]:39097/serverless-smoothielicious';
const DB_URL = 'mongodb://localhost:27017';
const DB_NAME = 'serverless-smoothielicious';

function connect() {
return MongoClient.connect(DB_URL);
}

module.exports.addPageVisit = () => {
return new Promise((resolve, reject) => {
connect().then(client => {
const db = client.db(DB_NAME);

db.collection('info').findOneAndUpdate({}, { $inc: { requests: 1 } }, { projection: { _id: 0 }, returnNewDocument: true }).then(result => resolve(result.value));
});
});
};

module.exports.getPageVisits = () => {
return new Promise((resolve, reject) => {
connect().then(client => {
const db = client.db(DB_NAME);

db.collection('info').findOne({}, { projection: { _id: 0 } }).then(result => resolve(result));
});
});
};

/***/ }),
/* 1 */
/***/ (function(module, exports) {

module.exports = require("mongodb");

/***/ }),
/* 2 */,
/* 3 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";


const db = __webpack_require__(0);

exports.handler = function (event, context, callback) {
db.getPageVisits().then(res => {
callback(null, {
statusCode: 200,
body: JSON.stringify(res)
});
}).catch(console.error);
};

/***/ })
/******/ ])));
73 changes: 73 additions & 0 deletions functions/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
(function(e, a) { for(var i in a) e[i] = a[i]; }(exports, /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {

throw new Error("Module build failed: Error: ENOENT: no such file or directory, open '/Users/maciejmatu/Documents/Projects/smoothielicious/src/lambda/test.js'");

/***/ })
/******/ ])));
6 changes: 6 additions & 0 deletions lambda-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const nodeExternals = require('webpack-node-externals');

module.exports = {
target: 'node',
externals: [nodeExternals()]
}
8 changes: 7 additions & 1 deletion netlify.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
[build]
publish = "public"
publish = "public/"
command = "npm run build"
functions = "functions/"
[build.environment]
YARN_VERSION = "1.3.2"
YARN_FLAGS = "--no-ignore-optional"
[[redirects]]
from = "/api/*"
to = "/.netlify/functions/:splat"
status = 200
force = true
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"gatsby-transformer-remark": "^1.7.40",
"gatsby-transformer-sharp": "^1.6.22",
"lodash": "^4.17.5",
"mongodb": "^3.0.7",
"netlify-cms": "^1.5.0",
"normalize.css": "^8.0.0",
"prop-types": "^15.6.1",
Expand All @@ -30,14 +31,18 @@
],
"scripts": {
"start": "npm run develop",
"build": "gatsby build",
"build": "gatsby build && npm run lambda-build",
"develop": "gatsby develop",
"serve": "gatsby serve",
"format": "prettier --trailing-comma es5 --no-semi --single-quote --write \"{gatsby-*.js,src/**/*.js}\""
"format": "prettier --trailing-comma es5 --no-semi --single-quote --write \"{gatsby-*.js,src/**/*.js}\"",
"lambda-start": "netlify-lambda -c ./lambda-config.js serve src/lambda",
"lambda-build": "netlify-lambda -c ./lambda-config.js build src/lambda"
},
"devDependencies": {
"netlify-lambda": "^0.4.0",
"postcss-cssnext": "^3.1.0",
"postcss-import": "^11.1.0",
"prettier": "^1.12.1"
"prettier": "^1.12.1",
"webpack-node-externals": "^1.7.2"
}
}
Loading

0 comments on commit 39db534

Please sign in to comment.