Skip to content

Latest commit

ย 

History

History
288 lines (194 loc) ยท 9.64 KB

7-api-gateway.md

File metadata and controls

288 lines (194 loc) ยท 9.64 KB

API Gateway

AWS ์„œ๋ฒ„๋ฆฌ์Šค ์•„ํ‚คํ…์ฒ˜ 7์žฅ (p189)

์‹ค์Šต : API Gateway์— get-video-list API๋ฅผ ์ถ”๊ฐ€ (p193)

1. get-video-list API ์ƒ์„ฑ

  • API Gateway์—์„œ API ์ƒ์„ฑ

    img

    img

    img

2. ๋ฆฌ์†Œ์Šค ์ƒ์„ฑ

  • ๋ฆฌ์†Œ์Šค ์ƒ์„ฑ

    img

    img

3. ๋งค์„œ๋“œ ์ƒ์„ฑ

  • ๋งค์„œ๋“œ ์ƒ์„ฑ

    img

    img

4. ๋งค์„œ๋“œ ํ…Œ์ŠคํŠธ

img

image-20210317163757398

5. CORS ํ™œ์„ฑํ™” ๋ฐ API ๋ฐฐํฌ

img

img

img




์›น ์‚ฌ์ดํŠธ์— ๋น„๋””์˜ค ๋ชฉ๋ก์„ ๊ฐ€์ ธ์™€์„œ ์žฌ์ƒํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ (p209)

1. config.js ํŒŒ์ผ์— get-file-list API ํ˜ธ์ถœ URL์„ ์ถ”๊ฐ€

  • js/config.js

    image-20210317172056009

    var configConstants = {
        auth0: {
            domain: '1yangsh.us.auth0.com',
            clientId: 'Bkia8NNISRVmEw6M7lANfYmvoutaOVdD'
        },
        // user-profile API ํ˜ธ์ถœ URL
        apiBaseUrl: 'https://tzecikd5j7.execute-api.us-east-1.amazonaws.com/dev',
    
        // get-file-list API ํ˜ธ์ถœ URL
        getFileListApiUrl: 'https://x9p0er0szb.execute-api.us-east-1.amazonaws.com/dev' // ์ถ”๊ฐ€
    };

2. video-controller.js ํŒŒ์ผ์„ ์ถ”๊ฐ€

img

  • c:\serverless\24-hour-video\js\video-controller.js

    // P209
    var videoController = {
        data: {
            config: null
        },
        uiElements: {
            videoCardTemplate: null, 
            videoList: null
        }, 
        init: function(config) {
            // index.html ๋ฌธ์„œ์— id ์†์„ฑ์ด video-template, video-list์ธ ์š”์†Œ๋ฅผ ์ฐธ์กฐ
            this.uiElements.videoCardTemplate = $('#video-template');
            this.uiElements.videoList = $('#video-list');
     
            // config.js ํŒŒ์ผ์— ์žˆ๋Š” ๋‚ด์šฉ์„ ์ฐธ์กฐ
            this.data.config = config;
     
            this.getVideoList();
        }, 
        // get-video-list API๋ฅผ ํ˜ธ์ถœ
        getVideoList: function() {
            var that = this;
     
            // get-video-list API ํ˜ธ์ถœ URL + ๋ฆฌ์†Œ์Šค ์ด๋ฆ„
            // videos ๋ฆฌ์†Œ์Šค๋ฅผ GET ๋ฐฉ์‹์œผ๋กœ ํ˜ธ์ถœ --> get-video-list ๋žŒ๋‹ค ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ ๋ฐ›์Œ
            var url = this.data.config.getFileListApiUrl + '/videos';
            $.get(url, function(data, status) {
                that.updateVideoFrontPage(data);
            });
        }, 
        // get-video-list ๋žŒ๋‹ค ํ•จ์ˆ˜์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๋ฅผ ๋ชฉ๋ก์œผ๋กœ ํ™”๋ฉด์— ์ถœ๋ ฅ
        updateVideoFrontPage: function(data) {
            console.log(data);
            // TODO
        }
    };

3. main.js ํŒŒ์ผ์— videoController ๊ฐ์ฒด์˜ ์ดˆ๊ธฐํ™” ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ์ถ”๊ฐ€

  • c:\serverless\24-hour-video\js\main.js

    // ์ฆ‰์‹œ ์‹คํ–‰ ํ•จ์ˆ˜
    (function() {
        console.log("### 1");
        // ํ•ด๋‹น ์›น ํŽ˜์ด์ง€ ๋ฌธ์„œ๊ฐ€ ๋กœ๋”ฉ๋˜๋ฉด ์„ค์ • ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์™€์„œ ์„ค์ •
        $(document).ready(function() {
            console.log("### 2");
            // user-controller.js์— ์„ ์–ธ๋˜์–ด ์žˆ๋Š” userController ๊ฐ์ฒด์˜ init ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœ
            // coonfig.js์— ์„ ์–ธ๋˜์–ด ์žˆ๋Š” configConstants ๊ฐ์ฒด๋ฅผ ์ธ์ž๋กœ ์ „๋‹ฌ
            userController.init(configConstants);
            videoController.init(configConstants);  /* ์ถ”๊ฐ€ */
        });
    })();

