Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Winkler authored and Scott Winkler committed Dec 13, 2020
0 parents commit 54d5de6
Show file tree
Hide file tree
Showing 1,008 changed files with 177,151 additions and 0 deletions.
44 changes: 44 additions & 0 deletions .gitignore
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# terraform-azure-ballroom
Source code for serverless functions on Azure
9 changes: 9 additions & 0 deletions main.tf
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
}
23 changes: 23 additions & 0 deletions src/api/function.json
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
}
152 changes: 152 additions & 0 deletions src/api/index.js
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()
});
}
8 changes: 8 additions & 0 deletions src/host.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"version": "2.0",
"extensions": {
"http": {
"routePrefix": ""
}
}
}
1 change: 1 addition & 0 deletions src/node_modules/.bin/sshpk-conv

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/node_modules/.bin/sshpk-sign

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/node_modules/.bin/sshpk-verify

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/node_modules/.bin/uuid

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/node_modules/ajv/.tonic_example.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions src/node_modules/ajv/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 54d5de6

Please sign in to comment.