美文网首页iOS进阶高级Swift服务端开发
Swift服务端开发系列:(三)Ubuntu安装最新版Vapor

Swift服务端开发系列:(三)Ubuntu安装最新版Vapor

作者: 择势量投 | 来源:发表于2019-12-22 01:48 被阅读0次

    Swift服务端开发系列完整版

    swift服务端开发系列

    1、下载官方安装脚本

    要安装Vapor,您需要从Vapor开发人员那里下载并执行脚本,该脚本会将Vapor的官方软件包存储库添加到服务器的软件包列表中。然后,您将用于apt安装最新版本的Vapor。

    首先,使用curl带有-o开关的命令将安装脚本下载到服务器,以指定本地文件名:

    curl -sL apt.vapor.sh -o apt.vapor.sh
    

    若提示:

    Command 'curl' not found, but can be installed with: sudo apt install curl
    

    安装 curl

    sudo apt install curl
    

    再次下载脚本 apt.vapor.sh。在不首先检查脚本的情况下执行从他人下载的脚本通常不是一个好的安全实践,可使用less命令查看脚本内容(可省略):

    less apt.vapor.sh
    

    检查安装脚本的内容后,按q 退出less 程序

    2、安装vapor

    执行脚本以添加存储库:

    bash ./apt.vapor.sh
    

    这个时候,系统可能会提示您输入sudo的密码。输入后,一旦脚本运行完成,您可以安装vapor软件包及其依赖项。

    sudo apt-get install vapor
    

    检查vapor 安装情况

    vapor version
    

    安装成功

    Vapor Toolbox: 3.1.10
    

    3、使用vapor创建一个swift应用

    安装完成后,我们来创建一个demo

    vapor new demo
    
    Cloning Template [Done]
    Updating Package Name [Done]
    Initializing git repository [Done]
    
                            **
                          **~~**
                        **~~~~~~**
                      **~~~~~~~~~~**
                    **~~~~~~~~~~~~~~**
                   **~~~~~~~~~~~~~~~~~~**
                 **~~~~~~~~~~~~~~~~~~~~~~**
                **~~~~~~~~~~~~~~~~~~~~~~~~**
               **~~~~~~~~~~~~~~~~~~~~~~~~~~**
              **~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
              **~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
              **~~~~~~~~~~~~~~~~~~~~~++++~~~**
               **~~~~~~~~~~~~~~~~~~~++++~~~**
                ***~~~~~~~~~~~~~~~++++~~~***
                  ****~~~~~~~~~~++++~~****
                    *****~~~~~~~~~*****
                       *************
               
               _       __    ___   ___   ___
              \ \  /  / /\  | |_) / / \ | |_)
               \_\/  /_/--\ |_|   \_\_/ |_| \
                 a web framework for Swift
    
              Project "demo" has been created.
              Type `cd demo` to enter the project directory.
                 Use `vapor cloud deploy` to host your project for free!
                    Enjoy!
    

    demo 创建成功后,切换到新创建的demo文件夹:

    cd demo
    

    然后执行vapor build命令以编译swift应用程序。

    vapor build
    

    首次构建应用程序时,该过程将获取一些依赖项,时间相对较长,大概5分钟左右(与网络状况有关),vapor将缓存它们,并在以后跳过此步骤,这将使构建过程更快。

    构建过程完成后,使用以下命令运行应用程序:

    vapor run serve
    

    终端输出如下

    Running demo ...
    [ INFO ] Migrating 'sqlite' database (/home/ubuntu/demo/.build/checkouts/fluent/Sources/Fluent/Migration/MigrationConfig.swift:69)
    [ INFO ] Preparing migration 'Todo' (/home/ubuntu/demo/.build/checkouts/fluent/Sources/Fluent/Migration/Migrations.swift:111)
    [ INFO ] Migrations complete (/home/ubuntu/demo/.build/checkouts/fluent/Sources/Fluent/Migration/MigrationConfig.swift:73)
    Server starting on http://localhost:8080
    

    vapor 服务端启动成功!!

    推荐阅读:Swift服务端开发系列完整版

    相关文章

      网友评论

        本文标题:Swift服务端开发系列:(三)Ubuntu安装最新版Vapor

        本文链接:https://www.haomeiwen.com/subject/afgnnctx.html