-
Notifications
You must be signed in to change notification settings - Fork 0
/
management-api.js
97 lines (74 loc) · 2.61 KB
/
management-api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
'use strict';
const AWS = require('aws-sdk/index');
const querystring = require('querystring');
const handlebars = require('handlebars');
const waitFor = require('p-wait-for');
const linkingFormTemplate = `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>to-link a page</title>
<!-- Bootstrap -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<h1 class="site-title">to-link</h1>
</div>
<div class="row">
<form action="{{path}}shorten" method="post" id="shortenForm">
<div class="form-group">
<label for="url">Page to link</label>
<input type="url" name="url" class="form-control" placeholder="http://www.example.com"
pattern="https?://.+" required/>
</div>
<input type="submit" name="shorten" class="btn btn-default" value="Shorten"/>
</form>
</div>
</div>
</body>
</html>`;
const shorteningForm = async () => {
const template = handlebars.compile(linkingFormTemplate);
const path = process.env.SHORTENED_PATH;
const body = template({path});
return {
statusCode: 200,
headers: {
'content-type': 'text/html'
},
body
}
};
const shortenUrl = async (httpInput) => {
const stepFunctions = new AWS.StepFunctions();
const stateMachineArn = process.env.SAVE_URL;
const path = process.env.SHORTENED_PATH;
const url = querystring.parse(httpInput.body).url;
const input = JSON.stringify({url});
const params = {
stateMachineArn,
input
};
const execution = await stepFunctions.startExecution(params).promise();
let shortenResult = {};
await waitFor(async () => {
shortenResult = await stepFunctions.describeExecution({
executionArn: execution.executionArn
}).promise();
return shortenResult.hasOwnProperty('output');
});
const shortId = JSON.parse(shortenResult.output).shortId;
return {
statusCode: 303,
headers:{
location: `${path}${shortId}`
}
};
};
module.exports = {shorteningForm, shortenUrl};