4. index.html ํŒŒ์ผ์— ๋™์˜์ƒ ๋ชฉ๋ก๊ณผ ์ถœ๋ ฅ ๋ถ€๋ถ„์„ ์ถ”๊ฐ€

  • c:\serverless\24-hour-video\index.html

       <!-- (์ƒ๋žต) -->
        <!-- Main jumbotron for a primary marketing message or call to action -->
        <div class="jumbotron">
          <!-- ๋Œ€๋žต 66๋ฒˆ ๋ผ์ธ
          <div class="container">
            <h1>Hello, world!</h1>
            <p>This is a template for a simple marketing or informational website. It includes a large callout called a jumbotron and three supporting pieces of content. Use it as a starting point to create something more unique.</p>
            <p><a class="btn btn-primary btn-lg" href="#" role="button">Learn more &raquo;</a></p>
          </div>
          -->
          <!-- ์ถ”๊ฐ€ ์‹œ์ž‘: P209 ์ฝ”๋“œ ์ฐธ์กฐ -->
          <div class="container" id="video-list-container">
            <div id="video-template" class="col-md-6 col">
              <div class="video-card">
                <video width="100%" height="100%" controls>
                  <source type="video/mp4">
                  ์ง€์›ํ•˜์ง€ ์•Š๋Š” ํƒ€์ž…
                </video>
              </div>
            </div>
            <div calss="row">
              <ul id="video-list">
     
              </ul>
            </div>
          </div>
          <!-- ์ถ”๊ฐ€ ๋ -->
        </div>
        <!-- (์ƒ๋žต) -->
            <script src="https://cdn.auth0.com/js/lock/11.27/lock.min.js"></script>
            <script src="js/config.js"></script>
            <script src="js/user-controller.js"></script>
            <script src="js/video-controller.js"></script>  <!-- ์ถ”๊ฐ€ -->
            <script src="js/main.js"></script>
            <!-- (์ƒ๋žต) -->

4. ์›น ํŽ˜์ด์ง€ ํ…Œ์ŠคํŠธ

  • ์ •์ƒ์ ์ธ ๋กœ๊ทธ๋ฅผ console์—์„œ ํ™•์ธ

    image-20210317173331851

    image-20210317173309642

6. S3 ๋ฒ„ํ‚ท ๋ชฉ๋ก์„ ํ™”๋ฉด์— ์ถœ๋ ฅ

  • c:\serverless\24-hour-video\js\video-controller.js

     updateVideoFrontPage: function(data) {
            console.log(data);
            // <ul id="video-list">
            // <li url="https://serverless...jini.s3.amazonaws.com/my+video/my+video-1080p.mp4">๋™์˜์ƒ ํŒŒ์ผ๋ช…</li>
            //          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~  ~~~~~~~~~~~~
            //          baseUrl                                    urls.Key                     urls.Key์—์„œ ํŒŒ์ผ๋ช…๋งŒ ์ถ”์ถœ
            var baseUrl = data.baseUrl;
            var urls = data.urls;
            /*
            for (var i = 0; i < urls.length; i ++) {
                var url = urls[i];
                var key = url.Key;
                var filename = key.split('/')[1];
                var litag = '<li url="' + baseUrl + '/' + key + '">' + filename + '</li>';
                $('#video-list').append(litag);
            }
            */
     
            urls.forEach(url => {
                var key = url.Key;
                var filename = key.split('/')[1];
                var litag = `<li url="${baseUrl}/${key}">${filename}</li>`; 
                $('#video-list').append(litag);
            });
        }

    img

7. ๋ชฉ๋ก์„ ํด๋ฆญํ–ˆ์„ ๋•Œ ๋น„๋””์˜ค๊ฐ€ ์žฌ์ƒ

  • controller.js

    // ์ถ”๊ฐ€
    // ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜(์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ)๋ฅผ ์ •์˜
        // https://developer.mozilla.org/ko/docs/Web/HTML/Element/Video
        wireEvents: function() {
            // id ์†์„ฑ(attibute) ๊ฐ’์ด video-list์ธ ์š”์†Œ(element, tag) ์•„๋ž˜์—์„œ 
            // li ์š”์†Œ์— click ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์ˆ˜ํ–‰ํ•  ๋™์ž‘์„ ์ •์˜
            $('#video-list').on('click', 'li', function() {
                // ํด๋ฆญํ•œ li ํƒœ๊ทธ(์š”์†Œ)์˜ url ์†์„ฑ์˜ ๊ฐ’์„ ๊ฐ€์ ธ์™€์„œ url ๋ณ€์ˆ˜์— ํ• ๋‹น
                var url = $(this).attr('url');
                /*
                <video width="100%" height="100%" controls>
                  <!-- video ํƒœ๊ทธ์— ์‚ฌ์šฉ๋  ๋™์˜์ƒ์˜ ์†Œ์Šค -->
                  <!-- src ์†์„ฑ์— ๋™์˜์ƒ์˜ ์ฃผ์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ์žฌ์ƒ์ด ๊ฐ€๋Šฅ -->
                  <source type="video/mp4">
                  ์ง€์›ํ•˜์ง€ ์•Š๋Š” ํƒ€์ž…
                </video>
                */
                // source ํƒœ๊ทธ(์š”์†Œ)์˜ src ์†์„ฑ์˜ ๊ฐ’์œผ๋กœ url ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ์„ค์ •
                $('source').attr('src', url);
                // video ํƒœ๊ทธ(์š”์†Œ)์— ์„ค์ •๋œ ๋™์˜์ƒ ํŒŒ์ผ์„ ์ฝ์–ด๋“ค๋ฆผ
                $('video').load();
            });
        }