api_gen

module
v2.5.5 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2021 License: MIT

README

api_gen

What's api_gen?

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

Prerequisite

Go 1.16 or newer is required to use mock servers.

Installation

from Releases

Download binaries from Releases.

go install

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

Basic Usage

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));

For more details

server-side

ENGLISH/日本語

client-side

ENGLISH/日本語

License

  • Under the MIT License
  • Copyright (c) 2020-2021 go-generalize

Directories

Path Synopsis
cmd
api_gen
Package main is the entrypoint for api_gen cmd Package main is the entrypoint for api_gen cmd Package main is the entrypoint for api_gen cmd Package main is the entrypoint for api_gen cmd
Package main is the entrypoint for api_gen cmd Package main is the entrypoint for api_gen cmd Package main is the entrypoint for api_gen cmd Package main is the entrypoint for api_gen cmd
api_gen/client
Package clientcmd has commands for "api_gen client" Package clientcmd has commands for "api_gen client" Package clientcmd has commands for "api_gen client"
Package clientcmd has commands for "api_gen client" Package clientcmd has commands for "api_gen client" Package clientcmd has commands for "api_gen client"
Package common ...
Package common ...
pkg
agerrors
Package agerrors has error types for api_gen Package agerrors has error types for api_gen
Package agerrors has error types for api_gen Package agerrors has error types for api_gen
client/dart
Package clientdart generates client-side Dart library Package clientdart generates client-side Dart library Package clientdart generates client-side Dart library
Package clientdart generates client-side Dart library Package clientdart generates client-side Dart library Package clientdart generates client-side Dart library
client/go
Package clientgo generates client-side Go library Package clientgo generates client-side Go library
Package clientgo generates client-side Go library Package clientgo generates client-side Go library
client/typescript
Package clientts generates client-side TypeScript library Package clientts generates client-side TypeScript library Package clientts generates client-side TypeScript library
Package clientts generates client-side TypeScript library Package clientts generates client-side TypeScript library Package clientts generates client-side TypeScript library
go2json
Package go2json ...
Package go2json ...
parser
Package parser parses API definitions in Go Package parser parses API definitions in Go Package parser parses API definitions in Go
Package parser parses API definitions in Go Package parser parses API definitions in Go Package parser parses API definitions in Go
server
Package server generates server-side library Package server generates server-side library Package server generates server-side library Package server generates server-side library Package server generates server-side library Package server generates server-side library Package server generates server-side library Package server generates server-side library
Package server generates server-side library Package server generates server-side library Package server generates server-side library Package server generates server-side library Package server generates server-side library Package server generates server-side library Package server generates server-side library Package server generates server-side library
samples
empty_root/api/foo/bar
Package foo ...
Package foo ...
empty_root/clients/go
THIS FILE IS A GENERATED CODE.
THIS FILE IS A GENERATED CODE.
empty_root/docs
Package docs GENERATED BY THE COMMAND ABOVE; DO NOT EDIT This file was generated by swaggo/swag
Package docs GENERATED BY THE COMMAND ABOVE; DO NOT EDIT This file was generated by swaggo/swag
empty_root/server
Code generated by api_gen.
Code generated by api_gen.
empty_root/server/controller/foo/bar
Package controller handles requests and returns responses.
Package controller handles requests and returns responses.
empty_root/server/mock
Code generated by api_gen.
Code generated by api_gen.
empty_root/server/mock/controller/foo/bar
Code generated by api_gen.
Code generated by api_gen.
empty_root/server/pkg/apierror
Code generated by api_gen.
Code generated by api_gen.
empty_root/server/props
Package props is a scaffold file for props of controllers
Package props is a scaffold file for props of controllers
standard/api
Package sample ...
Package sample ...
standard/api/service
Package service ...
Package service ...
standard/api/service/groups
Package groups ...
Package groups ...
Package common ...
Package static ...
standard/api/service/table
Package table ...
Package table ...
standard/api/service/user
Package user ...
Package user ...
standard/api/service/user2
Package user2 ...
Package user2 ...
standard/clients/go
THIS FILE IS A GENERATED CODE.
THIS FILE IS A GENERATED CODE.
standard/docs
Package docs GENERATED BY THE COMMAND ABOVE; DO NOT EDIT This file was generated by swaggo/swag
Package docs GENERATED BY THE COMMAND ABOVE; DO NOT EDIT This file was generated by swaggo/swag
standard/server
Code generated by api_gen.
Code generated by api_gen.
standard/server/controller
Package controller handles requests and returns responses.
Package controller handles requests and returns responses.
standard/server/controller/service
Package controller handles requests and returns responses.
Package controller handles requests and returns responses.
standard/server/controller/service/groups
Package controller handles requests and returns responses.
Package controller handles requests and returns responses.
standard/server/controller/service/static_page
Package controller handles requests and returns responses.
Package controller handles requests and returns responses.
standard/server/controller/service/user
Package controller handles requests and returns responses.
Package controller handles requests and returns responses.
standard/server/controller/service/user2
Package controller handles requests and returns responses.
Package controller handles requests and returns responses.
standard/server/controller/service/user2/_userID
Package controller handles requests and returns responses.
Package controller handles requests and returns responses.
standard/server/controller/service/user2/_userID/_JobID
Package controller handles requests and returns responses.
Package controller handles requests and returns responses.
standard/server/mock
Code generated by api_gen.
Code generated by api_gen.
standard/server/mock/controller
Code generated by api_gen.
Code generated by api_gen.
standard/server/mock/controller/service
Code generated by api_gen.
Code generated by api_gen.
Code generated by api_gen.
Code generated by api_gen.
Code generated by api_gen.
Code generated by api_gen.
Code generated by api_gen.
Code generated by api_gen.
standard/server/pkg/apierror
Code generated by api_gen.
Code generated by api_gen.
standard/server/props
Package props is a scaffold file for props of controllers
Package props is a scaffold file for props of controllers

Jump to

Keyboard shortcuts

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