使用 Torpedo 创建您的第一个项目:分步指南

使用 torpedo 创建您的第一个项目:分步指南

golang 中构建应用程序时,遵循六边形架构的原则可以确保代码干净、模块化和可维护。借助 torpedo,您可以轻松实现此架构,同时加快开发过程。在本指南中,我们将逐步介绍如何使用 torpedo 创建您的第一个项目,从安装到生成实体和用例。

这篇文章是已记录的快速入门指南的摘要

1. 鱼雷入门

在我们深入创建项目之前,请确保您的系统上安装了 go。然后,按照安装指南中的说明安装 torpedo

这个 cli 工具将为您处理项目生成、实体创建和用例脚手架。安装完成后,您就可以创建您的第一个项目了。

2. 设置你的第一个项目

让我们开始使用我们的第一个使用 torpedo 构建的应用程序吧!我们将开发一款名为 booking fly 的机票预订应用程序。

安装 torpedo 后,创建新项目就像运行一样简单:

mkdir booking-fly && cd booking-fly

登录后复制

torpedo init

登录后复制

此命令将生成文件夹 .torpedo,您应该在其中定义实体和用例。有关此文件夹的更多信息可以在 .torpedo dir struct

中找到

3. 定义您的第一个实体

接下来,您需要定义域实体。实体是应用程序业务逻辑中的核心对象,代表用户、产品或订单等事物。

要定义您的第一个实体,请在 .torpedo/entities 目录下创建一个 yaml 文件。例如,让我们创建一个简单的 user 实体:

.torpedo/entities/user.yaml

version: torpedo.darksub.io/v1.0kind: entityspec:    name: "user"    plural: "users"     description: "the frequent flyer user"    doc: |        the user entity represents a system user but also a frequent flyer.         this entity is only for the example purpose.    schema:        reserved:            id:                type: ulid         fields:          - name: name            type: string            description: "the user full name"          - name: email            type: string            description: "the user contact email"          - name: password # it is not recommended to save passwords, this is an example only            type: string            encrypted: true            description: "the user system password"          - name: plan            type: string            description: "the user membership plan"            validate:              list:                values:                  - gold                  - silver                  - bronze          - name: miles            type: integer            description: "the accumulated flyer miles"    relationships:        - name: trips          type: $rel          ref: ".torpedo/entities/trip.yaml"          cardinality: hasmany          load:            type: nested            metadata:                maxitems: 100    adapters:        input:            - type: http        output:          - type: memory 

登录后复制

此外,trip 实体是必需的,因此,让我们创建一个 trip 实体:

.torpedo/entities/trip.yaml

version: torpedo.darksub.io/v1.0kind: entityspec:    name: trip    plural: trips    description: "the user fly trip reservations"    doc: |        the trip entity handles all data related with the frequent flyer trip    schema:        reserved:            id:                type: ulid        fields:          - name: departure            type: string            description: "the trip departure airport"          - name: arrival            type: string            description: "the trip arrival airport"          - name: miles            type: integer            description: "the trip miles"          - name: from            type: date            description: "the trip from date"          - name: to            type: date            description: "the trip to date"    adapters:        input:            - type: http        output:            - type: memory

登录后复制

torpedo 将为 user 和 trip 实体生成 go 代码及其相应的 crud 操作,包括存储库接口和任何必要的数据库处理代码。

4. 创建用例

实体就位后,就可以使用用例定义它们如何与应用程序的工作流程交互。用例封装了作用于您的实体的业务规则和流程。

在 .torpedo/use_cases 目录下创建一个 yaml 文件来定义您的用例。以下是预订机票的简单用例示例:

.torpedo/use_cases/booking_fly.yaml

version: torpedo.darksub.io/v1.0kind: usecasespec:    name: "bookingfly"    description: "fly reservation use case"    doc: |         given a frequent flyer user should be able to do a booking fly from our well known fly routes, selecting the        departure airport and the arrival airport, also setting up the from-to fly dates. if the booking is successful, so the        system should calculate the user awards and upgrade it.    domain:        entities:            - user.yaml            - trip.yaml

登录后复制

此定义告诉 torpedo 创建框架代码来放置自定义逻辑,用于处理给定行程和用户的飞行预订。

torpedo 将构建完整的用例,包括与您的实体的交互。

完成下一步 (#5) 后,请阅读快速入门指南,了解如何在用例中生成的框架用例中编写逻辑

5. 将它们连接在一起

定义实体和用例后,torpedo 确保这些组件之间的连接遵循六边形架构原则。用例将通过服务接口与实体交互,而您的适配器(例如数据库或 api)则处理持久性和外部通信。

现在是时候编写您的应用程序规范以将所有内容放在一起了!应用程序定义是最重要的文件,因为这里描述了您的应用程序。以下示例展示了如何定义 booking fly 应用程序:

.torpedo/app.yaml

version: torpedo.darksub.io/v1.0kind: appspec:  name: "booking fly system"  description: "application example"  stack:    lang: go    package: "github.com/darksubmarine/booking-fly"   domain:    entities:      - user.yaml      - trip.yaml    usecases:      - booking_fly.yaml

登录后复制

要生成应用程序代码(实体、用例等),请运行命令:

torpedo fire

登录后复制

该命令将生成一个项目脚手架,设置基于六边形架构的目录结构。该项目将包括实体用例适配器的核心文件夹。它确保您的业务逻辑和基础设施从一开始就保持解耦。

您现在可以通过添加更多实体、用例甚至自定义适配器来扩展您的项目。 torpedo 的结构使您能够保持代码整洁和模块化,从而随着应用程序的增长轻松扩展应用程序。

另请参阅如何使用生成的用例代码编写自己的逻辑。

6. 运行您的应用程序

设置实体和用例后,您就可以运行应用程序了。 torpedo 包括一个基于 gin gonic 项目的轻量级服务器,您可以运行它来进行测试和开发。只需使用:

不要忘记在更新依赖项之前运行 go mod tidy!

go run main.go

登录后复制

您现在可以与应用程序的 api 交互,运行您定义的 crud 操作和用例。

7. 下一步是什么?

torpedo 使用六边形架构可以轻松生成干净、结构化的 go 代码。但这仅仅是开始!您可以通过添加更复杂的工作流程、集成外部服务以及自定义框架来满足您的需求,继续探索 torpedo 的功能。

请继续关注 torpedo 即将推出的更多高级功能,并在探索可能性时随时分享您的反馈!

结论

使用 torpedo 创建您的第一个项目既简单又快速。通过利用 yaml 中实体模式和用例定义的强大功能,您可以快速构建健壮的 golang 应用程序,同时保持简洁的架构原则。现在是时候投入并开始构建了!让我们知道您的想法以及 torpedo 如何帮助您未来的项目。

以上就是使用 Torpedo 创建您的第一个项目:分步指南的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2477264.html

(0)
上一篇 2025年3月4日 19:21:35
下一篇 2025年2月28日 03:52:14

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论