Go language develops web projects based on Go kit

01

introduce

We introduced the Go language toolkit Go kit in the previous article ” Golang Microservice Toolkit Go kit “. In this article, we introduce how to develop web projects based on Go kit. After reading the previous article, we already know that Go kit services are divided into three layers, namely transport, endpoint and service.

Among them, the service layer defines the business interface and implements the interface methods.

The endpoint layer receives the request parameters and returns the response result. It should be noted that in the endpoint layer, endpoint.Endpoint is constructed for the business interface method.

Because endpoint.Endpoint is a function type, it encapsulates a layer, which is convenient for us to use the endpoint decorator to add functions to endpoint.Endpoint, such as logging, current limiting, load balancing , link tracking, etc.

The endpoint layer uses the constructed endpoint.Endpoint to call the methods of the service layer interface to process requests.

The transport layer provides an external calling interface (http or rpc).

02

Develop web projects based on Go kit

We develop a user center project based on Go kit, which mainly includes the functions of registration and login.

Go language develops

The directory structure is as follows:

.
├── endpoint # 接收请求,构建 endpoint.Endpoint 调用 service 层的接口方法,处理请求参数,返回响应结果给 transport 层
│   └── user.go
├── go.mod
├── go.sum
├── main.go
├── service # 定义业务接口并实现接口方法
│   └── user.go
└── transport # 对外提供调用接口(http 或 rpc)
    └── http.go

The service package defines the interface of the service (user service) and implements the interface methods.

type IUser interface {
 Register(ctx context.Context, req *RegisterRequest) (*User, error)
 Login(ctx context.Context, email, password string) (*User, error)
}

The endpoint package constructs endpoint.Endpoint for the interface method, converts the request parameters into parameters that can be processed by the interface method, and encapsulates the returned response result into the corresponding response structure and returns it to the transport package.

...
type RegisterRequest struct {
 UserName string
 Email    string
 Password string
}
type RegisterResponse struct {
 User *service.User
}
func MakeRegisterEndpoint(iUser service.IUser) endpoint.Endpoint {
 return func(ctx context.Context, request interface{}) (response interface{}, err error) {
  req := request.(*RegisterRequest)
  user, err := iUser.Register(ctx, &service.RegisterRequest{
   UserName: req.UserName,
   Email:    req.Email,
   Password: req.Password,
  })
  return &RegisterResponse{User: user}, err
 }
}

The transport package provides the constructed endpoint.Endpoint to the caller.

...
func NewHttpHandler(ctx context.Context, endpoints *endpoint.Endpoints) http.Handler {
 r := http.NewServeMux()
 r.Handle("/register", kitHttp.NewServer(endpoints.RegisterEndpoint, decRegisterRequest, encResponse))
 return r
}
...

In the main function, create the service, endpoint and transport, and start the web server .

func main() {
 ctx := context.Background()
 userService := service.NewUserService()
 endpoints := &endpoint.Endpoints{
  RegisterEndpoint: endpoint.MakeRegisterEndpoint(userService),
  LoginEndpoint:    endpoint.MakeLoginEndpoint(userService),
 }
 r := transport.NewHttpHandler(ctx, endpoints)
 err := http.ListenAndServe(":8080", r)
 if err != nil {
  log.Fatal(err)
  return
 }
}

Start with the go run command and call the http interface with cURL.

03

Summarize

In this article, we introduce how to develop a web project based on Go kit through a simple user center project. In order to facilitate readers and friends to understand the code, no other components are used in the project code. Interested readers and friends can try to improve it, such as adding code for operating the database .

Leave a Reply

Your email address will not be published.

en_USEnglish