api_gen accelerates the development of API servers.
It generates a boilerplate for your API server in Go and clients in Go, TypeScript and so on.
Server: Go
Client: Go, TypeScript, Dart
Go 1.16 or newer is required to use mock servers.
Download binaries from Releases.
Go 1.17 or newer is required.
$ go install github.com/go-generalize/api_gen/v2/cmd/api_gen@latest # latest or vX.Y.ZPrepare API definitions in Go.
The path of directories are mapped to the path of URLs.
Define types for both of requests and responses.
All types must consist of three parts: method, endpoint name, request or response.
The below example is types for POST /foo/bar/update_user.
Requests are bound with the Binder in echo.
Please refer to Binding.
Responses are encoded in JSON.
// ./interfaces/foo/bar/hoge.gopackage package_name_that_you_want
typePostUpdateUserRequeststruct {
IDstring`json:"id"`
}
typePostUpdateUserResponsestruct {
Resultint`json:"result"`
}Run the below command to generate controllers.
$ api_gen server -o ./server ./interfacesControllers are generated into ./interfaces/controller/foo/bar/post_update_user.go.
Then, set up echo server.
// main.gopackage main
import (
controller "path/to/generated/server""github.com/labstack/echo/v4"
)
funcmain() {
e:=echo.New()
controller.NewControllers(nil, e)
panic(e.Start(":8080"))
}Run the below command to generate clients in TypeScript.
$ api_gen client ts -o ./client/ts ./interfacesAPI clients are generated into ./client/ts/api_client.ts.
import{APIClient}from'path/to/api_client.ts';constclient=newAPIClient('very_secure_token',{},'https://example.com');client.foo.bar.postUpdateUser({id: 'id'}).then(res=>console.log(res));- Under the MIT License
- Copyright (c) 2020-2021 go-generalize