API Generator based on comments
 
 
 
Go to file
Paulo Simão 34fe5c9740 Improving 2021-11-28 19:09:25 -03:00
lib Improving 2021-11-28 19:08:32 -03:00
test Improving 2021-11-28 19:08:32 -03:00
.gitignore 1st ver 2020-10-17 19:31:19 -03:00
LICENSE Templates 2021-04-02 11:40:28 -03:00
README.md Added README.md 2021-09-06 08:27:54 -03:00
go.mod Improving 2021-11-28 19:09:25 -03:00
go.sum Simplified 2021-10-03 11:39:42 -03:00
main.go Improving 2021-11-28 19:08:32 -03:00

README.md

API Gen

API Generator

API Generator works on top of Go code and comments. Instead of generating openapi and from there deriving your code, you can code 1st, and reduce the amount of effort at refactoring / improving your code in case of changes or improvements.

How it works:

Create a GO package with the business logic to be exposed:

/*
@API
@PATH: /someapi
@PERM: ASD
@VERB: POST
*/
func SomeAPI(ctx context.Context, s string) (out string, err error) {
	print("Got:" + s)
	out = time.Now().String() + " - Hey Ya!"
	return
}

Note the follwoing on the above API:

1 - Method has a context as 1st parameter
2 - Method has a string as 2nd parameter
3 - Method returns a string and an error

1st Rule of API Methods:

1 - Always recive context and an interface as parameters in
2 - Always return interface and error

Also note the comments just above the Method, this is used as meta programming, binding the method do the API impl.


/*@API*/
type AStr struct {
	Country       string
	City          string
	HouseNumber   int64
	IsCondo       bool
	SomeWeirdTest string `json:"SUPERCALIFRAGILISPEALIDOUX"`
	Recursive     map[string]AStr
	Arrofpstr     []string `json:"arrofpstr,omitempty"`
	When          time.Time
	Some          crypto.Decrypter
}

For datastructures you may need to use as params in or out - you may use similar approach:

Mark them with the comment @API and they will be also exported.

Once your code is ready, call apigen fromt the command line with the following alternatives:

1 - goserver: Provides API Server using standard lib router
2 - gin: Provives API Server using Gin
3 - gocli: Creates Go Client for the API
4 - ts: Creates typescript client for the API
5 - pycli: Creates python client for the API

See apigen -h for further details