自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Go 語言基于 Go kit 開發(fā) Web 項(xiàng)目

開發(fā) 項(xiàng)目管理
endpoint 層接收請(qǐng)求參數(shù)并返回響應(yīng)結(jié)果,需要注意的是,在 endpoint 層,給業(yè)務(wù)接口方法構(gòu)建 endpoint.Endpoint。

1.介紹

我們?cè)谏弦黄恼隆??Golang 微服務(wù)工具包 Go kit??」介紹了 Go 語言工具包 Go kit,本文我們介紹怎么基于 Go kit 開發(fā) Web 項(xiàng)目。在閱讀上篇文章后,我們已經(jīng)知道 Go kit 服務(wù)分為三層,分別是 transport、endpoint 和 service。

其中,service 層定義業(yè)務(wù)接口并實(shí)現(xiàn)接口方法。

endpoint 層接收請(qǐng)求參數(shù)并返回響應(yīng)結(jié)果,需要注意的是,在 endpoint 層,給業(yè)務(wù)接口方法構(gòu)建 endpoint.Endpoint。

因?yàn)?endpoint.Endpoint 是函數(shù)類型,封裝一層,方便我們使用 endpoint 裝飾器,給 endpoint.Endpoint 添加功能,例如日志、限流、負(fù)載均衡、鏈路追蹤等。

endpoint 層使用構(gòu)建的 endpoint.Endpoint 調(diào)用 service 層接口的方法處理請(qǐng)求。

transport 層對(duì)外提供調(diào)用接口(http 或 rpc)。

2.基于 Go kit 開發(fā) Web 項(xiàng)目

我們基于 Go kit 開發(fā)一個(gè)用戶中心項(xiàng)目,主要包含注冊(cè)和登錄的功能。

目錄結(jié)構(gòu)如下:

.
├── endpoint # 接收請(qǐng)求,構(gòu)建 endpoint.Endpoint 調(diào)用 service 層的接口方法,處理請(qǐng)求參數(shù),返回響應(yīng)結(jié)果給 transport 層
│ └── user.go
├── go.mod
├── go.sum
├── main.go
├── service # 定義業(yè)務(wù)接口并實(shí)現(xiàn)接口方法
│ └── user.go
└── transport # 對(duì)外提供調(diào)用接口(http 或 rpc)
└── http.go
  • service 包定義服務(wù)(user 服務(wù))的接口,并實(shí)現(xiàn)接口方法。
...
type IUser interface {
Register(ctx context.Context, req *RegisterRequest) (*User, error)
Login(ctx context.Context, email, password string) (*User, error)
}
...
  • endpoint 包為接口方法構(gòu)建 endpoint.Endpoint,將請(qǐng)求參數(shù)轉(zhuǎn)換為接口方法可以處理的參數(shù),并將返回的響應(yīng)結(jié)果封裝為對(duì)應(yīng)的 response 結(jié)構(gòu)體,返回給 transport 包。
...
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
}
}
...
  • transport 包把構(gòu)建的 endpoint.Endpoint 提供給調(diào)用方。
...
func NewHttpHandler(ctx context.Context, endpoints *endpoint.Endpoints) http.Handler {
r := http.NewServeMux()
r.Handle("/register", kitHttp.NewServer(endpoints.RegisterEndpoint, decRegisterRequest, encResponse))
return r
}
...
  • 在 main 函數(shù)中,創(chuàng)建 service、endpoint 和 transport,并啟動(dòng) Web 服務(wù)器。
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
}
}
  • 使用 go run 命令啟動(dòng),并使用 cURL 調(diào)用 http 接口。
go run main.go

curl -X POST http://localhost:8080/register \
-d 'email=gopher@88.com&password=123456&username=gopher'

3.總結(jié)

本文我們通過一個(gè)簡(jiǎn)單的用戶中心項(xiàng)目,介紹如何基于 Go kit 開發(fā) Web 項(xiàng)目,為了方便讀者朋友們理解代碼,項(xiàng)目代碼中未使用其他組件,感興趣的讀者朋友可以嘗試完善,例如添加操作數(shù)據(jù)庫的代碼。

責(zé)任編輯:武曉燕 來源: Golang語言開發(fā)棧
相關(guān)推薦

2022-09-04 23:24:45

Go語言監(jiān)控

2013-03-12 09:50:45

GoRESTful Web

2023-12-25 09:49:01

Golang架構(gòu)Go-Kit

2023-11-06 01:39:02

Go語言開發(fā)

2018-03-12 22:13:46

GO語言編程軟件

2012-03-20 09:20:40

Go語言

2023-01-16 00:12:20

Go語言Web

2022-04-13 08:20:32

DockerGo項(xiàng)目

2012-07-06 13:16:37

Google Go

2022-03-22 09:22:21

Go kitgRPC網(wǎng)絡(luò)傳輸

2021-06-08 10:41:00

Go語言算法

2023-12-26 00:58:53

Web應(yīng)用Go語言

2024-06-06 09:47:56

2025-02-05 12:09:12

2024-04-28 10:17:30

gnetGo語言

2012-10-08 09:25:59

GoGo語言開發(fā)語言

2015-01-05 09:44:33

Github

2013-04-08 15:52:06

Go語言

2012-05-19 22:17:30

Android

2022-01-17 16:09:43

Go語言開發(fā)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)