api-gen

command module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2024 License: MIT Imports: 3 Imported by: 0

README

API-GEN

API-GEN is a tool that generates APIs and handlers based on a configuration file and predefined type structures. It simplifies the process of creating APIs by automatically generating the necessary code.

Getting Started

  1. To install API-Gen, use the following command::
go install github.com/ydssx/api-gen@latest
  1. Define the type structures in the typeFile:

In the typeFile, you need to define the type structures for your APIs. Each structure should be annotated with metadata comments that specify the group, authentication requirement, handler, and router details.

Example:

// @group apiv22
// @auth false
// @handler register
// @router /register [get]
type (
	RegisterReq struct {
		Name     string `form:"name" binding:"required"` //用户名
		Password string `form:"password"`
	}

	RegisterResp struct {
		User string `json:"user"`
	}
)
  1. Configure the config.yaml file:

The config.yaml file contains the configuration settings for API-GEN. You can specify the API paths, type file path, logic file, handler file, and router file.

Example:

apiPath:
  - /register
  - /login

typeFile: example/types/example.go

logic:
  file: example/logic/logic.go
  receiver: "*UserLogic"

handler:
  file: example/handler/handler.go

router:
  file: example/router/router.go
  groupFunc: UserRouter

  1. Run the API-GEN tool:

To generate the APIs and handlers based on the configuration and type structures, run the following command:

api-gen -c config.yaml

This will read the config.yaml file, parse the type structures from the typeFile, generate logic functions, handler functions, and add routers accordingly.

Configuration Options

  • apiPath: A list of API paths where the generated APIs will be registered.
  • typeFile: The path to the file that contains the type structures for the APIs.
  • logic.file: The file where the logic functions will be generated.
  • logic.receiver: The receiver name for the logic functions.
  • handler.file: The file where the handler functions will be generated.
  • router.file: The file where the router functions will be generated.
  • router.groupFunc: The name of the group function in the router file.

Generated Files

API-GEN generates the following files based on the configuration:

  • Logic file: Contains the logic functions for the APIs.
  • Handler file: Contains the handler functions for the APIs.
  • Router file: Contains the router functions that register the APIs.

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on the API-GEN repository.

License

This project is licensed under the MIT License.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
docs
Package docs GENERATED BY SWAG; DO NOT EDIT This file was generated by swaggo/swag
Package docs GENERATED BY SWAG; DO NOT EDIT This file was generated by swaggo/swag

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL