随着 Swift 语言在服务端开发中的日益普及,以 Vapor 为代表的 Swift Web 框架也越来越受到开发者的青睐。今天,我们向大家介绍一款流行的 Swift 语言 Web 框架——Vapor。Vapor 基于非阻塞事件驱动库 SwiftNIO 构建,提供了 ORM、模板引擎、用户身份验证等模块,助力开发者快速构建高性能的 Web 应用和 API 服务。
项目概述
Vapor 是一个基于 SwiftNIO 构建的流行 Swift 语言 Web 框架。自2016年1月19日开源以来,Vapor 因其高性能和易用性,迅速在 GitHub 上积累了23.9k⭐ 的关注。
项目地址:Vapor on GitHub
GitHub趋势榜表现
2024-06-12:入选日榜,日增15 stars
2024-06-13:入选日榜,日增18 stars
2024-06-13:入选周榜,周增42 stars
项目信息
开源时间:2016-01-19
最后更新:2024-06-11
主要语言:Swift
项目分类:[中间件] [后端]
推荐理由:一个流行的 Swift 语言 Web 框架,基于非阻塞事件驱动库 SwiftNIO 构建。它提供了 ORM、模板引擎、用户身份验证等模块,能够快速创建网站、接口等服务。Vapor 的设计强调性能和易用性,适合用于构建高性能的 Web 应用和 API 服务。
Vapor的优势
1.高性能
Vapor 基于 SwiftNIO 构建,利用非阻塞事件驱动模型,提供卓越的性能和并发处理能力。
2.模块化设计
Vapor 提供了丰富的模块,包括ORM(Fluent)、模板引擎(Leaf)、用户身份验证、文件处理等,让开发者能快速构建功能完备的应用。
3.Swift 生态兼容
作为纯 Swift 编写的框架,Vapor 完美兼容 Swift 生态系统,支持使用 Swift 的所有特性和工具。
4.简易配置
Vapor 的配置和使用流程简洁清晰,内置了大量开发辅助工具,降低了上手门槛。
安装与使用
1.安装 Vapor CLI
首先,通过 Homebrew 安装 Vapor 的命令行工具(CLI)。
# 安装 Vapor CLIbrew install vapor
2.创建新项目
使用 Vapor CLI 创建一个新的项目。
# 创建新项目vapor new MyVaporApp# 进入项目目录cd MyVaporApp
3.启动开发服务器
在项目目录中,使用 Vapor CLI 启动开发服务器。
# 启动开发服务器vapor serve
服务器启动后,可以在浏览器中访问 http://localhost:8080 查看默认的欢迎页面。
4.定义路由
在 Vapor 项目中,通过 configure.swift 文件定义路由和处理函数。
// Sources/App/configure.swift import Vaporpublic func configure(_ app: Application) throws { // 配置路由 app.get { req -> String in return "Hello, Vapor!" } app.get("hello") { req -> String in return "Hello, world!" } }
5.使用ORM
Vapor 集成了 Fluent ORM,可以轻松进行数据库操作。以下是在项目中使用 Fluent 定义模型和进行 CRUD 操作的示例。
// 在项目中添加 Fluent 支持(Package.swift) dependencies: [ .package(url: "https://github.com/vapor/fluent.git", from: "4.0.0"), .package(url: "https://github.com/vapor/fluent-sqlite-driver.git", from: "4.0.0") ],targets: [ .target(name: "App", dependencies: [ .product(name: "Fluent", package: "fluent"), .product(name: "FluentSQLiteDriver", package: "fluent-sqlite-driver"), .product(name: "Vapor", package: "vapor") ]) ]
// 定义模型(Sources/App/Models/Todo.swift) import Fluentimport Vaporfinal class Todo: Model, Content { static let schema = "todos" @ID(key: .id) var id: UUID? @Field(key: "title") var title: String init() { } init(id: UUID? = nil, title: String) { self.id = id self.title = title } }
// 配置数据库和迁移(Sources/App/configure.swift) import Fluentimport FluentSQLiteDriverimport Vaporpublic func configure(_ app: Application) throws { app.databases.use(.sqlite(.file("db.sqlite")), as: .sqlite) app.migrations.add(CreateTodo()) try app.autoMigrate().wait() // 配置路由 app.get { req in return "It works!" } // CRUD 操作示例 app.post("todo") { req -> EventLoopFuture<Todo> in let todo = try req.content.decode(Todo.self) return todo.save(on: req.db).map { todo } } app.get("todos") { req -> EventLoopFuture<[Todo]> in return Todo.query(on: req.db).all() } }
实战案例
以下是一个使用 Vapor 构建任务管理系统的简单案例,演示了如何快速构建具有增删查改功能的 Web 应用。
1.项目结构
MyTaskManager/ │ ├── Sources/ │ ├── App/ │ │ ├── Models/ │ │ │ └── Task.swift │ │ ├── configure.swift │ │ └── routes.swift │ └── Run/ │ └── main.swift └── Package.swift
2.定义任务模型
// Sources/App/Models/Task.swiftimport Fluent import Vaporfinal class Task: Model, Content { static let schema = "tasks" @ID(key: .id) var id: UUID? @Field(key: "title") var title: String @Field(key: "isCompleted") var isCompleted: Bool init() { } init(id: UUID? = nil, title: String, isCompleted: Bool = false) { self.id = id self.title = title self.isCompleted = isCompleted } }
3.配置数据库和迁移
// Sources/App/configure.swiftimport Fluent import FluentSQLiteDriverimport Vaporpublic func configure(_ app: Application) throws { app.databases.use(.sqlite(.file("db.sqlite")), as: .sqlite) app.migrations.add(CreateTask()) try app.autoMigrate().wait() // 配置路由 try routes(app) }
// 定义迁移(Sources/App/Migrations/CreateTask.swift) import Fluentstruct CreateTask: Migration { func prepare(on database: Database) -> EventLoopFuture<Void> { database.schema("tasks") .id() .field("title", .string, .required) .field("isCompleted", .bool, .required) .create() } func revert(on database: Database) -> EventLoopFuture<Void> { database.schema("tasks").delete() } }
来源:
互联网
本文观点不代表源码解析立场,不承担法律责任,文章及观点也不构成任何投资意见。
评论列表