msgonest-cli is a command-line tool designed to eliminate boilerplate and repetitive code when generating the essential files for a microservices project. It streamlines the process by automatically creating GRPC proto files, API components (controller, service, and DTO), and Go code for the API gateway, based on a single JSON configuration file provided by the user.
- GRPC Proto Files Gener 8000 ation: Automatically generate GRPC proto files to define service interfaces and message types.
- API Generation: Create API components, including controllers, services, and DTOs, to handle HTTP requests and responses.
- API Gateway Code Generation in Go: Generate Go code for the API gateway to manage and route incoming API requests.
To use msgonest-cli, define a JSON configuration file that specifies the details of the microservice and the desired components. Here is an example JSON configuration file with detailed explanations for each parameter and section:
{
"name": "titok",
"description": "tiktokurls",
"microservice": "auth",
"functions": [
{
"name": "get_tiktok_auth_url2",
"enpoint": "/get_tiktok_auth_url2",
"method": "GET",
"needs_middleware": false,
"fieldsRequest": [
{
"name": "user_id",
"type": "string",
// int32, float,double,string
"nullable": true,
"source": "query",
"isUUID": true
}
],
"fieldsResponse": [
{
"name": "data",
"type": "string",
"nullable": false,
"source": "response"
}
]
},
{
"name": "tiktok_auth_callback2",
"enpoint": "/tiktok_auth_callback2",
"method": "GET",
"needs_middleware": true,
"fieldsRequest": [
{
"name": "code",
"type": "string",
"nullable": true,
"source": "body"
},
{
"name": "state",
"type": "string",
"nullable": false,
"source": "query"
}
],
"fieldsResponse": [
{
"name": "data",
"type": "object",
"nullable": false,
"source": "response",
"children": [
{
"name": "token",
"type": "string",
"nullable": false,
"source": "response"
},
{
"name": "tokenRefresh",
"type": "string",
"nullable": false,
"source": "response"
},
{
"name": "gender",
"type": "enum",
"values": [
"Male",
"Female",
"Non_BINARY",
"Not_APPLICABLE"
],
"nullable": false,
"source": "response"
}
]
}
]
}
]
}
- name: The name of the project or service. Example: "titok"
- description: A brief description of the project or service. Example: "tiktokurls"
- microservice: The name of the microservice. Example: "auth"
- functions: An array of function definitions that the microservice should handle.
- name: The name of the function. Example: "get_tiktok_auth_url2"
- endpoint: The API endpoint for the function. Example: "/get_tiktok_auth_url2"
- method: The HTTP method for the API call (e.g., GET, POST). Example: "GET"
- needs_middleware: Boolean flag to indicate whether middleware is required for this function. Example: false
-
fieldsRequest: An array of fields expected in the API request.
- name: The name of the field. Example: "user_id"
- type: The data type of the field (e.g., string, object). Example: "string"
- nullable: Boolean flag to indicate if the field is nullable. Example: true
- source: The source of the field in the request (e.g., query, body). Example: "query"
- isUUID: Boolean flag to indicate if the field should be a UUID. Example: true
- Response Fields
-
fieldsResponse: An array of fields expected in the API response.
- name: The name of the field. Example: "data"
- type: The data type of the field (e.g., string, object). Example: "string"
- nullable: Boolean flag to indicate if the field is nullable. Example: false
- source: The source of the field in the response. Example: "response"
- children: (Optional) An array of child fields if the field is an object.
Create a JSON Configuration File: Define the structure of your microservice and the necessary components using the JSON format described above.
Run msgonest-cli: Execute the command-line tool, passing the JSON configuration file as an argument.
Generate Code: The tool will generate the specified components based on the provided configuration. By using msgonest-cli, developers can significantly reduce the time spent on repetitive coding tasks and focus more on the unique aspects of their microservices projects.
git clone https://github.com/leganux/msgonest-cli
Access to the repository
cd msgonest-cli
Install dependencies
npm i
link up the project to CLI, this create a simlink to execute the code as bash command
npm link
npm install -g msgonest_cli
Run config command to save the default project paths
msgonestcli config
command request you for absolute paths for root projects of GRPC,API and Gateway. This configuration will be saved as JSON file on
~/.msgonest_cli/config.js
Example config file
{
"base_grpc_folder": "/Users/leganux/Documents/GitHub/grpc/grpc",
"base_API_folder": "/Users/leganux/Documents/GitHub/api",
"base_API_GATEWAY_folder": "/Users/leganux/Documents/GitHub/api-gateway"
}
Generates all code based on json template for GRPC,API and GATEWAY
msgonestcli full -p /absolute/path/of/json/template.json
Generates all code based on json template for GRPC
msgonestcli grpc -p /absolute/path/of/json/template.json
Generates all code based on json template for GATEWAY
msgonestcli gateway -p /absolute/path/of/json/template.json
Generates all code based on json template for API
msgonestcli api -p /absolute/path/of/json/template.json
Generates automatically the code for create a new microservice
msgonestcli microservice -n < ms_name > -p < [api,gateway,grpc,full] >
By default this command creates a microservice 8000 in full mode for all projects
Whit next functuin you can generate code for GRPC,Gateway and API for a completly CRUD
{
"ops": {
// which operations you will want to create
"create": true,
"getOne": true,
"list": true,
"delete": true,
"update": true
},
"name": "EventTypes",
// The CRUD name -- preserve "PascalCase"
"description": "The types for events",
"microservice": "events",
// In wich microservice will be added
"fields": [
// Wich fields you want to create, by default id, and dates will create automatically
{
"name": "name",
"description": "Its the name of the type of event ",
"kind": "string",
"mandatory": true
},
{
"name": "description",
"description": "Its the description of the type of event ",
"kind": "string",
"mandatory": false
},
{
"name": "enabled",
"description": "If the row is active or not",
"kind": "boolean",
"mandatory": false,
"default": true
},
{
"name": "order",
"description": "How be ordered in table or view",
"kind": "int",
"mandatory": false
},
{
"name": "other",
"description": "A custom float value",
"kind": "float",
"mandatory": false,
"default": 0.0
}
],
"seed": [
// optional uf you wanna create seed to preregister items
{
"name": "Music",
"description": "Is a music event type",
"enabled": true,
"order": 1,
"other": 1.23
},
{
"name": "Opera",
"description": "Is a opera event type",
"enabled": true,
"order": 2,
"other": 2.22
}
]
}
msgonestcli crud -p api -j /Users/leganux/Documents/GitHub/msgonest-cli/crud.example.json
- -p Mandatory (In wih project will be executed [full,api,grpc,gateway])
- -j Mandatory The absolute full path of json definition file from the step behind
Once you have your Json file, Now its time to execute the command
At the end you must to execute your code indentation and review and import dependencias
-
The msgonest-cli covers approximately 95% of the necessary code for a microservices project. However, it is essential to manually validate that the code generation was executed successfully.
-
We are currently working on improving indentation handling. For now, users will need to manually adjust the code formatting after running the CLI to ensure clean and structured code.
msgonest-CLI is another project of Angel Erick Cruz all rights reserved This project is distributed under the MIT license.
Special thanks to all developers that work for his contribution to this development.
The project was made with