Skip to content

Commit

Permalink
alonzo faucet be functions
Browse files Browse the repository at this point in the history
  • Loading branch information
martinseanhunt committed Jun 8, 2021
1 parent ce230c7 commit 3af62e2
Show file tree
Hide file tree
Showing 6 changed files with 346 additions and 4 deletions.
9 changes: 8 additions & 1 deletion .netlify.toml
Expand Up @@ -4,4 +4,11 @@
[[redirects]]
from = "/en/development-environments/native-tokens/native-tokens/"
to = "https://docs.cardano.org/en/latest/native-tokens/getting-started-with-native-tokens.html"
status = 301
status = 301

[build]
functions = "functions"
[[redirects]]
from = "/api/*"
to="/.netlify/functions/:splat"
status=200
40 changes: 40 additions & 0 deletions netlify/functions/get-addresses.js
@@ -0,0 +1,40 @@
const ObjectsToCsv = require('objects-to-csv')
const dbConnect = require('./helpers/dbConnect')

// Initialize outside of handler so it can be cached between invokations
let connection

// TODO: Netlify secret
const apiToken = process.env.ALONZO_FACUET_TOKEN

exports.handler = async function (event, context) {
// Authenticate the incoming request
if (event.headers.auth !== apiToken) return requestError('Unauthorized', 401)

// Connect to DB and intitialize the model
context.callbackWaitsForEmptyEventLoop = false
connection = await dbConnect(connection)

// Load the model
const Address = connection.model('Address')

// Get the addresses from the database
let addresses = await Address.find().select('-__v')

// remove mongo magic from the returned array
addresses = addresses.map((addr) => addr.toObject())

// Convert array to csv string
addresses = await new ObjectsToCsv(addresses).toString()

// Return the save adddress object and 201 status
return {
statusCode: 201,
body: addresses
}
}

const requestError = (message, status = 400) => ({
statusCode: status,
body: message
})
25 changes: 25 additions & 0 deletions netlify/functions/helpers/dbConnect.js
@@ -0,0 +1,25 @@
const mongoose = require('mongoose')

// TODO: netlify secret
const uri = process.env.MONGO_URI

module.exports = async function (connection) {
if (!connection) {
connection = await mongoose.createConnection(uri, {
bufferCommands: false, // Disable mongoose buffering
bufferMaxEntries: 0, // and MongoDB driver buffering
useNewUrlParser: true,
useUnifiedTopology: true
})

connection.model(
'Address',
new mongoose.Schema({
address: String,
created: { type: Date, default: Date.now() }
})
)
}

return connection
}
40 changes: 40 additions & 0 deletions netlify/functions/submit-address.js
@@ -0,0 +1,40 @@
const dbConnect = require('./helpers/dbConnect')

// Initialize outside of handler so it can be cached between invokations
let connection

exports.handler = async function (event, context) {
// Grab the address from the request body
let address
try {
address = JSON.parse(event.body).address
} catch (e) {
return requestError('Malformed JSON')
}

// Validate the provided address
if (!address || address.length !== 103) {
return requestError('Please provide a valid ADA receive address')
}

// Connect to DB and intitialize the model
context.callbackWaitsForEmptyEventLoop = false
connection = await dbConnect(connection)

// Load the model
const Address = connection.model('Address')

// Save the address to the database
const savedAddress = await new Address({ address }).save()

// Return the save adddress object and 201 status
return {
statusCode: 201,
body: JSON.stringify(savedAddress)
}
}

const requestError = (message, status = 400) => ({
statusCode: status,
body: message
})

0 comments on commit 3af62e2

Please sign in to comment.