-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,630 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.