-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#bin | ||
#obj | ||
csx | ||
.vs | ||
edge | ||
#Publish | ||
|
||
*.user | ||
*.suo | ||
*.cscfg | ||
*.Cache | ||
project.lock.json | ||
|
||
#/packages | ||
/TestResults | ||
|
||
/tools/NuGet.exe | ||
/App_Data | ||
/secrets | ||
/data | ||
.secrets | ||
appsettings.json | ||
local.settings.json | ||
|
||
#node_modules — need this | ||
#dist | ||
|
||
# Local python packages | ||
.python_packages/ | ||
|
||
# Python Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
.DS_Store |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# terraform-azure-ballroom | ||
Source code for serverless functions on Azure |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
data "archive_file" "code_package" { | ||
type = "zip" | ||
source_dir = "${path.module}/src" | ||
output_path = "${path.module}/dist/server.zip" | ||
} | ||
|
||
output "output_path" { | ||
value = data.archive_file.code_package.output_path | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"bindings": [ | ||
{ | ||
"authLevel": "anonymous", | ||
"type": "httpTrigger", | ||
"direction": "in", | ||
"name": "req", | ||
"methods": [ | ||
"get", | ||
"post", | ||
"delete", | ||
"patch" | ||
], | ||
"route": "api/{action}/{id?}" | ||
}, | ||
{ | ||
"type": "http", | ||
"direction": "out", | ||
"name": "res" | ||
} | ||
], | ||
"disabled": false | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
var azure = require('azure-storage'); | ||
var entGen = azure.TableUtilities.entityGenerator; | ||
var tableService = azure.createTableService(process.env.TABLES_CONNECTION_STRING); | ||
const tableName = "tweets" | ||
const uuidv4 = require('uuid/v4') | ||
|
||
// using table storage as a kind of "serverless" NoSQL database | ||
module.exports = function (context, req) { | ||
if (req.params.action === "tweet") { | ||
handleTweet(context) | ||
} else { | ||
context.res = { | ||
status: 404 | ||
} | ||
} | ||
}; | ||
|
||
async function handleTweet(context) { | ||
tableService.createTableIfNotExists(tableName, function (error, result, response) { | ||
if (!error) { | ||
switch (context.req.method) { | ||
case "POST": | ||
createTweet(context) | ||
break | ||
case "GET": | ||
readTweet(context) | ||
break | ||
case "PATCH": | ||
updateTweet(context) | ||
break | ||
case "DELETE": | ||
deleteTweet(context) | ||
break | ||
} | ||
} | ||
}); | ||
} | ||
|
||
function createTweet(context) { | ||
let message = context.req.body.message | ||
let name = context.req.body.name | ||
let entity = { | ||
PartitionKey: entGen.String(name), | ||
RowKey: entGen.String(uuidv4()), | ||
message: entGen.String(message), | ||
}; | ||
tableService.insertEntity(tableName, entity, function (error, result, response) { | ||
if (!error) { | ||
context.res = { | ||
status: 201 | ||
} | ||
} else { | ||
context.log(error) | ||
context.res = { | ||
status: 400 | ||
} | ||
} | ||
context.done() | ||
}); | ||
} | ||
|
||
function transformTweet(record) { | ||
return { | ||
uuid: record.RowKey._, | ||
name: record.PartitionKey._, | ||
message: record.message._, | ||
timestamp: record.Timestamp._ | ||
} | ||
} | ||
|
||
function readTweet(context) { | ||
let uuid = context.req.params.id | ||
let list = uuid === undefined | ||
if (list) { | ||
tableService.queryEntities(tableName, null, null, function (error, result) { | ||
if (!error) { | ||
let tweets = result.entries.map(function(e){return transformTweet(e)}) | ||
context.res = { | ||
headers: {"Content-Type":"text/json"}, | ||
status: 200, | ||
body: JSON.stringify(tweets) | ||
} | ||
} else { | ||
context.res = { | ||
status: 500 | ||
} | ||
} | ||
context.done() | ||
return | ||
}) | ||
} else{ | ||
let name = context.req.query.name | ||
tableService.retrieveEntity(tableName, name, uuid, function (error, result, response) { | ||
if (!error) { | ||
let tweet = transformTweet(result) | ||
context.res = { | ||
headers: {"Content-Type":"text/json"}, | ||
status: 200, | ||
body: JSON.stringify(tweet) | ||
} | ||
} else { | ||
context.res = { | ||
status: 404 | ||
} | ||
} | ||
context.done() | ||
}); | ||
} | ||
|
||
} | ||
|
||
function updateTweet(context) { | ||
let uuid = context.req.params.id | ||
let entity = { | ||
PartitionKey: entGen.String(context.req.body.name), | ||
RowKey: entGen.String(context.req.body.uuid), | ||
message: entGen.String(context.req.body.message) | ||
}; | ||
tableService.insertOrReplaceEntity(tableName, entity, function (error, result, response) { | ||
if (!error) { | ||
context.res = { | ||
status: 202 | ||
} | ||
} else { | ||
context.res = { | ||
status: 400 | ||
} | ||
} | ||
context.done() | ||
}); | ||
} | ||
|
||
function deleteTweet(context) { | ||
let uuid = context.req.params.id | ||
let name = context.req.query.name | ||
var entity = { | ||
PartitionKey: entGen.String(name), | ||
RowKey: entGen.String(uuid), | ||
}; | ||
tableService.deleteEntity(tableName, entity, function (error, result, response) { | ||
if (!error) { | ||
context.res = { | ||
status: 202 | ||
} | ||
} else { | ||
context.res = { | ||
status: 404 | ||
} | ||
} | ||
context.done() | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"version": "2.0", | ||
"extensions": { | ||
"http": { | ||
"routePrefix": "" | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.