每一天:研究室 4:建立網路前端

1. 總覽

在本程式碼實驗室中,您將在 Google App Engine 上建立網頁前端,讓使用者能從網頁應用程式上傳圖片,以及瀏覽上傳的圖片和縮圖。

21741cd63b425aeb.png

這個網頁應用程式會使用名為 Bulma 的 CSS 架構,打造美觀的使用者介面,並使用 Vue.JS JavaScript 前端架構,呼叫您要建構的應用程式 API。

這個應用程式包含三個分頁:

  • 首頁:顯示所有上傳圖片的縮圖,以及描述圖片的標籤清單 (Cloud Vision API 在先前實驗室中偵測到的標籤)。
  • 美術拼貼頁面:顯示由最近上傳的 4 張相片組成的美術拼貼。
  • 上傳頁面:使用者可上傳新相片。

產生的前端如下所示:

6a4d5e5603ba4b73.png

這 3 個頁面是簡單的 HTML 網頁:

  • 首頁 (index.html) 會呼叫 Node App Engine 後端程式碼,透過對 /api/pictures 網址的 AJAX 呼叫,取得縮圖圖片及其標籤的清單。首頁會使用 Vue.js 擷取這項資料。
  • 美術拼貼頁面 (collage.html) 指向 collage.png 圖片,該圖片由 4 張最新相片組成。
  • upload 頁面 (upload.html) 提供簡單的表單,可透過 POST 要求將圖片上傳至 /api/pictures 網址。

課程內容

  • App Engine
  • Cloud Storage
  • Cloud Firestore

2. 設定和需求

自修實驗室環境設定

  1. 登入 Google Cloud 控制台,建立新專案或重複使用現有專案。如果您沒有 Gmail 或 Google Workspace 帳戶,請建立帳戶。

b35bf95b8bf3d5d8.pnga99b7ace416376c4.pngbd84a6d3004737c5.png

  • 「專案名稱」是專案參與者的顯示名稱。這是一串 Google API 不會使用的字元,您隨時可以更新。
  • 專案 ID 在所有 Google Cloud 專案中不得重複,且一經設定即無法變更。Cloud 控制台會自動產生一組不重複的字串,通常您不需要特別留意。在大多數程式碼實驗室中,您必須參照專案 ID (通常會標示為 PROJECT_ID),因此如果您不喜歡這組字串,可以產生另一組隨機字串,也可以嘗試使用自己的字串,看看是否可用。專案建立後,這個值就會「凍結」。
  • 第三個值是「專案編號」,部分 API 會使用這個值。如要進一步瞭解這三個值,請參閱說明文件。
  1. 接著,您必須在 Cloud 控制台中啟用計費功能,才能使用 Cloud 資源/API。完成這個程式碼實驗室的費用應該不高,甚至可能完全免費。如要關閉資源,避免在完成本教學課程後產生費用,請按照程式碼實驗室結尾的「清除」操作說明操作。Google Cloud 新使用者可參加價值 $300 美元的免費試用計畫。

啟動 Cloud Shell

雖然您可以透過筆電遠端操作 Google Cloud,但在本程式碼實驗室中,您將使用 Google Cloud Shell,這是在雲端執行的指令列環境。

在 Google Cloud 控制台中,點選右上角工具列的「Cloud Shell」圖示:

55efc1aaa7a4d3ad.png

佈建並連線至環境的作業需要一些時間才能完成。完成後,您應該會看到類似下方的內容:

7ffe5cbb04455448.png

這個虛擬機器已預先安裝所有必要的開發人員工具,並提供永久的 5 GB 主目錄,而且可在 Google Cloud 運作,大幅提升網路效能並強化驗證功能。本實驗室的所有工作都只要透過瀏覽器即可完成。

3. 啟用 API

App Engine 須搭配 Compute Engine API。請確認是否已啟用:

gcloud services enable compute.googleapis.com

您應該會看到作業順利完成:

Operation "operations/acf.5c5ef4f6-f734-455d-b2f0-ee70b5a17322" finished successfully.

4. 複製程式碼

如果您尚未簽出程式碼,請先簽出:

git clone https://github.com/GoogleCloudPlatform/serverless-photosharing-workshop

接著,前往包含前端的目錄:

cd serverless-photosharing-workshop/frontend

前端的檔案版面配置如下:

