1. 下載demo
cd vapor
# 將在當(dāng)前目錄下創(chuàng)建一個(gè)hello工程
vapor new hello -n
# 切換到hello目錄
cd hello
# 將下載關(guān)聯(lián)庫(kù)叹洲,并編譯,需要較長(zhǎng)時(shí)間
vapor run
# 經(jīng)較長(zhǎng)時(shí)間下載工禾,編譯運(yùn)行运提,終端顯示如下
Server starting on http://127.0.0.1:8080
2.同步云端
# 將hello目錄上傳到云端已創(chuàng)建的swift目錄下
scp -r xxx/vapor/hello ubuntu@xxx.xxx.x.xxx:~/swift/hello
# ssh鏈接云端蝗柔,輸入密碼
ssh ubuntu@xxx.xxx.x.xxx
# 切換到工作目錄
cd swift/hello
# 運(yùn)行目錄
vapor run
# 終端迅速編譯后,顯示如下
Server starting on http://127.0.0.1:8080
3.開(kāi)放公網(wǎng)ip
由于127.0.0.1為本機(jī)地址民泵,通過(guò)云主機(jī)的公網(wǎng)ip癣丧,不能訪問(wèn)到此地址。所以栈妆,我們需要將httpserver的hostname設(shè)置為0.0.0.0胁编。
# 修改配置文件 hello/Sources/App/configure.swift代碼如下
import Vapor
// configures your application
public func configure(_ app: Application) throws {
// uncomment to serve files from /Public folder
// app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))
// register routes
app.http.server.configuration.hostname = "0.0.0.0"
try routes(app)
}
# 更新文件到云端后,關(guān)停云端重新運(yùn)行
vapor run
# 編譯運(yùn)行后鳞尔,顯示如下
Server starting on http://0.0.0.0:8080
4.測(cè)試公網(wǎng)
打開(kāi)瀏覽器或postman嬉橙,輸入http://xxx.xxx.xxx.xxx:8080,可見(jiàn)內(nèi)容如下:
It works!
繼續(xù)在瀏覽器或postman中寥假,輸入http://xxx.xxx.xxx.xxx:8080/hello市框,可見(jiàn)內(nèi)容如下:
Hello, world!