就在上周日,我在 GitHub 閑逛(就像我的大部分周日一樣),偶然發(fā)現(xiàn)了一個(gè)非常受歡迎超過(guò) 10K 的提交量的倉(cāng)庫(kù),我不打算說(shuō)出名字。盡管我知道這個(gè)項(xiàng)目的技術(shù)棧,但對(duì)其代碼還不太熟悉。里面不少功能被隨機(jī)地扔在了一個(gè)名為 utils 或更糟糕的 helpers 目錄下面。 大項(xiàng)目的陷阱是,隨著時(shí)間的推移,它們會(huì)變得非常復(fù)雜,以至于重寫比培養(yǎng)新人來(lái)理解代碼然后修改要容易得多。 這使我想到了從實(shí)現(xiàn)層面談?wù)麧嵓軜?gòu)。這篇文章將包含一些 Go 代碼,但不用擔(dān)心,即使你不熟悉這門語(yǔ)言,要說(shuō)的概念也是相當(dāng)容易理解的。 什么是整潔架構(gòu)? 簡(jiǎn)而言之,你會(huì)從使用整潔架構(gòu)中獲得以下好處。
現(xiàn)在,如果你想更多了解整潔架構(gòu)是如何工作的,你可以閱讀 Bob 大叔的博客 (2)?,F(xiàn)在,讓我們展開(kāi)一個(gè)整潔架構(gòu)的示例實(shí)現(xiàn),GitHub 可參看 (1)。 Clean-Architecture-Sample ├── api │ ├── handler │ │ ├── admin.go │ │ └── user.go │ ├── main.go │ ├── middleware │ │ ├── auth.go │ │ └── cors.go │ └── views │ └── errors.go ├── bin │ └── main ├── config.json ├── docker-compose.yml ├── go.mod ├── go.sum ├── Makefile ├── pkg │ ├── admin │ │ ├── entity.go │ │ ├── postgres.go │ │ ├── repository.go │ │ └── service.go │ ├── errors.go │ └── user │ ├── entity.go │ ├── postgres.go │ ├── repository.go │ └── service.go ├── README.md 實(shí)體 實(shí)體是可以通過(guò)函數(shù)實(shí)現(xiàn)的核心業(yè)務(wù)對(duì)象。用 MVC 術(shù)語(yǔ)來(lái)說(shuō),它們是整潔架構(gòu)的模型層。所有的實(shí)體和服務(wù)都封裝在 pkg 目錄中。這其實(shí)就是我們要抽象出的東西,讓它和其他部分分開(kāi)。 如果你看一下 user 下面的 entity.go ,它看起來(lái)是這樣的。
pkg/user/entity.go 實(shí)體是在 Repository 接口中使用的,它可以用任何數(shù)據(jù)庫(kù)實(shí)現(xiàn)。在本例中,我們?cè)?postgres.go 中用 Postgres 實(shí)現(xiàn)了它,由于 Repository 可以用任何數(shù)據(jù)庫(kù)實(shí)現(xiàn),因此與所實(shí)現(xiàn)細(xì)節(jié)無(wú)關(guān)。 package user import ( 'context' ) type Repository interface { FindByID(ctx context.Context, id uint) (*User, error) BuildProfile(ctx context.Context, user *User) (*User, error) CreateMinimal(ctx context.Context, email, password, phoneNumber string) (*User, error) FindByEmailAndPassword(ctx context.Context, email, password string) (*User, error) FindByEmail(ctx context.Context, email string) (*User, error) DoesEmailExist(ctx context.Context, email string) (bool, error) ChangePassword(ctx context.Context, email, password string) error } pkg/user/repository.go Service 服務(wù)包括面向更高層次的業(yè)務(wù)邏輯功能的接口。例如,F(xiàn)indByID 可能是一個(gè)存儲(chǔ)層函數(shù),但 login 或 signup 則是服務(wù)層函數(shù)。服務(wù)是存儲(chǔ)的抽象層,它們不與數(shù)據(jù)庫(kù)交互,而是與存儲(chǔ)的接口交互。
pkg/user/service.go 服務(wù)是在用戶接口層面實(shí)現(xiàn)的。 接口適配器 每個(gè)用戶接口都有獨(dú)立的目錄。在我們的例子中,因?yàn)槲覀冇?API 作為接口,因此有一個(gè)叫 api 的目錄。 現(xiàn)在,由于每個(gè)用戶接口對(duì)請(qǐng)求的監(jiān)聽(tīng)方式不同,所以接口適配器都有自己的 main.go 文件,其任務(wù)如下。
在這里,Handler 程序只是 Request-Response 模型的用戶接口實(shí)現(xiàn)。每個(gè)服務(wù)都有自己的 Handler 程序。參見(jiàn) user.go package handler import ( 'encoding/json' 'net/http' 'github.com/L04DB4L4NC3R/jobs-mhrd/api/middleware' 'github.com/L04DB4L4NC3R/jobs-mhrd/api/views' 'github.com/L04DB4L4NC3R/jobs-mhrd/pkg/user' 'github.com/dgrijalva/jwt-go' 'github.com/spf13/viper' ) func register(svc user.Service) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { views.Wrap(views.ErrMethodNotAllowed, w) return } var user user.User if err := json.NewDecoder(r.Body).Decode(&user); err != nil { views.Wrap(err, w) return } u, err := svc.Register(r.Context, user.Email, user.Password, user.PhoneNumber) if err != nil { views.Wrap(err, w) return } w.WriteHeader(http.StatusCreated) token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ 'email': u.Email, 'id': u.ID, 'role': 'user', }) tokenString, err := token.SignedString(byte(viper.GetString('jwt_secret'))) if err != nil { views.Wrap(err, w) return } json.NewEncoder(w).Encode(map[string]interface{}{ 'token': tokenString, 'user': u, }) return }) } func login(svc user.Service) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { views.Wrap(views.ErrMethodNotAllowed, w) return } var user user.User if err := json.NewDecoder(r.Body).Decode(&user); err != nil { views.Wrap(err, w) return } u, err := svc.Login(r.Context, user.Email, user.Password) if err != nil { views.Wrap(err, w) return } token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ 'email': u.Email, 'id': u.ID, 'role': 'user', }) tokenString, err := token.SignedString(byte(viper.GetString('jwt_secret'))) if err != nil { views.Wrap(err, w) return } json.NewEncoder(w).Encode(map[string]interface{}{ 'token': tokenString, 'user': u, }) return }) } func profile(svc user.Service) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // @protected // @description build profile if r.Method == http.MethodPost { var user user.User if err := json.NewDecoder(r.Body).Decode(&user); err != nil { views.Wrap(err, w) return } claims, err := middleware.ValidateAndGetClaims(r.Context, 'user') if err != nil { views.Wrap(err, w) return } user.Email = claims['email'].(string) u, err := svc.BuildProfile(r.Context, &user) if err != nil { views.Wrap(err, w) return } json.NewEncoder(w).Encode(u) return } else if r.Method == http.MethodGet { // @description view profile claims, err := middleware.ValidateAndGetClaims(r.Context, 'user') if err != nil { views.Wrap(err, w) return } u, err := svc.GetUserProfile(r.Context, claims['email'].(string)) if err != nil { views.Wrap(err, w) return } json.NewEncoder(w).Encode(map[string]interface{}{ 'message': 'User profile', 'data': u, }) return } else { views.Wrap(views.ErrMethodNotAllowed, w) return } }) } func changePassword(svc user.Service) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPost { var u user.User if err := json.NewDecoder(r.Body).Decode(&u); err != nil { views.Wrap(err, w) return } claims, err := middleware.ValidateAndGetClaims(r.Context, 'user') if err != nil { views.Wrap(err, w) return } if err := svc.ChangePassword(r.Context, claims['email'].(string), u.Password); err != nil { views.Wrap(err, w) return } return } else { views.Wrap(views.ErrMethodNotAllowed, w) return } }) } // expose handlers func MakeUserHandler(r *http.ServeMux, svc user.Service) { r.Handle('/api/v1/user/ping', http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) return })) r.Handle('/api/v1/user/register', register(svc)) r.Handle('/api/v1/user/login', login(svc)) r.Handle('/api/v1/user/profile', middleware.Validate(profile(svc))) r.Handle('/api/v1/user/pwd', middleware.Validate(changePassword(svc))) } 錯(cuò)誤處理 整潔架構(gòu)中錯(cuò)誤處理的基本原則如下。
這本質(zhì)上的意思是,所有的數(shù)據(jù)庫(kù)級(jí)錯(cuò)誤都應(yīng)該由用戶接口以不同的方式來(lái)處理。例如,如果用戶接口是一個(gè) REST API,那么錯(cuò)誤應(yīng)該以 HTTP 狀態(tài)碼的形式表現(xiàn)出來(lái),比如 500 錯(cuò)誤。而如果是 CLI 方式,則應(yīng)該以狀態(tài)碼 1 退出。 在整潔架構(gòu)中,Repository 錯(cuò)誤可以在 pkg 的根目錄下,這樣 Repository 函數(shù)就可以在控制流出現(xiàn)問(wèn)題時(shí)調(diào)用它們,如下圖所示。
pkg/errors.go 然后,同樣的錯(cuò)誤可以根據(jù)具體的用戶界面來(lái)實(shí)現(xiàn),最常見(jiàn)的是可以在 Handler 層面在 view 中進(jìn)行封裝,如下圖所示。 package views import ( 'encoding/json' 'errors' 'net/http' log 'github.com/sirupsen/logrus' pkg 'github.com/L04DB4L4NC3R/jobs-mhrd/pkg' ) type ErrView struct { Message string `json:'message'` Status int `json:'status'` } var ( ErrMethodNotAllowed = errors.New('Error: Method is not allowed') ErrInvalidToken = errors.New('Error: Invalid Authorization token') ErrUserExists = errors.New('User already exists') ) var ErrHTTPStatusMap = map[string]int{ pkg.ErrNotFound.Error: http.StatusNotFound, pkg.ErrInvalidSlug.Error: http.StatusBadRequest, pkg.ErrExists.Error: http.StatusConflict, pkg.ErrNoContent.Error: http.StatusNotFound, pkg.ErrDatabase.Error: http.StatusInternalServerError, pkg.ErrUnauthorized.Error: http.StatusUnauthorized, pkg.ErrForbidden.Error: http.StatusForbidden, ErrMethodNotAllowed.Error: http.StatusMethodNotAllowed, ErrInvalidToken.Error: http.StatusBadRequest, ErrUserExists.Error: http.StatusConflict, } func Wrap(err error, w http.ResponseWriter) { msg := err.Error code := ErrHTTPStatusMap[msg] // If error code is not found // like a default case if code == 0 { code = http.StatusInternalServerError } w.WriteHeader(code) errView := ErrView{ Message: msg, Status: code, } log.WithFields(log.Fields{ 'message': msg, 'code': code, }).Error('Error occurred') json.NewEncoder(w).Encode(errView) } 每個(gè) Repository 級(jí)別的錯(cuò)誤,或者其他的錯(cuò)誤,都會(huì)被封裝在一個(gè) map 中,該 map 返回一個(gè)與相應(yīng)的錯(cuò)誤相對(duì)應(yīng)的 HTTP 狀態(tài)代碼。 總結(jié) 整潔架構(gòu)是一個(gè)很好的構(gòu)造代碼的方法,并可以忘記所有可能由于敏捷迭代或快速原型而產(chǎn)生的復(fù)雜問(wèn)題。由于和數(shù)據(jù)庫(kù)、用戶界面,以及框架無(wú)關(guān),整潔架構(gòu)確實(shí)名副其實(shí)。 (小編注:看完本文,如果你還有些疑惑,建議閱讀鏈接1項(xiàng)目代碼后,再來(lái)結(jié)合文章看) 參考資料 (1) Clean Architecture Sample https://github.com/L04DB4L4NC3R/clean-architecture-sample (2) Clean Coder Blog https://blog./uncle-bob/2012/08/13/the-clean-architecture.html 英文原文: https:///gdg-vit/clean-architecture-the-right-way-d83b81ecac6 |
|
來(lái)自: taotao_2016 > 《歷史》