frontend
 |
 ├── index.js
 ├── package.json
 ├── app.yaml
 |
 ├── public
      |
      ├── index.html
      ├── collage.html
      ├── upload.html
      |
      ├── app.js
      ├── script.js
      ├── style.css

專案根目錄有 3 個檔案:

  • index.js 包含 Node.js 程式碼
  • package.json 定義程式庫依附元件
  • app.yaml 是 Google App Engine 的設定檔

public 資料夾包含靜態資源:

  • index.html 是顯示所有縮圖和標籤的頁面
  • collage.html顯示近期相片的美術拼貼
  • upload.html 包含上傳新相片的表單
  • app.js 使用 Vue.js 將資料填入 index.html 頁面
  • script.js 會處理導覽選單及其在小型螢幕上的「漢堡」圖示
  • style.css 定義一些 CSS 指令

5. 探索程式碼

依附元件

package.json 檔案會定義所需的程式庫依附元件:

{
  "name": "frontend",
  "version": "0.0.1",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "@google-cloud/firestore": "^3.4.1",
    "@google-cloud/storage": "^4.0.0",
    "express": "^4.16.4",
    "dayjs": "^1.8.22",
    "bluebird": "^3.5.0",
    "express-fileupload": "^1.1.6"
  }
}

我們的應用程式依附於:

  • firestore:使用圖片中繼資料存取 Cloud Firestore
  • storage:存取儲存相片的 Google Cloud Storage。
  • express:Node.js 的網頁架構。
  • dayjs:小型程式庫,以人性化的方式顯示日期。
  • bluebird:JavaScript Promise 程式庫。
  • express-fileupload:可輕鬆處理檔案上傳作業的程式庫。

Express 前端

在 index.js 控制器的開頭,您需要先前在 package.json 中定義的所有依附元件:

const express = require('express');
const fileUpload = require('express-fileupload');
const Firestore = require('@google-cloud/firestore');
const Promise = require("bluebird");
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const path = require('path');
const dayjs = require('dayjs');
const relativeTime = require('dayjs/plugin/relativeTime')
dayjs.extend(relativeTime)

接著,系統會建立 Express 應用程式執行個體。

系統會使用兩個 Express 中介軟體:

  • express.static() 呼叫表示靜態資源將在 public 子目錄中提供。
  • fileUpload() 則會設定檔案上傳功能,將檔案大小限制為 10 MB,並將檔案上傳至 /tmp 目錄中記憶體內檔案系統的本機位置。
const app = express();
app.use(express.static('public'));
app.use(fileUpload({
    limits: { fileSize: 10 * 1024 * 1024 },
    useTempFiles : true,
    tempFileDir : '/tmp/'
}))

靜態資源包括首頁、拼貼頁面和上傳頁面的 HTML 檔案。這些網頁會呼叫 API 後端。這個 API 會有下列端點:

  • POST /api/pictures 透過 upload.html 中的表單,系統會透過 POST 要求上傳圖片
  • GET /api/pictures:這個端點會傳回 JSON 文件,其中包含圖片清單和標籤
  • GET /api/pictures/:name 這個網址會重新導向至完整大小圖片的 Cloud Storage 位置
  • GET /api/thumbnails/:name 這個網址會重新導向至縮圖的雲端儲存空間位置
  • GET /api/collage最後一個網址會重新導向至生成的拼貼圖片在 Cloud Storage 的位置

上傳相片

在探索圖片上傳 Node.js 程式碼之前,請先快速查看 public/upload.html。

... 
<form method="POST" action="/api/pictures" enctype="multipart/form-data">
    ... 
    <input type="file" name="pictures">
    <button>Submit</button>
    ... 
</form>
... 

表單元素指向 /api/pictures 端點,採用 HTTP POST 方法和多部分格式。index.js 現在必須回應這個端點和方法,並擷取檔案:

app.post('/api/pictures', async (req, res) => {
    if (!req.files || Object.keys(req.files).length === 0) {
        console.log("No file uploaded");
        return res.status(400).send('No file was uploaded.');
    }
    console.log(`Receiving files ${JSON.stringify(req.files.pictures)}`);

    const pics = Array.isArray(req.files.pictures) ? req.files.pictures : [req.files.pictures];

    pics.forEach(async (pic) => {
        console.log('Storing file', pic.name);
        const newPicture = path.resolve('/tmp', pic.name);
        await pic.mv(newPicture);

        const pictureBucket = storage.bucket(process.env.BUCKET_PICTURES);
        await pictureBucket.upload(newPicture, { resumable: false });
    });


    res.redirect('/');
});

