Skip to content

Commit 54d5de6

Browse files
Scott WinklerScott Winkler
authored andcommitted
initial commit
0 parents  commit 54d5de6

1,008 files changed

Lines changed: 177151 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#bin
2+
#obj
3+
csx
4+
.vs
5+
edge
6+
#Publish
7+
8+
*.user
9+
*.suo
10+
*.cscfg
11+
*.Cache
12+
project.lock.json
13+
14+
#/packages
15+
/TestResults
16+
17+
/tools/NuGet.exe
18+
/App_Data
19+
/secrets
20+
/data
21+
.secrets
22+
appsettings.json
23+
local.settings.json
24+
25+
#node_modules — need this
26+
#dist
27+
28+
# Local python packages
29+
.python_packages/
30+
31+
# Python Environments
32+
.env
33+
.venv
34+
env/
35+
venv/
36+
ENV/
37+
env.bak/
38+
venv.bak/
39+
40+
# Byte-compiled / optimized / DLL files
41+
__pycache__/
42+
*.py[cod]
43+
*$py.class
44+
.DS_Store

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# terraform-azure-ballroom
2+
Source code for serverless functions on Azure

main.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
data "archive_file" "code_package" {
2+
type = "zip"
3+
source_dir = "${path.module}/src"
4+
output_path = "${path.module}/dist/server.zip"
5+
}
6+
7+
output "output_path" {
8+
value = data.archive_file.code_package.output_path
9+
}

src/api/function.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"bindings": [
3+
{
4+
"authLevel": "anonymous",
5+
"type": "httpTrigger",
6+
"direction": "in",
7+
"name": "req",
8+
"methods": [
9+
"get",
10+
"post",
11+
"delete",
12+
"patch"
13+
],
14+
"route": "api/{action}/{id?}"
15+
},
16+
{
17+
"type": "http",
18+
"direction": "out",
19+
"name": "res"
20+
}
21+
],
22+
"disabled": false
23+
}

src/api/index.js

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
var azure = require('azure-storage');
2+
var entGen = azure.TableUtilities.entityGenerator;
3+
var tableService = azure.createTableService(process.env.TABLES_CONNECTION_STRING);
4+
const tableName = "tweets"
5+
const uuidv4 = require('uuid/v4')
6+
7+
// using table storage as a kind of "serverless" NoSQL database
8+
module.exports = function (context, req) {
9+
if (req.params.action === "tweet") {
10+
handleTweet(context)
11+
} else {
12+
context.res = {
13+
status: 404
14+
}
15+
}
16+
};
17+
18+
async function handleTweet(context) {
19+
tableService.createTableIfNotExists(tableName, function (error, result, response) {
20+
if (!error) {
21+
switch (context.req.method) {
22+
case "POST":
23+
createTweet(context)
24+
break
25+
case "GET":
26+
readTweet(context)
27+
break
28+
case "PATCH":
29+
updateTweet(context)
30+
break
31+
case "DELETE":
32+
deleteTweet(context)
33+
break
34+
}
35+
}
36+
});
37+
}
38+
39+
function createTweet(context) {
40+
let message = context.req.body.message
41+
let name = context.req.body.name
42+
let entity = {
43+
PartitionKey: entGen.String(name),
44+
RowKey: entGen.String(uuidv4()),
45+
message: entGen.String(message),
46+
};
47+
tableService.insertEntity(tableName, entity, function (error, result, response) {
48+
if (!error) {
49+
context.res = {
50+
status: 201
51+
}
52+
} else {
53+
context.log(error)
54+
context.res = {
55+
status: 400
56+
}
57+
}
58+
context.done()
59+
});
60+
}
61+
62+
function transformTweet(record) {
63+
return {
64+
uuid: record.RowKey._,
65+
name: record.PartitionKey._,
66+
message: record.message._,
67+
timestamp: record.Timestamp._
68+
}
69+
}
70+
71+
function readTweet(context) {
72+
let uuid = context.req.params.id
73+
let list = uuid === undefined
74+
if (list) {
75+
tableService.queryEntities(tableName, null, null, function (error, result) {
76+
if (!error) {
77+
let tweets = result.entries.map(function(e){return transformTweet(e)})
78+
context.res = {
79+
headers: {"Content-Type":"text/json"},
80+
status: 200,
81+
body: JSON.stringify(tweets)
82+
}
83+
} else {
84+
context.res = {
85+
status: 500
86+
}
87+
}
88+
context.done()
89+
return
90+
})
91+
} else{
92+
let name = context.req.query.name
93+
tableService.retrieveEntity(tableName, name, uuid, function (error, result, response) {
94+
if (!error) {
95+
let tweet = transformTweet(result)
96+
context.res = {
97+
headers: {"Content-Type":"text/json"},
98+
status: 200,
99+
body: JSON.stringify(tweet)
100+
}
101+
} else {
102+
context.res = {
103+
status: 404
104+
}
105+
}
106+
context.done()
107+
});
108+
}
109+
110+
}
111+
112+
function updateTweet(context) {
113+
let uuid = context.req.params.id
114+
let entity = {
115+
PartitionKey: entGen.String(context.req.body.name),
116+
RowKey: entGen.String(context.req.body.uuid),
117+
message: entGen.String(context.req.body.message)
118+
};
119+
tableService.insertOrReplaceEntity(tableName, entity, function (error, result, response) {
120+
if (!error) {
121+
context.res = {
122+
status: 202
123+
}
124+
} else {
125+
context.res = {
126+
status: 400
127+
}
128+
}
129+
context.done()
130+
});
131+
}
132+
133+
function deleteTweet(context) {
134+
let uuid = context.req.params.id
135+
let name = context.req.query.name
136+
var entity = {
137+
PartitionKey: entGen.String(name),
138+
RowKey: entGen.String(uuid),
139+
};
140+
tableService.deleteEntity(tableName, entity, function (error, result, response) {
141+
if (!error) {
142+
context.res = {
143+
status: 202
144+
}
145+
} else {
146+
context.res = {
147+
status: 404
148+
}
149+
}
150+
context.done()
151+
});
152+
}

src/host.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"version": "2.0",
3+
"extensions": {
4+
"http": {
5+
"routePrefix": ""
6+
}
7+
}
8+
}

src/node_modules/.bin/sshpk-conv

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/node_modules/.bin/sshpk-sign

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/node_modules/.bin/sshpk-verify

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/node_modules/.bin/uuid

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)