目錄
前言
一狠半、安裝 Swift
二取胎、安裝 Swift Perfect
前言
我的 Ubuntu 是 16.04 64 位的系統(tǒng)。
一、安裝 Swift
Swift 官網(wǎng):https://swift.org/download/#releases
右鍵復(fù)制鏈接地址
使用 wget 下載 Swift 安裝包
[root@:~]# wget https://swift.org/builds/swift-4.2-release/ubuntu1604/swift-4.2-RELEASE/swift-4.2-RELEASE-ubuntu16.04.tar.gz
--2018-09-22 16:16:46-- https://swift.org/builds/swift-4.2-release/ubuntu1604/swift-4.2-RELEASE/swift-4.2-RELEASE-ubuntu16.04.tar.gz
Resolving swift.org (swift.org)... 169.47.73.10
Connecting to swift.org (swift.org)|169.47.73.10|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 223291596 (213M) [application/x-gzip]
Saving to: ‘swift-4.2-RELEASE-ubuntu16.04.tar.gz’
swift-4.2-RELEASE-ubuntu16. 100%[=========================================>] 212.95M 8.08MB/s in 26s
2018-09-22 16:17:14 (8.11 MB/s) - ‘swift-4.2-RELEASE-ubuntu16.04.tar.gz’ saved [223291596/223291596]
修改 Swift 包的名稱姑食,放置在 home 目錄下
[root@~]# ls
swift-4.2-RELEASE-ubuntu16.04.tar.gz
[root@~]# mv swift-4.2-RELEASE-ubuntu16.04.tar.gz swift-4.2.tar.gz
[root@~]# ls
swift-4.2.tar.gz
[root@~]# mv swift-4.2.tar.gz /home/
[root@~]# ls /home/
swift-4.2.tar.gz
[root@~]# cd /home/
解壓 Swift 安裝包
[root@/home]# tar -zxvf swift-4.2.tar.gz
[root@/home]# ls
swift-4.2-RELEASE-ubuntu16.04 swift-4.2.tar.gz
[root@/home]# mv swift-4.2-RELEASE-ubuntu16.04 swift-4.2
[root@/home]#ls
swift-4.2 swift-4.2.tar.gz
先更新 apt-get
[root@/home]# sudo apt-get update
...... // 等待更新完成
Get:27 http://mirrors.cloud.aliyuncs.com/ubuntu
xenial-security/universe Translation-en [144 kB]
Fetched 37.9 MB in 6s (5,589 kB/s)
Reading package lists... Done
安裝 clang ,clang 是 Swift 的編譯工具
[root@/home]# sudo apt-get -y install clang
......
Setting up llvm-3.8-runtime (1:3.8-2ubuntu4) ...
Setting up llvm-3.8 (1:3.8-2ubuntu4) ...
Setting up libjsoncpp1:amd64 (1.7.2-1) ...
Setting up libffi-dev:amd64 (3.2.1-4) ...
Setting up llvm-3.8-dev (1:3.8-2ubuntu4) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...
Processing triggers for systemd (229-4ubuntu21.2) ...
Processing triggers for ureadahead (0.100.0-19) ...
swift 可以直接運行了茅坛,查看安裝的版本
[root@/home]# ./swift-4.2/usr/bin/swift --version
Swift version 4.2 (swift-4.2-RELEASE)
Target: x86_64-unknown-linux-gnu
把 swift 添加進 .bashrc 啟動文件中音半,方便直接調(diào)用 swift 進行操作
[root@/home]# vim ~/.bashrc
文件末尾處,添加 export PATH=/home/swift-4.2/usr/bin:"${PATH}"
[root@/home]# cat ~/.bashrc | grep "swift"
export PATH=/home/swift-4.2/usr/bin:"${PATH}"
[root@/home]# source ~/.bashrc
使用 swift
[root@/home]# swift
Welcome to Swift version 4.2 (swift-4.2-RELEASE). Type :help for assistance.
1> print("Hello world !")
hello world !
2> :quit // 退出 swift
二贡蓖、安裝 Swift Perfect
安裝依賴包
[root@/home]# sudo apt-get -y install openssl libssl-dev uuid-dev
......
Setting up openssl (1.0.2g-1ubuntu4.13) ...
Setting up zlib1g-dev:amd64 (1:1.2.8.dfsg-2ubuntu4.1) ...
Setting up libssl-dev:amd64 (1.0.2g-1ubuntu4.13) ...
Setting up libssl-doc (1.0.2g-1ubuntu4.13) ...
Setting up uuid-dev:amd64 (2.27.1-6ubuntu3.6) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...
好像完了......
來點示例玩玩曹鸠,官方 Web 應(yīng)用入門項目
先安裝 git 用于下載 入門項目
[root@/home]# sudo apt-get -y install git
Unpacking git (1:2.7.4-0ubuntu1.4) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up liberror-perl (0.17-1.2) ...
Setting up git-man (1:2.7.4-0ubuntu1.4) ...
Setting up git (1:2.7.4-0ubuntu1.4) ...
[root@/home]# git --version
git version 2.7.4
克隆項目
[root@/home]# mkdir swiftPerfectLearn
[root@/home]# git clone https://github.com/PerfectlySoft/PerfectTemplate.git swiftPerfectLearn/
Cloning into 'swiftPerfectLearn'...
remote: Enumerating objects: 35, done.
remote: Counting objects: 100% (35/35), done.
remote: Compressing objects: 100% (19/19), done.
remote: Total 269 (delta 11), reused 33 (delta 9), pack-reused 234
Receiving objects: 100% (269/269), 60.50 KiB | 0 bytes/s, done.
Resolving deltas: 100% (130/130), done.
Checking connectivity... done.
移動文件位置,防止 home 上文件過于混亂方便操作
[root@/home]# mv PerfectTemplate swiftPerfectLearn/
[root@/home]# ls swiftPerfectLearn/
PerfectTemplate
[root@/home]# ls
swift-4.2 swift-4.2.tar.gz swiftPerfectLearn
[root@/home]# ls swiftPerfectLearn/
PerfectTemplate
[root@/home]# ls swiftPerfectLearn/PerfectTemplate/
LICENSE LICENSE.zh_CN Package.swift README.md README.zh_CN.md Sources
[root@/home]# cd swiftPerfectLearn/PerfectTemplate/
[root@/home/swiftPerfectLearn/PerfectTemplate]#
swift 編譯項目
[root@.../PerfectTemplate]# swift build
/home/swift-4.2/usr/bin/swift-build: error while loading shared libraries: libcurl.so.4: cannot open shared object file: No such file or directory
處理 libcurl.so.4 錯誤
[root@.../PerfectTemplate]# sudo apt-get install libcurl3
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
linux-headers-4.4.0-87 linux-headers-4.4.0-87-generic linux-image-4.4.0-87-generic
linux-image-extra-4.4.0-87-generic
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
libcurl3
0 upgraded, 1 newly installed, 0 to remove and 100 not upgraded.
Need to get 186 kB of archives.
After this operation, 564 kB of additional disk space will be used.
Get:1 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/main amd64 libcurl3 amd64 7.47.0-1ubuntu2.9 [186 kB]
Fetched 186 kB in 0s (7,268 kB/s)
Selecting previously unselected package libcurl3:amd64.
(Reading database ... 136968 files and directories currently installed.)
Preparing to unpack .../libcurl3_7.47.0-1ubuntu2.9_amd64.deb ...
Unpacking libcurl3:amd64 (7.47.0-1ubuntu2.9) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...
Setting up libcurl3:amd64 (7.47.0-1ubuntu2.9) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...
重新運行 swift build
[root@.../PerfectTemplate]# swift build
......
Resolving https://github.com/PerfectlySoft/Perfect-Crypto.git at 3.1.2
Cloning https://github.com/PerfectlySoft/Perfect-CZlib-src.git
Resolving https://github.com/PerfectlySoft/Perfect-CZlib-src.git at 0.0.4
warning: PackageDescription API v3 is deprecated and will be removed in the future; used by package(s): LinuxBridge, COpenSSL
Compile PerfectCZlib zutil.c
Compile PerfectCZlib uncompr.c
Compile PerfectCZlib trees.c
Compile PerfectCZlib inftrees.c
......
Compile Swift Module 'PerfectNet' (7 sources)
Compile Swift Module 'PerfectHTTP' (10 sources)
Compile Swift Module 'PerfectHTTPServer' (18 sources)
Compile Swift Module 'PerfectTemplate' (1 sources)
Linking ./.build/x86_64-unknown-linux/debug/PerfectTemplate
warning
warning: PackageDescription API v3 is deprecated and will be removed in the future; used by package(s): LinuxBridge, COpenSSL
這個可以忽略的斥铺,不關(guān)你的事啦彻桃!
查看一下文件的內(nèi)容 main.swift
[root@/home]# cat swiftPerfectLearn/PerfectTemplate/Sources/PerfectTemplate/main.swift | grep -v "^ *//" | grep -v "^$"
import PerfectHTTP
import PerfectHTTPServer
func handler(request: HTTPRequest, response: HTTPResponse) {
// Respond with a simple message.
response.setHeader(.contentType, value: "text/html")
response.appendBody(string: "<html><title>Hello, world!</title><body>Hello, world!</body></html>")
// Ensure that response.completed() is called when your processing is done.
response.completed()
}
var routes = Routes()
routes.add(method: .get, uri: "/", handler: handler)
routes.add(method: .get, uri: "/**",
handler: StaticFileHandler(documentRoot: "./webroot", allowResponseFilters: true).handleRequest)
try HTTPServer.launch(name: "localhost",
port: 8181,
routes: routes,
responseFilters: [
(PerfectHTTPServer.HTTPFilter.contentCompression(data: [:]), HTTPFilterPriority.high)])
網(wǎng)頁有內(nèi)容 <html><title>Hello, world!</title><body>Hello, world!</body></html>
就是說,打開網(wǎng)頁后晾蜘,會一串 “Hello, world!” 出現(xiàn)邻眷。
繼續(xù),運行 .build/debug/PerfectTemplate
啟動服務(wù)
[root@.../PerfectTemplate]# .build/debug/PerfectTemplate
[INFO] Starting HTTP server localhost on :::8181
ctrl + C 退出剔交!
安裝 elinks
[root@.../PerfectTemplate]# sudo apt-get -y install elinks
......
Setting up libfsplib0 (0.11-2) ...
Setting up liblua5.1-0:amd64 (5.1.5-8ubuntu1) ...
Setting up libtre5:amd64 (0.8.0-4) ...
Setting up elinks-data (0.12~pre6-11build2) ...
Setting up elinks (0.12~pre6-11build2) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...
我使用的是 Xsell 6 終端肆饶,先在一個命令行運行 Perfect
[root@.../PerfectTemplate]# .build/debug/PerfectTemplate
[INFO] Starting HTTP server localhost on :::8181
再打開一個命令行,用 elinks 打開就可以看到了
[root@.../PerfectTemplate]# elinks 私網(wǎng) IP:8181
Hello, world!
q 退出 elinks 岖常!
動畫演示
如果不想使用 elinks 可以直接
[root@/.../PerfectTemplate]# curl http://127.0.0.1:8181
<html><title>Hello, world!</title><body>Hello, world!</body></html>
已經(jīng)成功了驯镊!