Generating the files
Gohandlers can generate 2 different Go files. Files are for processing requests and making requests. The former is called helper file and the latter is called client file.
Generating helper files
Helpers are methods you call inside handlers and for route registration. To produce the helpers file you need to run the helpers subcommand.
cd <handlers directory>
gohandlers helpers
That’s all. If everything goes right, you’ll see a gh.go
file at the same directory. Inside the helpers file, there should be your listers, request builders, request parsers, request validators, response builders and response writers. More on that is in the Listing handlers and the Processing requests sections.
func (pe *Pets) ListHandlers() map[string]gohandlers.HandlerInfo
func (bq CreateRequest) Build(host string) (*http.Request, error)
func (bq *CreateRequest) Parse(rq *http.Request) error
func (bq CreateRequest) Validate() (issues map[string]any)
func (bs CreateResponse) Write(w http.ResponseWriter) error
func (bs *CreateResponse) Parse(rs *http.Response) error
// ...
Generating client files
Gohandlers provides Client, Mock and Interface type declarations which allow you to call your services as well as unit test the consumer service methods in isolation. Client file requires the helpers file, but it is optional. For services that is not consumed by Go services, such as APIs intended to be called from frontend, client file is not needed at all. To generate the client file pick the input and output folders. The client file might be in different folder, named as client
and also has that as the package name. Because of that it also needs to know how to import the helpers file.
cd <service root>
gohandlers client \
-dir endpoints \
-import <import path of handlers file package> \
-out client/client.go \
-pkg <service name>
This should produce a file with content below. While Mock
methods are almost empty, Client
methods are functional. More on that is in the Making requests section.
package <service name>
import (
"fmt"
"net/http"
"<import path of handlers file package>"
)
type Interface interface {
Create(*pets.CreateRequest) (*pets.CreateResponse, error)
Delete(*pets.DeleteRequest) (*http.Response, error)
Get(*pets.GetRequest) (*pets.GetResponse, error)
List(*pets.ListRequest) (*pets.ListResponse, error)
}
type Mock struct {
CreateFunc func(*pets.CreateRequest) (*pets.CreateResponse, error)
DeleteFunc func(*pets.DeleteRequest) (*http.Response, error)
GetFunc func(*pets.GetRequest) (*pets.GetResponse, error)
ListFunc func(*pets.ListRequest) (*pets.ListResponse, error)
}
func (m *Mock) Create(bq *pets.CreateRequest) (*pets.CreateResponse, error)
func (m *Mock) Delete(bq *pets.DeleteRequest) (*http.Response, error)
func (m *Mock) Get(bq *pets.GetRequest) (*pets.GetResponse, error)
func (m *Mock) List(bq *pets.ListRequest) (*pets.ListResponse, error)
type Pool interface {}
type Client struct {}
func NewClient(p Pool) *Client
func (c *Client) Create(bq *pets.CreateRequest) (*pets.CreateResponse, error)
func (c *Client) Delete(bq *pets.DeleteRequest) (*http.Response, error)
func (c *Client) Get(bq *pets.GetRequest) (*pets.GetResponse, error)
func (c *Client) List(bq *pets.ListRequest) (*pets.ListResponse, error)