Giter VIP home page Giter VIP logo

goctl-gogen's Introduction

goctl-gogen

goctl plugin to parse api definitions and generate api files with handler comments retained.

Installation

go install github.com/wqyjh/[email protected]

Example

See full example at https://github.com/WqyJh/zero-template.

Usage

Inspired by zeromicro/go-zero#2464.

Define swagger document for handler in .api files.

@server(
	group: user
    prefix: /api/v1/user
    jwt: JwtAuth
)
service api {
    // UserSelf godoc
    // @Summary      查询用户信息
    // @Description  查询用户信息
    // @Tags         user
    // @Security     ApiKeyAuth
    // @Accept       json
    // @Produce      json
    // @Param        client   header      string  true  "当前设备类型: android/ios"
    // @Success      200    {object}   types.DataResponse{data=types.UserSelfReply}
    // @Router       /api/v1/user/self [get]
    @handler UserSelf
    get /self (UserSelfReq) returns (UserSelfReply)
}

Generate api using goctl api plugin.

goctl api plugin -plugin goctl-gogen="--home ${CWD}/../template" -api def/main.api -dir ./ -style goZero

The document defined would be rendered on handler. And if you update the api file, and regenerate the handler, handler will also be updated.

// UserSelf godoc
//	@Summary		查询用户信息
//	@Description	查询用户信息
//	@Tags			user
//	@Security		ApiKeyAuth
//	@Accept			json
//	@Produce		json
//	@Param			client		header		string	true	"当前设备类型: android/ios"
//	@Success		200			{object}	types.DataResponse{data=types.UserSelfReply}
//	@Router			/api/v1/user/self [get]
func UserSelfHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.UserSelfReq
		if err := httpz.Parse(r, &req); err != nil {
			httpz.ErrorLog(r.Context(), w, err)
			return
		}

		logc.Infow(r.Context(), "UserSelfHandler", logc.Field("req", &req))

		l := user.NewUserSelfLogic(r.Context(), svcCtx)
		resp, err := l.UserSelf(&req)
		if err != nil {
			httpz.Error(w, err)
			logc.Infow(r.Context(), "UserSelfHandler error", logc.Field("error", err))
		} else {
			httpz.OkJson(w, resp)
			logc.Infow(r.Context(), "UserSelfHandler success", logc.Field("resp", resp))
		}
	}
}

Then you can generate swagger document by swaggo.

goctl-gogen's People

Contributors

wqyjh avatar

Watchers

 avatar

Forkers

mmc-98

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.