mirror of
https://github.com/chai2010/advanced-go-programming-book.git
synced 2025-05-24 04:22:22 +00:00
update protocol free section
This commit is contained in:
parent
2d4c1188a5
commit
60401c669f
@ -29,30 +29,78 @@ type CreateOrderParams struct {
|
|||||||
CreateTime time.Time
|
CreateTime time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 对订单服务入口的定义
|
||||||
type Entry interface {
|
type Entry interface {
|
||||||
GetCreateOrderParams() dto.CreateOrderParams
|
GetCreateOrderParams() dto.CreateOrderParams
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateOrder(e Entry) error {
|
func CreateOrder(e Entry) error {
|
||||||
params := e.GetCreateOrderParams()
|
params := e.GetCreateOrderParams()
|
||||||
|
|
||||||
|
// do some thing to create order
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// project/controller
|
// project/controller
|
||||||
type ThriftEntry struct{}
|
type ThriftGetOrderEntry struct{
|
||||||
type HTTPEntry struct{}
|
thriftRequestParams ThriftCreateOrderRequest
|
||||||
|
|
||||||
func (te ThriftEntry) GetCreateOrderParams() dto.CreateOrderParams {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (he HTTPEntry) GetCreateOrderParams() dto.CreateOrderParams {
|
type HTTPGetOrderEntry struct{
|
||||||
|
r *http.Request
|
||||||
}
|
}
|
||||||
|
|
||||||
var thriftEntryInstance ThriftEntry
|
func (te ThriftGetOrderEntry) GetCreateOrderParams() dto.CreateOrderParams {
|
||||||
var httpEntryInstance HTTPEntry
|
thriftRequestParams := te.thriftRequestParams
|
||||||
|
return logic.CreateOrderParams{
|
||||||
|
OrderID : thriftRequestParams.OrderID,
|
||||||
|
ShopID : thriftRequestParams.ShopID,
|
||||||
|
ProductID : thriftRequestParams.ProductID,
|
||||||
|
CreateTime : thriftRequestParams.CreateTime,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (he HTTPGetOrderEntry) GetCreateOrderParams() dto.CreateOrderParams {
|
||||||
|
//r := he.r
|
||||||
|
// get data
|
||||||
|
err := json.Unmarshal(data, &req) // or read body or something
|
||||||
|
|
||||||
|
return logic.CreateOrderParams{
|
||||||
|
OrderID : req.OrderID,
|
||||||
|
ShopID : req.ShopID,
|
||||||
|
ProductID : req.ProductID,
|
||||||
|
CreateTime : req.CreateTime,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// thrift serve on 9000
|
// thrift serve on 9000
|
||||||
|
func ThriftCreateOrderHandler(req ThriftCreateOrderRequest) (resp ThriftCreateOrderResp, error){
|
||||||
|
thriftEntryInstance := ThriftGetOrderEntry{
|
||||||
|
OrderID : req.OrderID,
|
||||||
|
ShopID : req.ShopID,
|
||||||
|
ProductID : req.ProductID,
|
||||||
|
CreateTime : req.CreateTime,
|
||||||
|
}
|
||||||
|
|
||||||
|
logicResp,err := logic.CreateOrder(thriftEntryInstance)
|
||||||
|
if err != nil {}
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
|
||||||
// http serve on 8000
|
// http serve on 8000
|
||||||
|
func HTTPGetOrderHandler(wr http.ResponseWriter, r *http.Request) {
|
||||||
|
httpEntryInstance := HTTPGetOrderEntry{
|
||||||
|
OrderID : req.OrderID,
|
||||||
|
ShopID : req.ShopID,
|
||||||
|
ProductID : req.ProductID,
|
||||||
|
CreateTime : req.CreateTime,
|
||||||
|
}
|
||||||
|
|
||||||
|
logicResp,err := logic.CreateOrder(thriftEntryInstance)
|
||||||
|
if err != nil {}
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
这样在对协议层进行修改时,就可以对 logic 层没有任何影响了。当然,如果完全按照 clean architecture 的设计来写代码着实有一些麻烦。
|
Loading…
x
Reference in New Issue
Block a user