首先,您要確認確實有檔案上傳。接著,您透過檔案上傳 Node 模組的 mv 方法,將檔案下載到本機。檔案已儲存在本機檔案系統,現在請將圖片上傳至 Cloud Storage bucket。最後,將使用者重新導向至應用程式的主畫面。

列出相片

現在就展示你的精美相片吧!

在 /api/pictures 處理常式中,您會查看 Firestore 資料庫的 pictures 集合,擷取所有圖片 (已產生縮圖),並依建立日期遞減排序。

您將每張圖片推送至 JavaScript 陣列,其中包含圖片名稱、描述圖片的標籤 (來自 Cloud Vision API)、主色,以及易於理解的建立日期 (使用 dayjs,我們使用相對時間偏移,例如「3 天後」)。

app.get('/api/pictures', async (req, res) => {
    console.log('Retrieving list of pictures');

    const thumbnails = [];
    const pictureStore = new Firestore().collection('pictures');
    const snapshot = await pictureStore
        .where('thumbnail', '==', true)
        .orderBy('created', 'desc').get();

    if (snapshot.empty) {
        console.log('No pictures found');
    } else {
        snapshot.forEach(doc => {
            const pic = doc.data();
            thumbnails.push({
                name: doc.id,
                labels: pic.labels,
                color: pic.color,
                created: dayjs(pic.created.toDate()).fromNow()
            });
        });
    }
    console.table(thumbnails);
    res.send(thumbnails);
});

這個控制器會傳回下列形狀的結果:

[
   {
      "name": "IMG_20180423_163745.jpg",
      "labels": [
         "Dish",
         "Food",
         "Cuisine",
         "Ingredient",
         "Orange chicken",
         "Produce",
         "Meat",
         "Staple food"
      ],
      "color": "#e78012",
      "created": "a day ago"
   },
   ...
]

這個資料結構會由 index.html 頁面中的一小段 Vue.js 程式碼片段使用。以下是該網頁標記的簡化版本:

<div id="app">
        <div class="container" id="app">
                <div id="picture-grid">
                        <div class="card" v-for="pic in pictures">
                                <div class="card-content">
                                        <div class="content">
                                                <div class="image-border" :style="{ 'border-color': pic.color }">
                                                        <a :href="'/api/pictures/' + pic.name">
                                                                <img :src="'/api/thumbnails/' + pic.name">
                                                        </a>
                                                </div>
                                                <a class="panel-block" v-for="label in pic.labels" :href="'/?q=' + label">
                                                        <span class="panel-icon">
                                                                <i class="fas fa-bookmark"></i> &nbsp;
                                                        </span>
                                                        {{ label }}
                                                </a>
                                        </div>
                                </div>
                        </div>
            </div>
        </div>
</div>

div 的 ID 會向 Vue.js 指出這是要動態轉譯的標記部分。v-for 指令會完成疊代。

Cloud Vision API 會找出圖片中的主色,並為圖片加上相應顏色的邊框。我們也會在連結和圖片來源中,指向縮圖和全寬圖片。

最後,我們會列出描述相片的標籤。

以下是 Vue.js 程式碼片段的 JavaScript 程式碼 (位於 index.html 頁面底部匯入的 public/app.js 檔案中):

var app = new Vue({
  el: '#app',
  data() {
    return { pictures: [] }
  },
  mounted() {
    axios
      .get('/api/pictures')
      .then(response => { this.pictures = response.data })
  }
})

Vue 程式碼會使用 Axios 程式庫,向 /api/pictures 端點發出 AJAX 呼叫。接著,傳回的資料會繫結至您先前看到的標記中的檢視區塊程式碼。

查看相片

使用者可以透過 index.html 查看圖片縮圖,點選縮圖即可查看完整大小的圖片,並透過 collage.html 查看 collage.png 圖片。

在這些網頁的 HTML 標記中,圖片 src 和連結 href 會指向這 3 個端點,並重新導向至圖片、縮圖和美術拼貼的 Cloud Storage 位置。不必在 HTML 標記中硬式編碼路徑。

app.get('/api/pictures/:name', async (req, res) => {
    res.redirect(`https://storage.cloud.google.com/${process.env.BUCKET_PICTURES}/${req.params.name}`);
});

