Other features

A good thing about kit is that it can be used while you are developing and not only for one time project creation.

Generate new service endpoints

A common thing that happens is that you want to add a new endpoint to your service that you did not think of in the beginning, kit makes it very easy to do that you just add the endpoint definition inside the service interface and rerun the generate command

// TodoService describes the service.

type TodoService interface {

Get(ctx context.Context) (t []io.Todo, error error)

Add(ctx context.Context, todo io.Todo) (t io.Todo, error error)

SetComplete(ctx context.Context, id string) (error error)

RemoveComplete(ctx context.Context, id string) (error error)

Delete(ctx context.Context, id string) (error error)

#Example we want to add a get by id method.

GetById(ctx context.Context, id string) (t io.Todo, error error) }

Then run:

kit g s todo --gorilla -w

kit will then create everything that is missing for the new endpoint and recreate the _gen files, this will not override any change you made in the non _gen files.

Add new middleware

kit also supports generating new service middleware, the generator will create the boilerplate code for you but because kit can not possible know what parameters your middleware needs you will have to add them manually to your middleware and then add the middleware to your service.

Lets say we want to add an auth middleware to our service.

kit g m auth -s todo

this will generate the boilerplate code inside todo/service/middleware.go :

type authMiddleware struct {

next TodoService

}



// AuthMiddleware returns a TodoService Middleware.

func AuthMiddleware() Middleware {

return func(next TodoService) TodoService {

return &authMiddleware{next}

}



}

func (a authMiddleware) Get(ctx context.Context) (t []io.Todo, error error) {

// Implement your middleware logic here



return a.next.Get(ctx)

}

func (a authMiddleware) Add(ctx context.Context, todo io.Todo) (t io.Todo, error error) {

// Implement your middleware logic here



return a.next.Add(ctx, todo)

}

func (a authMiddleware) SetComplete(ctx context.Context, id string) (error error) {

// Implement your middleware logic here



return a.next.SetComplete(ctx, id)

}

func (a authMiddleware) RemoveComplete(ctx context.Context, id string) (error error) {

// Implement your middleware logic here



return a.next.RemoveComplete(ctx, id)

}

func (a authMiddleware) Delete(ctx context.Context, id string) (error error) {

// Implement your middleware logic here



return a.next.Delete(ctx, id)

}

If you want to add an endpoint middleware just add the -e flag

kit g m auth -s todo -e

and that will generate the boilerplate code inside todo/endpoints/middleware.go

// AuthMiddleware returns an endpoint middleware

func AuthMiddleware() endpoint.Middleware {

return func(next endpoint.Endpoint) endpoint.Endpoint {

return func(ctx context.Context, request interface{}) (response interface{}, err error) {

// Add your middleware logic here

return next(ctx, request)

}

}

}

Now to add your middleware to your service you will need to edit todo/cmd/service/service.go#getServiceMiddleware and todo/cmd/service/service.go#getEndpointMiddleware functions.

func getServiceMiddleware(logger log.Logger) (mw []service.Middleware) {

mw = []service.Middleware{}

mw = addDefaultServiceMiddleware(logger, mw)

// My auth middleware

mw = append(mw, service.AuthMiddleware())

return

}

func getEndpointMiddleware(logger log.Logger) (mw map[string][]endpoint1.Middleware) {

mw = map[string][]endpoint1.Middleware{}

duration := prometheus.NewSummaryFrom(prometheus1.SummaryOpts{

Help: "Request duration in seconds.",

Name: "request_duration_seconds",

Namespace: "example",

Subsystem: "todo",

}, []string{"method", "success"})

addDefaultEndpointMiddleware(logger, duration, mw)

// My auth middleware

addEndpointMiddlewareToAllMethods(mw,endpoint.AuthMiddleware())

return

}

for adding the endpoint middleware I am using a small helper function inside todo/cmd/service/service_gen.go that will add the middleware to all the endpoints if you want to add the middleware to only specific endpoints you can do that by:

func getEndpointMiddleware(logger log.Logger) (mw map[string][]endpoint1.Middleware) {

mw = map[string][]endpoint1.Middleware{}

duration := prometheus.NewSummaryFrom(prometheus1.SummaryOpts{

Help: "Request duration in seconds.",

Name: "request_duration_seconds",

Namespace: "example",

Subsystem: "todo",

}, []string{"method", "success"})

addDefaultEndpointMiddleware(logger, duration, mw)

// My auth middleware only for the "Get" method

mw["Get"] = append(mw["Get"], endpoint.AuthMiddleware())

return

}

Add GRPC transport

If you want to add the GRPC transport to your service you just rerun the generate command using -t grpc .

kit g s todo -w -t grpc

Since GRPC has some extra stuff you need to take care of kit will create your basic GRPC setup and will give you further instructions on how to complete the generation.

You can find all the source code of the todo service in GitHub kujtimiihoxha/todo-gokit-demo

And you can find out more about GoKit-CLI here kujtimiihoxha/kit