目 录CONTENT

文章目录

新坑尝鲜 | 使用 Swift 框架 Vapor 搭建 Web 网站

Wissy
2021-08-02 / 0 评论 / 0 点赞 / 65 阅读 / 0 字

前言

今天在浏览网站的时候发现有一篇 Linux 的 Swift 语言简单入门。经历6个小时的网络搜索终于搞定。

随着 Swift 语言的开源,不再是 Apple 系列的专属语言了。我们可以将 Swift 的代码部署到 Linux 服务器,尽一步促进 Swift & Apple 生态的发展。

而几乎没有预操作,内存更小,可以打成可执行包,ARC(而不是跟踪垃圾收集)垃圾回收,成为服务器界的一霸指日可待。

Vapor 是 Swift Top1 的 Web 框架,中文文档也是比较全面。

开始吧

如果你的服务器访问不到 Github 服务器,请配置以下

# 将 github.com 替换成国内加速地址
git config --global url."https://hub.fastgit.org/".insteadOf https://github.com/

安装 Swift

如果你是 Mac 上面测试,忽略这步吧。Swift 官网比较全的文档,其他系统自取。

[wissy@wissy PristisApi]$ cat /etc/centos-release
CentOS Linux release 8.4.2105
安装依赖

安装 main repo 拥有的包

yum install binutils gcc git libbsd-devel libedit libicu-devel pkg-config python2 sqlite

安装 CentOS-PowerTools repo

# 下载 repo 文件
wget https://gist.githubusercontent.com/austinsonger/23bfc812382b44eaed001908e94bc2cf/raw/4d6f8e11f2bd1387b9a544160481e1428c1519b8/CentOS-PowerTools.repo \
-O /etc/yum.repos.d/CentOS-PowerTools.repo 
# 更新 index 缓存
yum update

安装官网显示其他依赖

yum install glibc-static libstdc++-static libedit-devel 

安装 <sys/parm.h>​ 依赖

dnf install glibc-headers

报错 could not build module 'SwiftGlibc'​,请安装官网指定的依赖,切记不能跳过。

下载 Swift 包

Swift 包有Development,Releases,Snapshots,建议生产部署使用 Releases 版。

其他系统请换成相应地址。

# 下载 Swift 5.4.2
wget https://swift.org/builds/swift-5.4.2-release/centos8/swift-5.4.2-RELEASE/swift-5.4.2-RELEASE-centos8.tar.gz

# 导入 PGP 钥匙串
wget -q -O - https://swift.org/keys/all-keys.asc | gpg --import -

# 验证
略。。。

# 解压
tar xavf wift-5.4.2-RELEASE-centos8.tar.gz
mv swift-5.4.2-RELEASE-centos8 /opt/software/swift/

# 设置环境变量 /etc/profile
# Swift
export SWIFT_HOME="/opt/software/swift"
export C_INCLUDE_PATH=$SWIFT_HOME/usr/lib/swift/clang/include/
export CPLUS_INCLUDE_PATH=$C_INCLUDE_PATH
export PATH=$SWIFT_HOME/usr/bin:$PATH

#重新加载一下添加的环境变量文件
source /etc/profile
测试 Swift
[wissy@wissy PristisApi]$  swift --version
Swift version 5.4.2 (swift-5.4.2-RELEASE)
Target: x86_64-unknown-linux-gnu

写个脚本简单测试一下

[wissy@wissy PristisApi]$ echo "print(\"Hello World\")" > hello.swift
[wissy@wissy PristisApi]$ swift hello.swift
Hello World

创建一个 Package 全面测试一下

[wissy@wissy swift]$ mkdir Test
[wissy@wissy swift]$ cd Test/
[wissy@wissy Test]$ swift package init --type executable
Creating executable package: Test
Creating Package.swift
Creating README.md
Creating .gitignore
Creating Sources/
Creating Sources/Test/main.swift
Creating Tests/
Creating Tests/TestTests/
Creating Tests/TestTests/TestTests.swift
[wissy@wissy Test]$ swift test
[13/13] Build complete!
Test Suite 'All tests' started at 2021-08-01 11:57:59.307
Test Suite 'debug.xctest' started at 2021-08-01 11:57:59.308
Test Suite 'TestTests' started at 2021-08-01 11:57:59.308
Test Case 'TestTests.testExample' started at 2021-08-01 11:57:59.308
Test Case 'TestTests.testExample' passed (0.052 seconds)
Test Suite 'TestTests' passed at 2021-08-01 11:57:59.361
     Executed 1 test, with 0 failures (0 unexpected) in 0.052 (0.052) seconds