app.get('/api/thumbnails/:name', async (req, res) => {
    res.redirect(`https://storage.cloud.google.com/${process.env.BUCKET_THUMBNAILS}/${req.params.name}`);
});

app.get('/api/collage', async (req, res) => {
    res.redirect(`https://storage.cloud.google.com/${process.env.BUCKET_THUMBNAILS}/collage.png`);
});

執行 Node 應用程式

定義所有端點後,即可啟動 Node.js 應用程式。Express 應用程式預設會監聽通訊埠 8080,並準備好處理傳入的要求。

const PORT = process.env.PORT || 8080;

app.listen(PORT, () => {
    console.log(`Started web frontend service on port ${PORT}`);
    console.log(`- Pictures bucket = ${process.env.BUCKET_PICTURES}`);
    console.log(`- Thumbnails bucket = ${process.env.BUCKET_THUMBNAILS}`);
});

6. 在本機測試

在本機測試程式碼,確認運作正常後再部署至雲端。

您需要匯出兩個環境變數,分別對應兩個 Cloud Storage bucket:

export BUCKET_THUMBNAILS=thumbnails-${GOOGLE_CLOUD_PROJECT}
export BUCKET_PICTURES=uploaded-pictures-${GOOGLE_CLOUD_PROJECT}

在 frontend 資料夾中,安裝 npm 依附元件並啟動伺服器:

npm install; npm start

如果一切順利,伺服器應會在通訊埠 8080 啟動:

Started web frontend service on port 8080
- Pictures bucket = uploaded-pictures-${GOOGLE_CLOUD_PROJECT}
- Thumbnails bucket = thumbnails-${GOOGLE_CLOUD_PROJECT}

這些記錄會顯示值區的實際名稱,有助於偵錯。

在 Cloud Shell 中,您可以使用網頁預先發布版功能,瀏覽在本機執行的應用程式:

82fa3266d48c0d0a.png

按 CTRL-C 即可退出。

7. 部署至 App Engine

應用程式已可部署。

設定 App Engine

查看 App Engine 的 app.yaml 設定檔:

runtime: nodejs16
env_variables:
  BUCKET_PICTURES: uploaded-pictures-GOOGLE_CLOUD_PROJECT
  BUCKET_THUMBNAILS: thumbnails-GOOGLE_CLOUD_PROJECT

第一行宣告執行階段是以 Node.js 10 為基礎。定義兩個環境變數,分別指向原始圖片和縮圖的兩個 bucket。

如要將 GOOGLE_CLOUD_PROJECT 替換為實際專案 ID,請執行下列指令:

sed -i -e "s/GOOGLE_CLOUD_PROJECT/${GOOGLE_CLOUD_PROJECT}/" app.yaml

部署

設定偏好的 App Engine 區域,請務必使用與先前實驗室相同的區域:

gcloud config set compute/region europe-west1

並部署:

gcloud app deploy

一兩分鐘後,系統會通知您應用程式正在提供流量:

Beginning deployment of service [default]...
╔════════════════════════════════════════════════════════════╗
╠═ Uploading 8 files to Google Cloud Storage                ═╣
╚════════════════════════════════════════════════════════════╝
File upload done.
Updating service [default]...done.
Setting traffic split for service [default]...done.
Deployed service [default] to [https://GOOGLE_CLOUD_PROJECT.appspot.com]
You can stream logs from the command line by running:
  $ gcloud app logs tail -s default
To view your application in the web browser run:
  $ gcloud app browse

您也可以前往 Cloud 控制台的 App Engine 專區,查看應用程式是否已部署,並探索 App Engine 的功能,例如版本管理和流量拆分:

db0e196b00fceab1.png

8. 測試應用程式

如要測試,請前往應用程式的預設 App Engine 網址 (https://<YOUR_PROJECT_ID>.appspot.com/),您應該會看到前端 UI 正常運作!

6a4d5e5603ba4b73.png

9. 清除 (選用)

如果不想保留應用程式,可以刪除整個專案來清理資源,節省成本並當個優質的雲端使用者:

gcloud projects delete ${GOOGLE_CLOUD_PROJECT} 

10. 恭喜!

恭喜!這個 Node.js 網頁應用程式託管於 App Engine,可將所有服務繫結在一起,讓使用者上傳及查看圖片。

涵蓋內容

  • App Engine
  • Cloud Storage
  • Cloud Firestore

後續步驟