首頁 後端開發 Golang 使用 Torpedo 建立您的第一個專案:逐步指南

使用 Torpedo 建立您的第一個專案:逐步指南

Nov 18, 2024 am 09:06 AM

Creating Your First Project with Torpedo: A Step-by-Step Guide

在 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.0
kind: entity
spec:
    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.0
kind: entity
spec:
    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

mkdir booking-fly && cd booking-fly
登入後複製
登入後複製

此定義告訴 Torpedo 建立框架程式碼來放置自訂邏輯,用於處理給定行程和使用者的飛行預訂。

Torpedo 將建立完整的用例,包括與您的實體的互動。

完成下一步 (#5) 後,請閱讀快速入門指南,以了解如何在用例中產生的框架用例中編寫邏輯

5. 將它們連接在一起

定義實體和用例後,Torpedo 確保這些元件之間的連接遵循六邊形架構原則。用例將透過服務介面與實體交互,而您的適配器(例如資料庫或 API)則處理持久性和外部通訊。

現在是時候編寫您的應用程式規格以將所有內容放在一起了!應用程式定義是最重要的文件,因為這裡描述了您的應用程式。以下範例顯示如何定義 Booking Fly 應用程式:

.torpedo/app.yaml

torpedo init
登入後複製
登入後複製

要產生應用程式程式碼(實體、用例等),請執行指令:

version: torpedo.darksub.io/v1.0
kind: entity
spec:
    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 

登入後複製
登入後複製

此指令將產生一個專案鷹架,設定基於六邊形架構的目錄結構。該專案將包括實體用例適配器的核心資料夾。它確保您的業務邏輯和基礎設施從一開始就保持解耦。

您現在可以透過新增更多實體、用例甚至自訂適配器來擴充您的專案。 Torpedo 的結構可讓您保持程式碼整潔和模組化,從而可以隨著應用程式的增長輕鬆擴展應用程式。

另請參閱如何使用產生的用例程式碼編寫自己的邏輯。

6. 運行您的應用程式

設定實體和用例後,您就可以執行應用程式了。 Torpedo 包含一個基於 Gin Gonic 專案的輕量級伺服器,您可以執行它來進行測試和開發。只要使用:

不要忘記在更新依賴項之前運行 go mod tidy!

version: torpedo.darksub.io/v1.0
kind: entity
spec:
    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

登入後複製
登入後複製

您現在可以與應用程式的 API 交互,運行您定義的 CRUD 操作和用例。

7. 下一步是什麼?

Torpedo 使用六角形架構可以輕鬆產生乾淨、結構化的 Go 程式碼。但這只是開始!您可以透過添加更複雜的工作流程、整合外部服務以及自訂框架來繼續探索 Torpedo 的功能以滿足您的需求。

請繼續關注 Torpedo 即將推出的更多高級功能,並在探索可能性時隨時分享您的回饋!


結論

使用 Torpedo 建立您的第一個專案既簡單又快速。透過利用 YAML 中實體模式和用例定義的強大功能,您可以快速建立健壯的 Golang 應用程序,同時保持簡潔的架構原則。現在是時候投入並開始建造了!讓我們知道您的想法以及 Torpedo 如何幫助您未來的專案。

以上是使用 Torpedo 建立您的第一個專案:逐步指南的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1655
14
CakePHP 教程
1413
52
Laravel 教程
1306
25
PHP教程
1252
29
C# 教程
1226
24
Golang的目的:建立高效且可擴展的系統 Golang的目的:建立高效且可擴展的系統 Apr 09, 2025 pm 05:17 PM

Go語言在構建高效且可擴展的系統中表現出色,其優勢包括:1.高性能:編譯成機器碼,運行速度快;2.並發編程:通過goroutines和channels簡化多任務處理;3.簡潔性:語法簡潔,降低學習和維護成本;4.跨平台:支持跨平台編譯,方便部署。

Golang和C:並發與原始速度 Golang和C:並發與原始速度 Apr 21, 2025 am 12:16 AM

Golang在並發性上優於C ,而C 在原始速度上優於Golang。 1)Golang通過goroutine和channel實現高效並發,適合處理大量並發任務。 2)C 通過編譯器優化和標準庫,提供接近硬件的高性能,適合需要極致優化的應用。

Golang vs. Python:主要差異和相似之處 Golang vs. Python:主要差異和相似之處 Apr 17, 2025 am 12:15 AM

Golang和Python各有优势:Golang适合高性能和并发编程,Python适用于数据科学和Web开发。Golang以其并发模型和高效性能著称,Python则以简洁语法和丰富库生态系统著称。

Golang vs. Python:性能和可伸縮性 Golang vs. Python:性能和可伸縮性 Apr 19, 2025 am 12:18 AM

Golang在性能和可擴展性方面優於Python。 1)Golang的編譯型特性和高效並發模型使其在高並發場景下表現出色。 2)Python作為解釋型語言,執行速度較慢,但通過工具如Cython可優化性能。

表演競賽:Golang vs.C 表演競賽:Golang vs.C Apr 16, 2025 am 12:07 AM

Golang和C 在性能競賽中的表現各有優勢:1)Golang適合高並發和快速開發,2)C 提供更高性能和細粒度控制。選擇應基於項目需求和團隊技術棧。

Golang的影響:速度,效率和簡單性 Golang的影響:速度,效率和簡單性 Apr 14, 2025 am 12:11 AM

goimpactsdevelopmentpositationality throughspeed,效率和模擬性。 1)速度:gocompilesquicklyandrunseff,IdealforlargeProjects.2)效率:效率:ITScomprehenSevestAndardArdardArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdEcceSteral Depentencies,增強的Depleflovelmentimency.3)簡單性。

Golang和C:性能的權衡 Golang和C:性能的權衡 Apr 17, 2025 am 12:18 AM

Golang和C 在性能上的差異主要體現在內存管理、編譯優化和運行時效率等方面。 1)Golang的垃圾回收機制方便但可能影響性能,2)C 的手動內存管理和編譯器優化在遞歸計算中表現更為高效。

C和Golang:表演至關重要時 C和Golang:表演至關重要時 Apr 13, 2025 am 12:11 AM

C 更適合需要直接控制硬件資源和高性能優化的場景,而Golang更適合需要快速開發和高並發處理的場景。 1.C 的優勢在於其接近硬件的特性和高度的優化能力,適合遊戲開發等高性能需求。 2.Golang的優勢在於其簡潔的語法和天然的並發支持,適合高並發服務開發。

See all articles