Share
Embed
Transcript
52 slides
-
©Gunosy Inc. 2 ࣗݾհ ▪ azihsoyn (͋͡;ͦΜ) – Twitter –
Github – …etc ▪ ۀ – GoͰAPIαʔόͷ։ൃɾӡ༻ ▪ झຯ – Γ(6ׂ) • ւͰϧΞʔΓ – Ξχϝ(4ׂ) • μΠϠ༷ʙ ▪ ۙگ – isucon6ຊઓग़·͢ʂ (νʔϜ: ͝จpoyoͰ͔͢ʁ) ٢ᖒ ࠸ -
©Gunosy Inc. 3 Gunosy ใΩϡϨʔγϣϯαʔϏεʮά ϊγʔʯͱ 20166݄1ʹKDDIגࣜձࣾͱڞಉͰϦϦʔεͨ͠ ແྉχϡʔε৴ΞϓϦʮχϡʔεύεʯΛఏڙ͢Δ ձࣾͰ͢ɻʮใΛੈքதͷਓʹ࠷దʹಧ͚ΔʯΛ Ϗδϣϯʹ׆ಈ͍ͯ͠·͢ɻ
ωοτ্ʹଘࡏ͢Δ͞·͟·ͳใΛɺ ಠࣗͷΞϧΰϦζϜͰऩूɺධՁ͚Λߦ͍ Ϣʔβʔʹಧ͚·͢ɻ ใΩϡϨʔγϣϯαʔϏε ʮά ϊγʔʯ 200ഔମҎ্ͷχϡʔειʔεΛϕʔεʹɺ ৽ͨʹ։ൃͨ͠ใղੳɾ৴ٕज़Λ༻͍ͯࣗಈతʹ બఆͨ͠χϡʔεใΛϢʔβʔʹಧ͚·͢ɻ ແྉχϡʔε৴ΞϓϦ ʮχϡʔεύεʯ גࣜձࣾGunosy – ʮใΛੈքதͷਓʹ࠷దʹಧ͚Δʯ -
©Gunosy Inc. 6 ▪ https://github.com/citerus/dddsample-core – DDDͷαϯϓϧ࣮(Java) ▪ https://github.com/VaughnVernon/IDDD_Samples –
࣮ફDDDͷαϯϓϧ࣮(Java) ▪ https://github.com/marcusolsson/goddd – DDDͷαϯϓϧ࣮(golang) ͜ͷ͋ͨΓͷίʔυಡΈͳ͕ΒϓϩμΫτʹམͱ͜͠Μͩײ͡Ͱ͢ -
©Gunosy Inc. 8 վΊͯࠓͷ ͢͜ͱ ͞ͳ͍͜ͱ ▪ GoͰAPIॻ͘ͱ͖ʹؾΛ͚ͭͯΔ͜ͱ ▪ ύοέʔδߏ
▪ ϋϚͬͨ͜ͱ ▪ DDDͷ༻ޠͷઆ໌ͱ͔ ▪ ϥΠϒϥϦͷ͍ํͱ͔ GoͰDDDΛͬͯࢼߦࡨޡͨ͠Λ͍ͨ͠ͱࢥ͍·͢ -
©Gunosy Inc. 10 ▪ main.goΛॻ͘ ▪ handlerΛॻ͘ ▪ هࣄͷmodelΛॻ͘ ▪
هࣄͷmodelΛσʔλετΞ͔Βऔಘ͢ΔॲཧΛ࣮ ▪ jsonͱ͔ʹมͯ͠ฦ͢ େମ͜ΜͳྲྀΕʹͳΔͱࢥ͍·͢ -
©Gunosy Inc. 12 // main.go package main import ( "context"
"github.com/azihsoyn/IDDD_go_sample/article" "github.com/azihsoyn/IDDD_go_sample/internal/repository" "github.com/guregu/kami" ) func main() { ctx := context.Background() ctx = repository.NewContext(ctx) kami.Context = ctx kami.Get("/articles", article.ArticleListHandler) kami.Get("/article/:article_id", article.ArticleHandler) kami.Serve() } repositoryͱ͍͏ͷΛNewͯ͠ΔҎ֎ಛʹมΘͬͨ͜ͱͯ͠ͳ͍ -
©Gunosy Inc. 14 handler (1) // article/api_article.go package article import
( "context" "net/http" "github.com/azihsoyn/IDDD_go_sample/internal/repository" ) func ArticleHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { req := articleRequest{} if err := req.Parse(ctx); err != nil { renderer.JSON(w, http.StatusBadRequest, err.Error()) } a, err := repository.FromContext(ctx).ArticleRepository.ResolveByID(req.ArticleID) if err != nil { renderer.JSON(w, http.StatusBadRequest, err.Error()) } renderArticleView(w, a) } -
©Gunosy Inc. 15 handler (2) - request // article/request_article.go package
article import ( "context" "strconv" "github.com/azihsoyn/IDDD_go_sample/internal/domain/article" "github.com/guregu/kami" ) type articleRequest struct { ArticleID article.Identifier } func (req *articleRequest) Parse(ctx context.Context) error { articleID, err := strconv.ParseInt(kami.Param(ctx, "article_id"), 10, 64) if err != nil { return err } req.ArticleID = article.Identifier(articleID) return nil } -
©Gunosy Inc. 16 handler (3) - view // article/view_article.go package
article import ( "net/http" "github.com/azihsoyn/IDDD_go_sample/internal/domain/article" ) type articleView struct { ID int64 `json:"id"` Title string `json:"title"` Content string `json:"content"` } func renderArticleView(w http.ResponseWriter, a article.Article) { view := articleView{ ID: int64(a.ID), Title: a.Title, Content: a.Content, } renderer.JSON(w, http.StatusOK, view) } -
©Gunosy Inc. 17 ▪ handlerͰΔ͜ͱΛγϯϓϧʹ͢Δ – ϦΫΤετͷύʔε&όϦσʔγϣϯ – modelΛऔಘ –
Ϩεϙϯε༻ʹviewΛੜ ΤϥʔϋϯυϦϯά͕ଟ͘ͳΒͳ͍Α͏ʹͯ͠Δ ▪ ϑΝΠϧΛׂΓͱׂ͢Δ – ϦΫΤετ༻ͷϑΝΠϧ – Ϩεϙϯε༻ͷϑΝΠϧ – handler༻ͷϑΝΠϧ ϑΝΠϧ໊ͰԿͯ͠Δ͔͔ΔΑ͏ʹ handlerͰҙ͍ࣝͯ͠Δ͜ͱ ͱʹ͔͘ݟ͘͢γϯϓϧʹʂ -
©Gunosy Inc. 19 modelͷఆٛ // internal/domain/article/article.go package article type Identifier
int64 type Article struct { ID Identifier Title string Content string } -
©Gunosy Inc. 20 modelͷऔಘ(repository) // internal/domain/article/repository.go package article //go:generate mockgen
-package article -source repository.go -destination mock/ repository_mock.go -imports .=github.com/azihsoyn/IDDD_go_sample/internal/domain/article type Repository interface { ResolveByID(articleID Identifier) (Article, error) ResolveAll() ([]Article, error) } -
©Gunosy Inc. 21 ▪ modelΛhandlerͷviewͱڞ༻ʹ͠ͳ͍ – modelʹjsonͷstruct tag͚ͭͳ͍ ▪ modelDBͷςʔϒϧߏͱҰக͢ΔͱݶΒͳ͍
– joinͨ݁͠Ռͷ߹͋Δ – ෆཁͳΧϥϜΛmodelʹ࣋ͭඞཁͳ͍ ▪ ϑΝΠϧΛׂΓͱׂ͢Δ – model༻ͷϑΝΠϧ – interface༻ͷϑΝΠϧ ϑΝΠϧ໊ͰԿͯ͠Δ͔͔ΔΑ͏ʹ modelͰҙ͍ࣝͯ͠Δ͜ͱ1 -
©Gunosy Inc. 22 ▪ repositoryΛinterfaceʹ࣮ͯ͠ͱ͢Δ – articleͱ͍͏modelΛͲ͔͜Βऔಘ͢Δ͔Λҙࣝ͠ͳ͍ – interfaceʹ͓ͯ͘͜͠ͱͰmockͷੜ͕؆୯ ▪
gomockΛͬͯinterface͔ΒmockΛੜ – https://github.com/golang/mock – mockmock༻ͷσΟϨΫτϦʹ articleσΟϨΫτϦͷΧόϨοδΛԼ͛ͳ͍ͨΊ (CI࣌ʹmockΛੜͯ͠ίϛοτ͠ͳ͍ํ๏͋Δͱ͔) ▪ ࢀরܥResolveXXXɺߋ৽ܥStoreXXX – ໋໊Ͱ·ͳ͍Α͏ʹ – ྫ֎͋Δ modelͰҙ͍ࣝͯ͠Δ͜ͱ2 ͱʹ͔͘ݟ͘͢γϯϓϧʹʂ -
©Gunosy Inc. 24 // main.go package main import ( "context"
"github.com/azihsoyn/IDDD_go_sample/article" "github.com/azihsoyn/IDDD_go_sample/internal/repository" "github.com/guregu/kami" ) func main() { ctx := context.Background() ctx = repository.NewContext(ctx) kami.Context = ctx kami.Get("/articles", article.ArticleListHandler) kami.Get("/article/:article_id", article.ArticleHandler) kami.Serve() } -
©Gunosy Inc. 25 ·ͣcontext͔ΒrepositoryΛऔΕΔΑ͏ʹ // internal/repository/context.go package repository import (…)
type Repository struct { ArticleRepository article.Repository // ଞͷmodelͷrepository͕Ͱ͖ͨΒՃ͍ͯ͘͠ } func NewContext(ctx context.Context) context.Context { return context.WithValue(ctx, contextKey, newRepository(ctx)) } -
©Gunosy Inc. 27 mockΛͬͯrepositoryΛ࣮ // internal/repository/context.go func newRepository(ctx context.Context) *Repository
{ t := new(testing.T) ctrl := gomock.NewController(t) repo := articlemock.NewMockRepository(ctrl) repo.EXPECT().ResolveByID(article.Identifier(1)).Return(article.Article{ ID: 1, Title: "hello", Content: "world", }, nil).Times(100) return &Repository{ ArticleRepository: repo, } } ͜͜·ͰͰAPIαʔόͱͯ͠ಈ͘Α͏ʹͳΔ -
©Gunosy Inc. 29 // internal/repository/article/repository.go package articlerepo import (…) type
articleRepository struct{} var _ article.Repository = (*articleRepository)(nil) func New() article.Repository { return &articleRepository{} } func (repo *articleRepository) ResolveByID(articleID article.Identifier) (article.Article error) { // DBΩϟογϡ͔ΒarticleΛऔಘ͢Δॲཧ return article.Article{}, errors.New("not implemented") } func (repo *articleRepository) ResolveAll() ([]article.Article, error) { // DBΩϟογϡ͔ΒarticleΛऔಘ͢Δॲཧ return nil, errors.New("not implemented") } -
©Gunosy Inc. 30 // internal/repository/context.go package repository func newRepository(ctx context.Context)
*Repository { /* t := new(testing.T) ctrl := gomock.NewController(t) repo := articlemock.NewMockRepository(ctrl) repo.EXPECT().ResolveByID(article.Identifier(1)).Return(article.Article{ ID: 1, Title: "hello", Content: "world", }, nil).Times(100) */ repo := articlerepo.New() return &Repository{ ArticleRepository: repo, } } context.goΛ͚ͩ͢Ͱmockͱ࣮͕ΓସΘΔ -
©Gunosy Inc. 33 ͜Μͳॲཧ func UserSpecificArticles(as []Article, userID int64) []Article
{ ret := make([]Article, 0, len(as)) // userID͕ಛఆͷ(= 1)ͩͬͨΒهࣄID͕100ͷهࣄΛઌ಄ʹग़͢ if userID == 1 { ret = append(ret, Article{ ID: 100, Title: "for user 1", Content: "this is recommended by your activity", }) } ret = append(ret, as...) return ret } Ͳ͜ʹ࣮͢Δ͔ʁ -
©Gunosy Inc. 34 Ҋ1) articleͷrepositoryʹ࣮ repository͋͘·ͰσʔλετΞͱͷΓͱΓʹཹΊΔ͖ // internal/repository/article/repository.go package articlerepo
func (repo *articleRepository) ResolveAll() ([]article.Article, error) { as, err := repo.resolveAllFromMySQL() if err != nil { return nil, err } as = userSpecificArticles(as, req.UserID) return ret } ✕ -
©Gunosy Inc. 35 Ҋ2) handlerʹ࣮ handler͕articleΛՃͯ͠͠·͍͕ࢄͯ͠Δ // article/api_article_list.go package article
import (…) func ArticleListHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { req := articleListRequest{} if err := req.Parse(ctx, r); err != nil { renderer.JSON(w, http.StatusBadRequest, err.Error()) } as, err := repository.FromContext(ctx).ArticleRepository.ResolveAll(req.UserID) if err != nil { renderer.JSON(w, http.StatusBadRequest, err.Error()) } as = userSpecificArticles(as, req.UserID) renderArticleListView(w, as) } ˚ -
©Gunosy Inc. 36 Ҋ3) domain modelʹ࣮͢Δ هࣄͷϩδοΫͳͷͰΑͦ͞͏ɻ ͨͩhandlerͰݺͼग़͢ͷมΘΒͳ͍ ̋ //
article/api_article_list.go package article import (…) func ArticleListHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { req := articleListRequest{} if err := req.Parse(ctx, r); err != nil { renderer.JSON(w, http.StatusBadRequest, err.Error()) } as, err := repository.FromContext(ctx).ArticleRepository.ResolveAll(req.UserID) if err != nil { renderer.JSON(w, http.StatusBadRequest, err.Error()) } as = article.UserSpecificArticles(as, req.UserID) renderArticleListView(w, as) } -
©Gunosy Inc. 37 ▪ handlerͰΔ͜ͱΛγϯϓϧʹ͢Δ – ϦΫΤετͷύʔε&όϦσʔγϣϯ – modelΛऔಘ –
Ϩεϙϯε༻ʹviewΛੜ ΤϥʔϋϯυϦϯά͕ଟ͘ͳΒͳ͍Α͏ʹͯ͠Δ ▪ ϑΝΠϧΛׂΓͱׂ͢Δ – ϦΫΤετ༻ͷϑΝΠϧ – Ϩεϙϯε༻ͷϑΝΠϧ – handler༻ͷϑΝΠϧ ϑΝΠϧ໊ͰԿͯ͠Δ͔͔ΔΑ͏ʹ handlerͰҙ͍ࣝͯ͠Δ͜ͱ(࠶ܝ) ͱʹ͔͘ݟ͘͢γϯϓϧʹʂ -
©Gunosy Inc. 44 αʔϏεͰ࣮ (1) // internal/domain/article/user_specific_article_service.go package article type
UserSpecificArticleService interface { Get(userID int64) ([]Article, error) } // internal/service/article/user_specific_article_service.go type userSpecificArticleService struct { repo Repository } func NewUserSpecificArticleService(articleRepo Repository) UserSpecificArticleService { return &userSpecificArticleService{ repo: articleRepo, } } -
©Gunosy Inc. 45 αʔϏεͰ࣮ (2) func (s *userSpecificArticleService) Get(userID int64)
([]Article, error) { as, err := s.repo.ResolveAll() if err != nil { return nil, err } as = article.UserSpecificArticles(as, userID) return as, nil } service͕repositoryϏδωεϩδοΫΛ͏ -
©Gunosy Inc. // article/api_article_list.go package article import (…) func ArticleListHandler(ctx
context.Context, w http.ResponseWriter, r *http.Request) { req := articleRequest{} if err := req.Parse(ctx); err != nil { renderer.JSON(w, http.StatusBadRequest, err.Error()) } as, err := service.FromContext(ctx).UserSpecificArticleService.Get(req.UserID) if err != nil { renderer.JSON(w, http.StatusBadRequest, err.Error()) } renderArticleListView(w, as) } 46 αʔϏεͰ࣮ (3) handlerͷγϯϓϧ͕͞อͨΕͨ ˕ -
©Gunosy Inc. 47 ಛఆͷϢʔβʔʹରͯ͠ಛఆͷهࣄΛՃͨ͠Γ͠ͳ͔ͬͨΓ͢Δ ̍ ಛఆͷϢʔβʔʹରͯ͠ಛఆͷλϒΛՃͨ͠Γฒͼସ͑ͨΓ͢Δ ̎ GunosyͰαʔϏεʹͯ͠Δॲཧྫ ▪ A/Bςετͱ͔
▪ ߟ͑ΒΕΔύϥϝʔλ – ϢʔβʔID OS ΞϓϦέʔγϣϯͷόʔδϣϯ...etc ▪ A/Bςετͱ͔ ▪ ߟ͑ΒΕΔύϥϝʔλ – ϢʔβʔID OS ΞϓϦέʔγϣϯͷόʔδϣϯ...etc xxx_serviceͬͯͷ͕͋ͬͨΒԿ͔ෳࡶͳ͜ͱΛͬͯΔͳͬͯΠϝʔδ -
©Gunosy Inc. 49 ϋϚͬͨͱ͜Ζ ▪ package໊ͷॏෳ – domainͱrepository͕େମಉ͡ߏʹͳΔͨΊ – ํ͕ͳ͍ͷͰrepositoryͷํʹsuffixΛ͚͍ͭͯΔ
– ྫ) repository/article → package artilcerepo – ߋʹimport͢Δͱ໊͖લ͖ʹ͠ͳ͍ͱ͏·͘ิ͕ޮ͔ͳ͍ ▪ cyclic import – σΟϨΫτϦΛׂ͢Δฐ – ਂ͍֊ʹ͍ΔϑΝΠϧͷςετͰϓϩδΣΫτશମͰ͑ΔΑ͏ ͳศརhelperΛݺͼग़ͦ͏ͱͯ͠Τϥʔ͍ͬͯ͏ͷ͕Α͋ͬͨ͘ – σΟϨΫτϦϑΝΠϧΛখ͍ͯ͘͞͠ΔͷͰศརhelperʹཔΒͣʹ ςετΛॻ͘Α͏ʹͨ͠ -
©Gunosy Inc. 50 ·ͱΊ ▪ DDDҰਓͰͬͯҙຯ͕͋Δʂ(ͱࢥ͏) – Ͳ͜ΛͲ͏͍͍͔͙ͤ͢ΠϝʔδͰ͖Δ – मਖ਼ൣғΛখ͘͞Ͱ͖Δ
– ϨϏϡʔ͘͢͠Ͱ͖Δ(ࠓޙਓ͕૿͑ͨͱ͖) ▪ ϑΝΠϧҙຯͷ͋Δ୯Ґʹׂ͢Δ ▪ ύοέʔδҙຯͷ͋Δ୯Ґʹׂ͢Δ ▪ ؾ͍ͮͨΒϦϑΝΫλϦϯά͢Δ – ύοέʔδʹҧײ͕Ͱ͖ͯͨΒ – ϦϑΝΫλ͍͢͠Α͏ʹϦϑΝΫλ͍ͯ͘͠ -
©Gunosy Inc. 52 ͦͷଞࢿྉ ▪ https://github.com/azihsoyn/IDDD_go_sample – ࠓճͷαϯϓϧϓϩδΣΫτ ▪ https://github.com/guregu/kami
– contextରԠͷWAF ▪ https://github.com/azihsoyn/lwcache – [એ] ϓϩηεΩϟογϡϥΠϒϥϦ ▪ https://github.com/golang/mock – interface͔Βmockੜͯ͘͠ΕΔπʔϧ