開發函式計算的正確姿勢 —— 使用 Fun Init 初始化專案
首先介紹下在本文出現的幾個比較重要的概念:
__函式計算(Function Compute)__: 函式計算是一個事件驅動的服務,通過函式計算,使用者無需管理伺服器等執行情況,只需編寫程式碼並上傳。函式計算準備計算資源,並以彈性伸縮的方式執行使用者程式碼,而使用者只需根據實際程式碼執行所消耗的資源進行付費。函式計算更多資訊參考。
__Fun__: Fun 是一個用於支援 Serverless 應用部署的工具,能幫助您便捷地管理函式計算、API 閘道器、日誌服務等資源。它通過一個資源配置檔案(template.yml),協助您進行開發、構建、部署操作。Fun 的更多文件 ofollow,noindex" target="_blank">參考 。
2.0 版本的 Fun,在部署這一塊做了很多努力,並提供了比較完善的功能,能夠做到將雲資源方便、平滑地部署到雲端。但該版本,在本地開發上的體驗,還有較多的工作要做。於是,我們決定推出 Fun Init 彌補這一處短板。
__Fun Init__: Fun Init 作為 Fun 的一個子命令存在,只要 Fun 的版本大於等於 2.7.0,即可以直接通過 fun init
命令使用。Fun Init 工具可以根據指定的模板快速的建立函式計算應用,快速體驗和開發函式計算相關業務。官方會提供常用的模板,使用者也可以自定自己的模板。
快速體驗
hellworld-nodejs8 模板目錄結構
helloworld-nodejs8① 模板專案根目錄 ├── metadata.json② 模板專案配置檔案 └── {{ projectName }}③ 模板根目錄 ├── index.js └── template.yml
執行命令:fun init -n xxx helloworld-nodejs8
$ fun init -n xxx helloworld-nodejs8 Start rendering template... + /Users/kevin/xxx + /Users/kevin/xxx/index.js + /Users/kevin/xxx/template.yml finish rendering template.
初始化生成的專案目錄結構:
xxx ├── index.js └── template.yml
Fun Init 命令格式
$ fun init --help Usage: init [options] [location] Initializes a new fun project Options: -o, --output-dir [outputDir]where to output the initialized app into (default: .) -n, --name [name]name of your project to be generated as a folder (default: fun-app) --no-input [noInput]disable prompting and accept default values defined template config -V, --var [vars]template variable -h, --helpoutput usage information Examples: $ fun init $ fun init helloworld-nodejs8 $ fun init foo/bar $ fun init gh:foo/bar $ fun init gl:foo/bar $ fun init bb:foo/bar $ fun init github:foo/bar $ fun init gitlab:foo/bar $ fun init bitbucket:foo/bar $ fun init git+ssh://[email protected]/foo/bar.git $ fun init hg+ssh://[email protected]/bar/foo $ fun init [email protected]:foo/bar.git $ fun init https://github.com/foo/bar.git $ fun init /path/foo/bar $ fun init -n fun-app -V foo=bar /path/foo/bar
選項說明
選項 | 預設值 | 描述 |
---|---|---|
-o, --output-dir |
.
|
初始化的應用程式輸出目錄(可選) |
-n, --name |
fun-app
|
要作為資料夾生成的專案名稱(可選) |
--no-input |
false
|
禁用提示並接受預設值來定義的模板配置(可選) |
-V, --var | 空 | 模板變數(可選) |
-h, --help | 無 | 列印使用說明(可選) |
示例:
-
-o,--output-dir
選項輸出初始化應用程式的輸出目錄。預設是
fun init
命令執行的當前路徑。
$ fun init -o /path/foo/bar
-
-n,--name
選項要作為資料夾生成的專案名稱。預設值是
fun-app
。
$ fun init -n xxx
-
-V,--var
選項模板變數。模板可能會有很多模板變數,模板變數一般都會設定預設值,如果某些模板變數希望使用者填寫自己的值,在初始化模板的過程中,會提示使用者輸入自定義的值,使用者可以輸入自定義的值,也可以直接回車使用預設值,還可以直接通過
-V,--var
選項設定模板變數值。通過該選項設定的模板變數,就不會再提示使用者輸入了。
選項格式要求:以等號分隔的鍵值對,鍵只能包含字母、數字和 _;值可以是任意字元。
$ fun init -V foo=bar -V bar=baz
-
--no-input
選項禁用提示並接受預設值來定義的模板配置。假如不想提示輸入模板變數,可以通過該選項,跳過提示輸入,直接使用模板變數的預設值。
$ fun init --no-input
模板位置(Location)
支援如下方式指定模板:
-
官方模板。包含離線和線上模板:
- 離線模板。內嵌在 fun 工具中
- 線上模板。通過模板名稱來指定模板位置,fun 內部會將其轉換為 github 地址
- Git / Mercurial 倉庫模板。支援縮寫方式,具體請看下文
- 本地檔案系統路徑模板
如果您需要自定義自己的模板,只需要將您的模板提交到 github 上,然後在執行fun init
指定您自己的 github地址。具體請參考:Fun Init 自定義模板
示例:
- 選擇官方模板
輸入fun init
命令,則提示使用者選擇模板,支援模糊查詢。在不清楚自己需要什麼樣的模板時,簡單的使用不帶任何引數的fun init
會提示使用者官方模板有哪些,使用者更具提示選擇自己的需要模板。
$ fun init ? Select a tempalte to init (Use arrow keys or type to search) helloworld-nodejs8 helloworld-nodejs6 helloworld-python3 helloworld-python2.7 helloworld-java8 helloworld-php7.2
- 指定官方模板
helloworld-nodejs8 是內嵌在 fun 工具的官方模板,所以支援離線,該模板最終生成一個執行時為 nodejs8 的 helloworld 函式計算應用。對於一些常用的模板,我們會把它放到官方模板列表中,使用者只需要指定簡短模板名稱即可,不需要指定完整的模板倉庫地址。
$ fun init helloworld-nodejs8
- 指定 Git / Mercurial 倉庫模板
支援 Git / Mercurial 倉庫模板,支援多種字首的縮寫形式,對於 github,甚至可以進一步縮寫為user/repo
。
$ fun init foo/bar $ fun init gh:foo/bar $ fun init gl:foo/bar $ fun init bb:foo/bar $ fun init github:foo/bar $ fun init gitlab:foo/bar $ fun init bitbucket:foo/bar $ fun init git+ssh://[email protected]/foo/bar.git $ fun init hg+ssh://[email protected]/bar/foo $ fun init [email protected]:foo/bar.git $ fun init https://github.com/foo/bar.git
說明:需要提前安裝好相應的版本控制工具。
- 指定本地檔案系統路徑模板
我們可以將線上模板克隆放到本地檔案系統,通過指定模板路徑,實現離線初始化。
$ fun init /path/foo/bar
小結
通過 Fun Init 工具可以快速體驗和開發函式計算的應用,避免重複機械勞動,你也可以將自己覺得有價值的函式計算應用製作成模板專案,方便自己同時也可以方便別人。