首先介紹下在本文出現的幾個比較重要的概念:
函數計算(Function Compute): 函數計算是一個事件驅動的服務,通過函數計算,使用者無需管理伺服器等運作情況,隻需編寫代碼并上傳。函數計算準備計算資源,并以彈性伸縮的方式運作使用者代碼,而使用者隻需根據實際代碼運作所消耗的資源進行付費。函數計算更多資訊 參考。
Fun: Fun 是一個用于支援 Serverless 應用部署的工具,能幫助您便捷地管理函數計算、API 網關、日志服務等資源。它通過一個資源配置檔案(template.yml),協助您進行開發、建構、部署操作。Fun 的更多文檔
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, --help output 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 | | 要作為檔案夾生成的項目名稱(可選) |
--no-input | | 禁用提示并接受預設值來定義的模闆配置(可選) |
-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 工具可以快速體驗和開發函數計算的應用,避免重複機械勞動,你也可以将自己覺得有價值的函數計算應用制作成模闆項目,友善自己同時也可以友善别人。