美文网首页Vapor
Vapor系列教程 - Droplet

Vapor系列教程 - Droplet

作者: CaryZheng | 来源:发表于2016-08-29 17:42 被阅读351次

Droplet 是一个比较综合的类,它负责路由的注册、服务的启动和中间件的添加等等。

初始化


编辑 main.swift

import Vapor

let drop = Droplet()

drop.serve()

使用默认参数配置的 Droplet 就被构造出来了。

同时,也可以自定义构造 Droplet

public init(
        // non-providable
        arguments: [String]? = nil,
        workDir workDirProvided: String? = nil,
        config configProvided: Config? = nil,
        localization localizationProvided: Localization? = nil,

        // providable
        server: ServerProtocol.Type? = nil,
        sessions: Sessions? = nil,
        hash: Hash? = nil,
        console: ConsoleProtocol? = nil,
        log: Log? = nil,
        client: ClientProtocol.Type? = nil,
        database: Database? = nil,

        // database preparations
        preparations: [Preparation.Type] = [],

        // providers
        providers providerTypes: [Provider.Type] = [],
        initializedProviders: [Provider] = []
    )

环境


定义

Environment 是一个枚举类型,具体定义如下

/**
    Represents the current environment the
    droplet is running in. This information
    can be used to conditionally show debug or testing information.
*/
public enum Environment: Equatable {
    case production
    case test
    case development
    case custom(String)

    init(id string: String) {
        switch string.lowercased() {
        case "production", "prod":
            self = .production
        case "test":
            self = .test
        case "development", "dev":
            self = .development
        default:
            self = .custom(string)
        }
    }
}

分为 productiontestdevelopmentcustom 环境类型。

使用

let env = drop.config.environment
if .production == env {
    print("environment = production")
} else if .test == env {
    print("environment = test")
} else if .development == env {
    print("environment = development")
} else {
    print("environment = \(env)")
}

可通过执行如下命令来指定当前环境类型

  • production
vapor run serve --env=production

vapor run serve --env=prod

输出

environment = production

  • test
vapor run serve --env=test

输出

environment = test

  • development
vapor run serve --env=development

vapor run serve --env=dev

输出

environment = development

  • ** custom**
vapor run serve --env=myenv

输出

environment = myenv

其中, myenv 也可以是其它任意字符串。

注意

  1. 如果未明确指定环境的话,默认将以 development 环境执行。
  2. production 环境下可以过滤 Debug 日志以提高性能,建议最终部署的时候选择 production 环境。

工作目录


可通过 drop.workDir 获取当前工作目录。

print("workDir = \(drop.workDir)")

输出

workDir = /Users/Cary/Documents/Swift/Vapor/Project/vapor_test/

注意: 输出的路径由执行时所在路径决定。


Go to Vapor系列教程 - 目录

相关文章

  • Vapor系列教程 - Droplet

    Droplet 是一个比较综合的类,它负责路由的注册、服务的启动和中间件的添加等等。 初始化 编辑 main.sw...

  • Vapor 2.0 - Droplet

    前往 Vapor 2.0 - 文档目录 Droplet是一个服务容器,它可以给你提供许多Vapor工具。它负责注册...

  • Vapor学习

    通过将Vapor官方文档进行梳理,了解Vapor所涉及到的知识点 Vapor英文教程Vapor中文教程官方Github

  • Vapor文档学习十二:Hash

    Vapor哈希化很容易。 Example 在Droplet中使用hash类,将字符串转换为哈希值。 SHA2Has...

  • Vapor文档学习一 :Droplet

    说明:这系列文章都是基于Vapor文档的理解与翻译,仅供个人学习使用。 Hello world 关于如何安装Vap...

  • Vapor系列教程 - Hash

    Vapor 内置支持 Hash 。 示例 想要获取一个字符串的 Hash 值,只需使用 Droplet 中的 ha...

  • Vapor系列教程 - 目录

    Vapor Version: v0.16 介绍 第一章 Vapor 起航1.1 macOS 安装 Swift1.2...

  • Vapor系列教程 - 介绍

    Vapor 是基于 Swift 实现的 Web 框架与服务,可运行于 macOS 和 Ubuntu 系统上。 特性...

  • Vapor系列教程 - Views

    Vapor 可直接返回纯 HTML 页面,也可以用 Mustache 或 Stencil 模版来渲染页面。 目录 ...

  • Vapor系列教程 - Middleware

    Middleware 是现代 Web 框架中必不可少的一部分。通过 Middleware 可以单独处理 Reque...

网友评论

    本文标题:Vapor系列教程 - Droplet

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