Skip to content

Commit 04b3114

Browse files
author
Ethan Jon
committed
serverless hacker news submit
1 parent c8e8599 commit 04b3114

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

lambdas/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# package directories
2+
node_modules
3+
jspm_packages
4+
5+
# Serverless directories
6+
.serverless
7+
8+
npm-debug.log

lambdas/event.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"title": "The title",
3+
"url": "http://coderintros.com/"
4+
}

lambdas/handler.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
const osmosis = require('osmosis');
4+
5+
module.exports.hackerNewsSubmit = (event, context, callback) => {
6+
if (!process.env.HACKER_NEWS_USERNAME) {
7+
return callback('Missing Hacker News username');
8+
}
9+
10+
if (!process.env.HACKER_NEWS_PASSWORD) {
11+
return callback('Missing Hacker News password');
12+
}
13+
14+
if (!event.title) {
15+
return callback('Missing title');
16+
}
17+
18+
if (!event.url) {
19+
return callback('Missing URL');
20+
}
21+
22+
osmosis
23+
.get('https://news.ycombinator.com/login')
24+
.login(process.env.HACKER_NEWS_USERNAME, process.env.HACKER_NEWS_PASSWORD)
25+
.get('https://news.ycombinator.com/submit')
26+
.submit('form', {
27+
title: event.title,
28+
url: event.url
29+
})
30+
.then(window => {
31+
callback(null, {html: window.document.body.innerHTML});
32+
})
33+
.log(console.log)
34+
.error(console.log)
35+
.debug(console.log);
36+
};

lambdas/package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "lambdas",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "handler.js",
6+
"keywords": [],
7+
"author": "",
8+
"license": "ISC",
9+
"scripts": {
10+
"deploy": "serverless deploy function -f hackerNewsSubmit",
11+
"local": "serverless invoke local -f hackerNewsSubmit -p event.json",
12+
"invoke": "serverless invoke -f hackerNewsSubmit -l"
13+
},
14+
"dependencies": {
15+
"osmosis": "^1.1.4"
16+
},
17+
"engines": {
18+
"node": "6.10.3"
19+
},
20+
"devDependencies": {
21+
"serverless": "^1.17.0"
22+
}
23+
}

lambdas/serverless.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
service: coderintros
2+
3+
provider:
4+
memorySize: 128
5+
name: aws
6+
profile: serverless
7+
runtime: nodejs6.10
8+
region: us-west-2
9+
stage: prod
10+
11+
functions:
12+
hackerNewsSubmit:
13+
handler: handler.hackerNewsSubmit

0 commit comments

Comments
 (0)