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.Z
Prepare 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.go
package package_name_that_you_want
type PostUpdateUserRequest struct {
ID string `json:"id"`
}
type PostUpdateUserResponse struct {
Result int `json:"result"`
}
Run the below command to generate controllers.
$ api_gen server -o ./server ./interfaces
Controllers are generated into ./interfaces/controller/foo/bar/post_update_user.go
.
Then, set up echo server.
// main.go
package main
import (
controller "path/to/generated/server"
"github.com/labstack/echo/v4"
)
func main() {
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 ./interfaces
API clients are generated into ./client/ts/api_client.ts
.
import { APIClient } from 'path/to/api_client.ts';
const client = new APIClient('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