A Golang package designed to generate high-performance backend services using fasthttp. It follows the Clean Architecture pattern, automatically generating the domain, infrastructure, handlers, and routing layers for each service, streamlining the development process.
- Automatically generates backend code structure for Go projects.
- Creates service folders with pre-configured Go modules.
- Simplifies repetitive tasks in setting up backend service.
project/
├── sql-migrations/ # Migration scripts for PostgreSQL
│ ├── seed/
│ │ └── init-seed.sql
│ ├── sql/
│ │ └── init.sql
├── main.go # Application entry point
├── go.mod
├── src/ # Source code folder
│ ├── bootstrap/ # Central module initialization
│ │ └── bootstrap.go
│ ├── common/ # Common utility functions
│ │ ├── constants.go # UUID generator
│ ├── core/ # Contains all the layers
│ │ ├── domain/
│ │ │ └── aggregates/
│ │ │ ├── user.go # Business logic for User (Domain model)
│ │ ├── application/
│ │ │ └── service/
│ │ │ ├── user_service.go # Business logic for User
│ │ │ └── workers/
│ │ │ ├── worker.go # Background worker
│ │ ├── infrastructure/
│ │ │ ├── db/
│ │ │ │ ├── db.go # General Database connection
│ │ │ │ └── sql-db.go # SQL-DB connection logic
│ │ │ ├── entity/
│ │ │ │ ├── user.go # User entity
│ │ │ ├── repository/
│ │ │ │ ├── user_repository_impl.go # UserRepository implementation
│ │ ├── interface/
│ │ │ └── dto/
│ │ │ ├── user_dto.go # Request and Response DTO for User
│ │ │ └── handlers/
│ │ │ ├── user_handler.go # HTTP handler for User
│ ├── helpers/ # Additional utility functions
│ │ ├── string_helpers.go # String manipulation helpers
-
Domain Layer: Contains the core business models and domain logic. It defines entities and their relationships, encapsulating the business rules.
-
Application Layer: Holds the business logic and use cases. It coordinates the flow of data between the domain and infrastructure layers, processing requests and responses.
-
Infrastructure Layer: Manages data access and external integrations (e.g., database interactions, third-party APIs). It provides concrete implementations for the repository interfaces defined in the domain layer.
-
Interface Layer: Contains HTTP handlers for routing incoming requests and managing responses. It may also include Data Transfer Objects (DTOs) for structured data interchange between client and server.
- Go 1.XX or higher
- Dependencies specified in
go.mod
Run the following command to install gStructify:
go install github.com/nanda03dev/gStructify@latest
Create and navigate to your project directory:
mkdir my-microservice
cd my-microservice
Note: In this example, the project is named
my-microservice
. You can define your own service name.
Initialize a new Go module for your project:
go mod init my-microservice
Run gStructify
to automatically create your service structure for a specific entity:
gStructify -entity=user
By default, gStructify will generate the entire structure for one entity with only an id
field.
For more advanced setups, you can define multiple entities and their fields in a configuration file, as explained in Step 5.
To customize the generated code, create a gStructify.config.json
file in your project directory. Define your entities and their fields as shown below:
{
"entities": [
{
"entity_name": "user",
"fields": [
{ "field_name": "id", "type": "string" },
{ "field_name": "email", "type": "string" }
]
},
{
"entity_name": "order",
"fields": [
{ "field_name": "id", "type": "string" },
{ "field_name": "user_id", "type": "string" },
{ "field_name": "order_amount", "type": "int" }
]
}
]
}
- The fields in the configuration file should be defined using
snake_case
. - Both keys and values in the configuration file are case-sensitive.
- You can directly use Go data types (e.g.,
string
,int
,float64
, etc.) in the field definitions.
Once you’ve added fields in the configuration file, run gStructify
without any additional arguments:
gStructify
This will:
- Generate a structured set of files for the
user
andorder
entities. - Populate the necessary Go files for your application, ready for development.
If you have a URL for the SQL database, you can add it in the .env
file under the SQL_DB_URI
variable.
To run the database locally, follow these steps:
-
Start the PostgreSQL database:
make run-sql-db
This command will start the PostgreSQL database in detached mode.
-
Run the application locally:
make run-dev
This command will start the application on your local machine.
Once the application is running, you can start executing the generated CRUD APIs for your entities.
Now you're ready to build efficient and scalable Go backend services with gStructify. 🚀
Happy coding with gStructify
!
Contributions are welcome! Please open an issue or submit a pull request for any improvements.
This project is licensed under the MIT License. See the LICENSE file for details.