Created
January 30, 2020 17:31
-
-
Save emiling/5333eeafa9971aa0d2a8de658cfae15c to your computer and use it in GitHub Desktop.
ausg-seminar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const serverless = require('serverless-http') | |
const createRouter = require('./routes/createURL') | |
/* TODO: delete module 구현하기*/ | |
// const deleteRouter = require('./routes/redirectURL') | |
const app = express() | |
app.use(express.json()) | |
app.use('/create', createRouter) | |
// app.use('/delete', deleteRouter) | |
module.exports.handler = serverless(app) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const express = require('express') | |
const AWS = require('aws-sdk') | |
const shortID = require('shortid') | |
const dynamoDB = new AWS.DynamoDB.DocumentClient() | |
const router = express.Router() | |
const HOST_DOMAIN = '***' | |
const validateURL = (raw) => { | |
const regex = new RegExp('^(https?:\\/\\/)?'+ // protocol | |
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name | |
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address | |
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path | |
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string | |
'(\\#[-a-z\\d_]*)?$','i' | |
) | |
return regex.test(raw) | |
} | |
const checkURL = (raw) => { | |
if(!raw) return { message : 'URL을 입력해주세요'} | |
if(!validateURL(raw)) return { message: '올바른 형태의 URL을 입력해주세요'} | |
} | |
function isExistURL(params) { | |
return new Promise((resolve, reject) => { | |
dynamoDB.get(params, (err, data) => { | |
if(err) reject(err) | |
else resolve(data) | |
}) | |
}) | |
} | |
router.post('/', async(req, res) => { | |
const { raw } = req.body; | |
const urlError = checkURL(raw) | |
if(urlError) return res.status(400).send(urlError) | |
const uniqueID = shortID.generate() | |
const newURLData = { | |
TableName: 'URLTable', | |
Item: { | |
raw_url : raw, | |
short_url: uniqueID, | |
created_at: Date.now() | |
} | |
} | |
dynamoDB.put(newURLData, (err, data) => { | |
if(err) res.status(500).send({ message: '데이터베이스 추가에 실패했습니다', error: err }) | |
else res.json({ | |
message: 'url을 생성했습니다', | |
data: HOST_DOMAIN+uniqueID | |
}) | |
}).promise() | |
}) | |
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "url-shortener", | |
"version": "1.0.0", | |
"main": "index.js", | |
"license": "MIT", | |
"scripts": { | |
"start": "sls offline start", | |
"deploy": "sls deploy --stage dev", | |
"remve": "sls remove" | |
}, | |
"dependencies": { | |
"aws-sdk": "^2.610.0", | |
"express": "^4.17.1", | |
"serverless-http": "^2.3.1", | |
}, | |
"devDependencies": { | |
"serverless-offline": "^5.12.1", | |
"shortid": "^2.2.15" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const express = require('express') | |
const AWS = require('aws-sdk') | |
const shortID = require('shortid') | |
const dynamoDB = new AWS.DynamoDB.DocumentClient() | |
const router = express.Router() | |
const checkURL = (path) => { | |
if(!shortID.isValid(path)) return { message: '올바른 형태의 축약된 URL을 입력해주세요', data: { path }} | |
} | |
router.get('/*', async(req,res) => { | |
const pathID = req.url.replace('/','') | |
const pathError = checkURL(pathID) | |
if(pathError) res.status(400).send(pathError) | |
const findURLData = { | |
AttributesToGet: [ 'raw_url' ], | |
TableName: 'URLTable', | |
Key: { | |
short_url: pathID | |
} | |
} | |
dynamoDB.get(findURLData, (err, data) => { | |
if(err) res.status(500).send({ message: '데이터베이스에 존재하지 않는 축약 URL 입니다', error: err }) | |
const rawURL = data.Item['raw_url'] | |
res.redirect(rawURL) | |
}) | |
}) | |
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Welcome to Serverless! | |
# | |
# This file is the main config file for your service. | |
# It's very minimal at this point and uses default values. | |
# You can always add more config options for more control. | |
# We've included some commented out config examples here. | |
# Just uncomment any of them to get that config option. | |
# | |
# For full config options, check the docs: | |
# docs.serverless.com | |
# | |
# Happy Coding! | |
service: url-shortener | |
plugins: | |
- serverless-offline | |
provider: | |
name: aws | |
runtime: nodejs12.x | |
environment: | |
DYNAMODB_TABLE: URLTable | |
stage: dev | |
region: ap-northeast-2 | |
iamRoleStatements: | |
- Effect: Allow | |
Action: | |
- dynamodb:Query | |
- dynamodb:Scan | |
- dynamodb:GetItem | |
- dynamodb:PutItem | |
- dynamodb:UpdateItem | |
- dynamodb:DeleteItem | |
Resource: arn:aws:dynamodb:${self:provider.region}:*:table/${self:provider.environment.DYNAMODB_TABLE} | |
functions: | |
api: | |
handler: src/api.handler | |
events: | |
- http: | |
path: /create | |
method: POST | |
web: | |
handler: src/web.handler | |
events: | |
- http: | |
path: / | |
method: GET | |
- http: | |
path: /{any+} | |
method: GET | |
resources: | |
Resources: | |
URLMappingTable: | |
Type: AWS::DynamoDB::Table | |
Properties: | |
TableName: URLTable | |
AttributeDefinitions: | |
- AttributeName: short_url | |
AttributeType: S | |
KeySchema: | |
- AttributeName: short_url | |
KeyType: HASH | |
ProvisionedThroughput: | |
ReadCapacityUnits: 1 | |
WriteCapacityUnits: 1 | |
# vpc 설정 추가하기!! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const serverless = require('serverless-http') | |
const redirectRouter = require('./routes/redirectURL') | |
const app = express() | |
app.use(express.json()) | |
app.use('/', redirectRouter) | |
module.exports.handler = serverless(app) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment