Skip to content

Commit

Permalink
add: wire
Browse files Browse the repository at this point in the history
  • Loading branch information
rema424 committed Nov 17, 2019
1 parent 871aa9c commit 5aeccfb
Show file tree
Hide file tree
Showing 16 changed files with 1,630 additions and 295 deletions.
1,554 changes: 1,266 additions & 288 deletions CREDITS

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
# hexample

Go っぽいプロジェクト構成でヘキサゴナルアーキテクチャ的なことをやるリポジトリ

## wire

### インストール

```sh
go get github.com/google/wire/cmd/wire
```

### ビルド

```sh
cd cmd/httpwire
wire
```

### アプリケーションの起動

```sh
cd cmd/httpwire
go run main.go wire_gen.go
```
32 changes: 32 additions & 0 deletions cmd/httpwire/controller/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package controller

import (
"github.com/rema424/hexample/internal/service1"

"github.com/labstack/echo/v4"
)

// Controller ...
type Controller struct{}

// NewController ...
func NewController() *Controller {
return &Controller{}
}

// HandleMessage ...
// curl localhost:8080/messageeee
func (ctrl *Controller) HandleMessage(c echo.Context) error {
msg := c.Param("message")
if msg == "" {
msg = "Hello, from http!"
}

arg := service1.AppCoreLogicIn{
From: "http",
Message: msg,
}

service1.AppCoreLogic(c.Request().Context(), arg)
return nil
}
63 changes: 63 additions & 0 deletions cmd/httpwire/controller/controller2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package controller

import (
"net/http"
"strconv"

"github.com/rema424/hexample/internal/service2"

"github.com/labstack/echo/v4"
)

// Controller2 ...
type Controller2 struct {
p *service2.Provider
}

// NewController2 ...
func NewController2(p *service2.Provider) *Controller2 {
return &Controller2{p}
}

// HandlePersonRegister ...
// curl -X POST -H 'Content-type: application/json' -d '{"name": "Alice", "email": "[email protected]"}' localhost:8080/people
func (ctrl *Controller2) HandlePersonRegister(c echo.Context) error {
in := struct {
Name string `json:"name"`
Email string `json:"email"`
}{}

if err := c.Bind(&in); err != nil {
return c.JSON(http.StatusBadRequest, err.Error())
}

// TODO: implement
// if err := c.Validate(&in); err != nil {
// return c.JSON(http.StatusUnprocessableEntity, err.Error())
// }

ctx := c.Request().Context()
psn, err := ctrl.p.RegisterPerson(ctx, in.Name, in.Email)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}

return c.JSON(http.StatusOK, psn)
}

// HandlePersonGet ...
// curl localhost:8080/people/999
func (ctrl *Controller2) HandlePersonGet(c echo.Context) error {
id, err := strconv.Atoi(c.Param("personID"))
if err != nil {
return c.JSON(http.StatusUnprocessableEntity, err.Error())
}

ctx := c.Request().Context()
psn, err := ctrl.p.GetPersonByID(ctx, int64(id))
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}

return c.JSON(http.StatusOK, psn)
}
71 changes: 71 additions & 0 deletions cmd/httpwire/controller/controller3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package controller

import (
"net/http"

"github.com/rema424/hexample/internal/service3"

"github.com/labstack/echo/v4"
)

// Controller3 ...
type Controller3 struct {
p *service3.Provider
}

// NewController3 ...
func NewController3(p *service3.Provider) *Controller3 {
return &Controller3{p}
}

// HandleAccountOpen ...
// curl -X POST -H 'Content-type: application/json' -d '{"ammount": 1000}' localhost:8080/accounts
func (ctrl *Controller3) HandleAccountOpen(c echo.Context) error {
in := struct {
Ammount int `json:"ammount"`
}{}

if err := c.Bind(&in); err != nil {
return c.JSON(http.StatusBadRequest, err.Error())
}

// TODO: implement
// if err := c.Validate(&in); err != nil {
// return c.JSON(http.StatusUnprocessableEntity, err.Error())
// }

ctx := c.Request().Context()
psn, err := ctrl.p.OpenAccount(ctx, in.Ammount)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}

return c.JSON(http.StatusOK, psn)
}

// HandleMoneyTransfer ...
// curl -X POST -H 'Content-type: application/json' -d '{"fromId": , "toId": , "ammount": 1000}' localhost:8080/accounts/transfer
func (ctrl *Controller3) HandleMoneyTransfer(c echo.Context) error {
in := struct {
FromAccountID int64 `json:"fromId"`
ToAccountID int64 `json:"toId"`
Ammount int `json:"ammount"`
}{}

if err := c.Bind(&in); err != nil {
return c.JSON(http.StatusBadRequest, err.Error())
}

// TODO: implement
// if err := c.Validate(&in); err != nil {
// return c.JSON(http.StatusUnprocessableEntity, err.Error())
// }

ctx := c.Request().Context()
from, to, err := ctrl.p.Transfer(ctx, in.Ammount, in.FromAccountID, in.ToAccountID)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}

return c.JSON(http.StatusOK, map[string]interface{}{"from": from, "to": to})
}
13 changes: 13 additions & 0 deletions cmd/httpwire/controller/controllers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package controller

// Controllers ...
type Controllers struct {
Ctrl *Controller
Ctrl2 *Controller2
Ctrl3 *Controller3
}

// NewControllers ...
func NewControllers(ctrl *Controller, ctrl2 *Controller2, ctrl3 *Controller3) *Controllers {
return &Controllers{ctrl, ctrl2, ctrl3}
}
91 changes: 91 additions & 0 deletions cmd/httpwire/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main

import (
"fmt"
"log"
"net/http"
"os"

"github.com/rema424/hexample/pkg/mysql"

"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/rema424/sqlxx"
)

var e = createMux()

func main() {
http.Handle("/", e)

port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}

log.Printf("Listening on port %s", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}

func init() {
// Mysql
c := mysql.Config{
Host: os.Getenv("DB_HOST"),
Port: os.Getenv("DB_PORT"),
User: os.Getenv("DB_USER"),
DBName: os.Getenv("DB_NAME"),
Passwd: os.Getenv("DB_PASSWORD"),
AllowNativePasswords: true,
}
db, err := mysql.Connect(c)
if err != nil {
log.Fatalln(err)
}
acsr, err := sqlxx.Open(db)
if err != nil {
log.Fatalln(err)
}

// --------------------------------------------------
// 手動でDI
// --------------------------------------------------

// Service2
// gateway2 := service2.NewGateway(db)
// provider2 := service2.NewProvider(gateway2)
// mockGateway2 := service2.NewMockGateway(service2.NewMockDB())
// provider2 := service2.NewProvider(mockGateway2)

// Service3
// gateway3 := service3.NewGateway(acsr)
// provider3 := service3.NewProvider(gateway3)
// mockGateway3 := service3.NewMockGateway(service3.NewMockDB())
// provider3 := service3.NewProvider(mockGateway3)

// ctrl := &controller.Controller{}
// ctrl2 := controller.NewController2(provider2)
// ctrl3 := controller.NewController3(provider3)

// --------------------------------------------------
// wireでDI
// --------------------------------------------------

ctrls := InitializeControllers(db, acsr)

e.GET("/:message", ctrls.Ctrl.HandleMessage)
e.GET("/people/:personID", ctrls.Ctrl2.HandlePersonGet)
e.POST("/people", ctrls.Ctrl2.HandlePersonRegister)
e.POST("/accounts", ctrls.Ctrl3.HandleAccountOpen)
e.POST("/accounts/transfer", ctrls.Ctrl3.HandleMoneyTransfer)
}

func createMux() *echo.Echo {
e := echo.New()

e.Use(middleware.Recover())
e.Use(middleware.Logger())
e.Use(middleware.Gzip())

return e
}
28 changes: 28 additions & 0 deletions cmd/httpwire/wire.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//+build wireinject

package main

import (
"github.com/rema424/hexample/cmd/httpwire/controller"
"github.com/rema424/hexample/internal/service2"
"github.com/rema424/hexample/internal/service3"

"github.com/google/wire"
"github.com/jmoiron/sqlx"
"github.com/rema424/sqlxx"
)

// InitializeControllers .
func InitializeControllers(db *sqlx.DB, acsr *sqlxx.Accessor) *controller.Controllers {
wire.Build(
service2.NewGateway,
service3.NewGateway,
service2.NewProvider,
service3.NewProvider,
controller.NewController,
controller.NewController2,
controller.NewController3,
controller.NewControllers,
)
return &controller.Controllers{}
}
28 changes: 28 additions & 0 deletions cmd/httpwire/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.13

require (
github.com/go-sql-driver/mysql v1.4.0
github.com/google/wire v0.3.0
github.com/jmoiron/sqlx v1.2.0
github.com/labstack/echo/v4 v4.1.11
github.com/rema424/sqlxx v0.0.0-20191110163611-fda406e032db
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-sql-driver/mysql v1.4.0 h1:7LxgVwFb2hIQtMm87NdgAVfXjnt4OePseqT1tKx+opk=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/subcommands v1.0.1/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/google/wire v0.3.0 h1:imGQZGEVEHpje5056+K+cgdO72p0LQv2xIIFXNGUf60=
github.com/google/wire v0.3.0/go.mod h1:i1DMg/Lu8Sz5yYl25iOdmc5CT5qusaa+zmRWs16741s=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
Expand Down Expand Up @@ -70,6 +75,7 @@ golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnf
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 h1:HuIa8hRrWRSrqYzx1qI49NNxhdi2PrY7gxVSq1JjLDc=
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65 h1:+rhAzEzT3f4JtomfC371qB+0Ola2caSKcY69NUBZrRQ=
Expand All @@ -85,6 +91,7 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190422233926-fe54fb35175b/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
google.golang.org/appengine v1.6.5 h1:tycE03LOZYQNhDpS27tcQdAzLCVMaj7QT2SXxebnpCM=
google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
Loading

0 comments on commit 5aeccfb

Please sign in to comment.