c++服务器端开发⼊门_服务器端快速⼊门
c++服务器端开发⼊门
In 2014 WWDC Apple introduced Swift, a brand new programming language with which we can write apps for iOS and Mac OS. Writing code in Swift is great fun and I simply love this language. In 2016 Apple also announced Swift support for Linux and we can write server-side code with swift.
2014年,WWDC苹果公司推出了Swift,这是⼀种全新的编程语⾔,我们可以使⽤它编写适⽤于iOS和Mac OS的应⽤程序。 ⽤Swift编写代码⾮常有趣,我只是喜欢这种语⾔。 苹果在2016年还宣布Swift⽀持Linux,我们可以快速编写服务器端代码。
Being an iOS developer I was very curious about how the server code is written, especially the web services which we consume in our mobile apps. This curiosity drives me to learn more about server-side swift.
作为⼀名iOS开发⼈员,我很好奇服务器代码的编写⽅式,尤其是我们在移动应⽤程序中使⽤的Web服务。 这种好奇⼼驱使我进⼀步了解服务器端swift。
There are multiple frameworks available to write server codes with swift like Kitura(developed by IBM),
Perfect, Vapor, etc. We will be writing server-side code in swift with Vapor.
有多种框架可⽤于快速编写服务器代码,例如Kitura(由IBM开发),Perfect,Vapor等。我们将使⽤Vapor快速编写服务器端代码。
Why Vapor?
为什么要蒸⽓?
Fluent: Fluent is an ORM provided by Vapor. With Fluent we can create Models for tables in the database. We can query tables as very similar as we query our entities in Core-Data in iOS. Due to ORM, you won’t have to write direct
database queries.
Fluent: Fluent是Vapor提供的ORM。 使⽤Fluent,我们可以为数据库中的表创建模型。 我们可以像在iOS的Core-Data中查询实体⼀样查询表。 由于ORM,您不必编写直接的数据库查询。
Continuous evolution: We get continuous updates of this framework with new Swift versions. Vapor 3.0 was completely re-written to support Apple’s non-blocking networking framework Swift NIO. That
means Vapor is a non-blocking framework. You don’t have to worry if 2 API calls are hit by the user at the same time. Vapor itself will take care of that.
持续发展:我们使⽤新的Swift版本不断更新此框架。 完全重写了Vapor 3.0,以⽀持Apple的⾮阻塞⽹络框架Swift NIO。 这意味着Vapor是⼀个⾮阻塞框架。 您不必担⼼⽤户是否同时点击了2个API调⽤。 蒸⽓本⾝会解决这个问题。
Huge community: The last and most important reason I recommend Vapor over others is its huge community. Vapor has a huge community on different platforms like Slack, Discord. These are the places where you get instant help.
庞⼤的社区:我向其他⼈推荐Vapor的最后⼀个也是最重要的原因是庞⼤的社区。 Vapor在Slack,Discord等不同平台上拥有庞⼤的社区。 这些都是您获得即时帮助的地⽅。
Install Vapor
安装蒸⽓
To install Vapor refer to link for mac OS. Linux refers to link.
要安装Vapor,请参阅Mac OS的链接。 Linux是指链接。
First Vapor Project
第⼀个蒸⽓项⽬
Go to the desired directory path on the terminal and hit command
转到终端上所需的⽬录路径,然后单击命令
vapor new Todo
You can see that a new folder has been created at the path you had selected with the project name.
xcode入门您可以看到在使⽤项⽬名称选择的路径上已经创建了⼀个新⽂件夹。
Now we have to go to the project directory path. Hit following command on terminal
现在我们必须转到项⽬⽬录路径。 在终端上点击以下命令
cd Todo
Now hit following command
现在点击以下命令
vapor xcode
Now open the project in Xcode and in run scheme select My Mac and build and run the project as shown in the following image.
现在,在Xcode中打开项⽬,然后在运⾏⽅案中选择My Mac,然后如下图所⽰构建并运⾏该项⽬。
Image for post
Select Run Scheme and device My Mac to run the project
选择“运⾏⽅案”和“设备我的Mac”以运⾏项⽬
如果看到服务器从 。 现在,在浏览器中点击localhost URL,您将看到⽂本“ 它有效”! 恭喜你 您已经成功运⾏了第⼀个Swift服务器代码。 参考下图
Image for post
Output when you hit localhost URL in browser
当您在浏览器中命中localhost URL时输出
Understanding the project structure
了解项⽬结构
Package.swift
Package.swift
This file manages all dependencies we need in our project. Here you can see two dependencies, Vapor itself and Fluen-SQLite which is ORM provided by Vapor.
该⽂件管理我们项⽬中需要的所有依赖项。 在这⾥,您可以看到两个依赖项,即Vapor本⾝和Fluen-SQLite,后者是Vapor提供的ORM。
Todo.swift
托多·斯威夫特
The table will have two columns id and title. You can see Todo is confirming protocols like SQLiteMod
el, Content, Migration, and Parameter. For now let us only talk about content. Content confirms swift protocol codable. All of us know how codable has made our lives easier to create models from the JSON response. Content does a similar thing here. If your model is confirming content protocol, you don’t have to do anything else to return it as JSON in API response.
该表将具有两列id和title。 您可以看到Todo正在确认协议,例如SQLiteModel,Content,Migration和Parameter。 现在,让我们仅谈论内容。 内容确认快速协议可编码。 我们所有⼈都知道,可编码如何使我们的⽣活更容易从JSON响应创建模型。 内容在这⾥做类似的事情。 如果您的模型正在确认内容协议,则⽆需执⾏任何其他操作即可在API响应中将其作为JSON返回。
configure.swift
configure.swift
As its name suggests, everything we need in the app has to be configured here. You can see the last line in the code snippet where we are registering the model. Unless and until you register the model in this file like above, the respective table
won’t be created in the database.
顾名思义,必须在此处配置应⽤程序中需要的所有内容。 您可以在代码段中看到我们注册模型的最后⼀⾏。 除⾮并且直到您像上⾯⼀样在该⽂件中注册模型,否则不会在数据库中创建相应的表。
TodoController.swift
TodoController.swift
All the operations we have to do with Todo table with the help of web services, we’ll be doing them in this file. Here you can see the functions to fetch all todos and functions to create and delete todos.
在Web服务的帮助下,我们必须与Todo表进⾏的所有操作,我们将在此⽂件中进⾏。 在这⾥,您可以看到提取所有待办事项的功能以及创建和删除待办事项的功能。
routes.swift
路线
在此⽂件中,我们使⽤带有URL端点的控制器中编写的函数来调⽤服务。 当您⾸次运⾏该应⽤程序时,它会显⽰“ It works!”。 在浏览器上。 您可以在第⼀条路线中看到它。 现在,如果您在本地主机URL上附加“ hello”,则最终URL将为 ,您将看到“世界你好!”。 作为浏览器上的输出。 参考下图
Image for post
Response of hello world
你好世界的回应
You can test create, get all, and delete service outputs with REST clients like the postman.
您可以使⽤REST客户端(如邮递员)测试创建,获取所有内容以及删除服务输出。
What Next
接下来是什么
Here we just explored the template project created by Vapor. In the next article, we’ll create a new table with the PostgreSQL database and will try to perform CRUD operations and we’ll also learn how to query the tables in the database.
在这⾥,我们只是探讨了由Vapor创建的模板项⽬。 在下⼀篇⽂章中,我们将使⽤PostgreSQL数据库创建⼀个新表,并将尝试执⾏CRUD操作,并且还将学习如何查询数据库中的表。
Till then, Keep Calm and Keep Coding.
直到那时,保持冷静并保持编码。
c++服务器端开发⼊门

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。