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 this link for mac OS. Linux refers to this 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.

您可以看到在使用项目名称选择的路径上已经创建了一个新文件夹。

  • 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”以运行项目
  • If you see Server starting on http://localhost:8080. Now hit the localhost URL on the browser and you’ll see the text It works! Congratulations. You’ve run your first Swift server code successfully. Refer following image

    如果看到服务器从 http:// localhost:8080启动 现在,在浏览器中点击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 SQLiteModel, 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

    路线

In this file we call the services with the functions written in the controller with the URL endpoints. When you run the app for the first time, it printed “It works!” on the browser. You can see it in the first route. Now if you append “hello” on the localhost URL, the final URL would be http://localhost:8080/hello and you’ll see “Hello, world!” as an output on the browser. Refer following image

在此文件中,我们使用带有URL端点的控制器中编写的函数来调用服务。 当您首次运行该应用程序时,它会显示“ It works!”。 在浏览器上。 您可以在第一条路线中看到它。 现在,如果您在本地主机URL上附加“ hello”,则最终URL将为http:// localhost:8080 / hello ,您将看到“世界你好!”。 作为浏览器上的输出。 参考下图

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.

直到那时,保持冷静并保持编码。

翻译自: https://medium.com/macoclock/getting-started-with-server-side-swift-with-vapor-84497d9b7257

c++服务器端开发入门

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值