When building applications in Golang, adhering to the principles of Hexagonal Architecture can ensure clean, modular, and maintainable code. With Torpedo, you can easily implement this architecture while speeding up your development process. In this guide, we’ll walk through how to create your first project with Torpedo, from installation to generating entities and use cases.
This post is a summary of the documented
This CLI tool will handle project generation, entity creation, and use case scaffolding for you. Once installed, you’re ready to create your first project.
2. Setting Up Your First Project
Lets begging with our first application built with Torpedo!. We will be building a fly reservation app called Booking Fly.
With Torpedo installed, creating a new project is as simple as running:
CODEmkdir booking-fly && cd booking-fly
CODEtorpedo init
This command will generate the folder
.torpedowhere you should defines your entities and use cases. More info about this folder can be found at
5. Wiring It All Together
Once you’ve defined your entities and use cases, Torpedo ensures that the wiring between these components follows Hexagonal Architecture principles. The use cases will interact with the entities through the service interfaces, while your adapters (such as databases or APIs) handle persistence and external communication.
Now it's time to write your app specification to put all together!. The application definition is the most important file because here is described your app. The following example shows how to define the Booking Fly app:
.torpedo/app.yaml
CODEversion: torpedo.darksub.io/v1.0
kind: app
spec:
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
To generate the application code (entities, use cases and more) run the command:
CODEtorpedo fire
This command will generate a project scaffold, setting up the directory structure based on the Hexagonal Architecture. The project will include core folders for entities, use cases, and adapters. It ensures that your business logic and infrastructure remain decoupled from the get-go.
You can now extend your project by adding more entities, use cases, and even custom adapters. Torpedo’s structure allows you to keep your code clean and modular, making it easy to scale your application as it grows.
Also take a look at how to code your own logic withing generated Use Case code.
6. Running Your Application
After setting up entities and use cases, you’re ready to run your application. Torpedo includes a lightweight server, based on Gin Gonic project, that you can run for testing and development. Simply use:
Don't forget to run
go mod tidybefore to update dependencies!
CODEgo run main.go
You can now interact with your application’s API, running the CRUD operations and use cases you’ve defined.
7. What’s Next?
Torpedo makes it easy to generate clean, structured Go code with Hexagonal Architecture. But this is just the beginning! You can continue to explore Torpedo’s features by adding more complex workflows, integrating external services, and customizing the framework to suit your needs.
Stay tuned for more advanced features coming soon to Torpedo, and feel free to share your feedback as you explore what’s possible!
Conclusion
Creating your first project with Torpedo is simple and fast. By leveraging the power of entity schemas and use case definitions in YAML, you can quickly scaffold a robust Golang application while maintaining clean architectural principles. Now it's time to dive in and start building! Let us know what you think and how Torpedo can help your future projects.
↗ Original-Artikel auf dev.to lesenVollständiger Original-BerichtAusführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
SOCIAL SHARE CARD GENERATOR