Test Suite 'debug.xctest' passed at 2021-08-01 11:57:59.361
     Executed 1 test, with 0 failures (0 unexpected) in 0.052 (0.052) seconds
Test Suite 'All tests' passed at 2021-08-01 11:57:59.361
     Executed 1 test, with 0 failures (0 unexpected) in 0.052 (0.052) seconds
[wissy@wissy Test]$ swift run
[0/0] Build complete!
Hello, world!

安装 Vapor

编译 Vapor 工具包

[scode type="yellow"]mac 直接使用 brew install vapor​安装[/scode]

git clone https://github.com/vapor/toolbox.git
cd toolbox
git checkout <desired version>
swift build -c release --disable-sandbox --enable-test-discovery
mv .build/release/vapor /usr/local/bin
创建自己的项目
[wissy@wissy swift]$ vapor new Test
Cloning template...
name: Test
Would you like to use Fluent? (--fluent/--no-fluent)
y/n> y
fluent: Yes
db: SQLite
Would you like to use Leaf? (--leaf/--no-leaf)
y/n> y
leaf: Yes
Generating project files
+ Package.swift
+ main.swift
+ configure.swift
+ routes.swift
+ Todo.swift
+ CreateTodo.swift
+ .gitkeep
+ TodoController.swift
+ AppTests.swift
+ index.leaf
+ Dockerfile
+ docker-compose.yml
+ .gitignore
+ .dockerignore
Creating git repository
Adding first commit

                                                                                                                      **
                                                                                                                    **~~**
                                                                                                                  **~~~~~~**
                                                                                                                **~~~~~~~~~~**
                                                                                                              **~~~~~~~~~~~~~~**
                                                                                                            **~~~~~~~~~~~~~~~~~~**
                                                                                                          **~~~~~~~~~~~~~~~~~~~~~~**
                                                                                                         **~~~~~~~~~~~~~~~~~~~~~~~~**
                                                                                                        **~~~~~~~~~~~~~~~~~~~~~~~~~~**
                                                                                                       **~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
                                                                                                       **~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
                                                                                                       **~~~~~~~~~~~~~~~~~~~~~++++~~~**
                                                                                                        **~~~~~~~~~~~~~~~~~~~++++~~~**
                                                                                                         ***~~~~~~~~~~~~~~~++++~~~***
                                                                                                           ****~~~~~~~~~~++++~~****
                                                                                                              *****~~~~~~~~~*****
                                                                                                                 *************

                                                                                                        _       __    ___   ___   ___
                                                                                                       \ \  /  / /\  | |_) / / \ | |_)
                                                                                                        \_\/  /_/--\ |_|   \_\_/ |_| \
                                                                                                          a web framework for Swift

                                                                                                        Project Test has been created!

                                                                                                 Use cd 'Test' to enter the project directory
                                                                                                 Use vapor xcode to open the project in Xcode
[wissy@wissy swift]$
运行项目 & 安装依赖

因为我这边用到了 sqlite 需要安装一下开发包,系统的 zlib 也需要开发包

dnf install zlib-devel
dnf install libsq3-devel
使用 vapor 工具运行
[wissy@wissy PristisApi]$ vapor run serve --port 1337 --hostname 127.0.0.1
warning: '--enable-test-discovery' option is deprecated; tests are automatically discovered on all platforms
[0/0] Build complete!
[ NOTICE ] Server starting on http://127.0.0.1:1337
使用 swift run

如果需要自定义端口和地址需要修改 Sources/App/configure.swift​

   app.http.server.configuration.port = 1337
   app.http.server.configuration.hostname = "127.0.0.1"
[wissy@wissy PristisApi]$ swift run
[0/0] Build complete!
[ NOTICE ] Server starting on http://127.0.0.1:1337

结尾

?恭喜你,到了这一步你已经完成了Swift Web Vapor 入门。

0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区