add bookstore example

This commit is contained in:
kevin
2020-09-03 23:26:04 +08:00
parent 167422ac4f
commit 11dd3d75ec
44 changed files with 1741 additions and 121 deletions

View File

@@ -0,0 +1,33 @@
type (
addReq struct {
book string `form:"book"`
price int64 `form:"price"`
}
addResp struct {
ok bool `json:"ok"`
}
)
type (
checkReq struct {
book string `form:"book"`
}
checkResp struct {
found bool `json:"found"`
price int64 `json:"price"`
}
)
service bookstore-api {
@server(
handler: AddHandler
)
get /add(addReq) returns(addResp)
@server(
handler: CheckHandler
)
get /check(checkReq) returns(checkResp)
}

View File

@@ -0,0 +1,27 @@
package main
import (
"bookstore/api/internal/config"
"bookstore/api/internal/handler"
"bookstore/api/internal/svc"
"flag"
"github.com/tal-tech/go-zero/core/conf"
"github.com/tal-tech/go-zero/rest"
)
var configFile = flag.String("f", "etc/bookstore-api.yaml", "the config file")
func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
ctx := svc.NewServiceContext(c)
server := rest.MustNewServer(c.RestConf)
defer server.Stop()
handler.RegisterHandlers(server, ctx)
server.Start()
}

View File

@@ -0,0 +1,13 @@
Name: bookstore-api
Host: 0.0.0.0
Port: 8888
Add:
Etcd:
Hosts:
- localhost:2379
Key: add.rpc
Check:
Etcd:
Hosts:
- localhost:2379
Key: check.rpc

View File

@@ -0,0 +1,12 @@
package config
import (
"github.com/tal-tech/go-zero/rest"
"github.com/tal-tech/go-zero/rpcx"
)
type Config struct {
rest.RestConf
Add rpcx.RpcClientConf
Check rpcx.RpcClientConf
}

View File

@@ -0,0 +1,28 @@
package handler
import (
"bookstore/api/internal/logic"
"bookstore/api/internal/svc"
"bookstore/api/internal/types"
"net/http"
"github.com/tal-tech/go-zero/rest/httpx"
)
func addHandler(ctx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.AddReq
if err := httpx.Parse(r, &req); err != nil {
httpx.Error(w, err)
return
}
l := logic.NewAddLogic(r.Context(), ctx)
resp, err := l.Add(req)
if err != nil {
httpx.Error(w, err)
} else {
httpx.WriteJson(w, http.StatusOK, resp)
}
}
}

View File

@@ -0,0 +1,28 @@
package handler
import (
"bookstore/api/internal/logic"
"bookstore/api/internal/svc"
"bookstore/api/internal/types"
"net/http"
"github.com/tal-tech/go-zero/rest/httpx"
)
func checkHandler(ctx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.CheckReq
if err := httpx.Parse(r, &req); err != nil {
httpx.Error(w, err)
return
}
l := logic.NewCheckLogic(r.Context(), ctx)
resp, err := l.Check(req)
if err != nil {
httpx.Error(w, err)
} else {
httpx.WriteJson(w, http.StatusOK, resp)
}
}
}

View File

@@ -0,0 +1,24 @@
// DO NOT EDIT, generated by goctl
package handler
import (
"bookstore/api/internal/svc"
"net/http"
"github.com/tal-tech/go-zero/rest"
)
func RegisterHandlers(engine *rest.Server, serverCtx *svc.ServiceContext) {
engine.AddRoutes([]rest.Route{
{
Method: http.MethodGet,
Path: "/add",
Handler: addHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/check",
Handler: checkHandler(serverCtx),
},
})
}

View File

@@ -0,0 +1,38 @@
package logic
import (
"bookstore/api/internal/svc"
"bookstore/api/internal/types"
"bookstore/rpc/add/adder"
"context"
"github.com/tal-tech/go-zero/core/logx"
)
type AddLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) AddLogic {
return AddLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *AddLogic) Add(req types.AddReq) (*types.AddResp, error) {
resp, err := l.svcCtx.Adder.Add(l.ctx, &adder.AddReq{
Book: req.Book,
Price: req.Price,
})
if err != nil {
return nil, err
}
return &types.AddResp{
Ok: resp.Ok,
}, nil
}

View File

@@ -0,0 +1,38 @@
package logic
import (
"bookstore/api/internal/svc"
"bookstore/api/internal/types"
"bookstore/rpc/check/checker"
"context"
"github.com/tal-tech/go-zero/core/logx"
)
type CheckLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewCheckLogic(ctx context.Context, svcCtx *svc.ServiceContext) CheckLogic {
return CheckLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *CheckLogic) Check(req types.CheckReq) (*types.CheckResp, error) {
resp, err := l.svcCtx.Checker.Check(l.ctx, &checker.CheckReq{
Book: req.Book,
})
if err != nil {
return nil, err
}
return &types.CheckResp{
Found: resp.Found,
Price: resp.Price,
}, nil
}

View File

@@ -0,0 +1,23 @@
package svc
import (
"bookstore/api/internal/config"
"bookstore/rpc/add/adder"
"bookstore/rpc/check/checker"
"github.com/tal-tech/go-zero/rpcx"
)
type ServiceContext struct {
Config config.Config
Adder adder.Adder
Checker checker.Checker
}
func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
Adder: adder.NewAdder(rpcx.MustNewClient(c.Add)),
Checker: checker.NewChecker(rpcx.MustNewClient(c.Check)),
}
}

View File

@@ -0,0 +1,20 @@
// DO NOT EDIT, generated by goctl
package types
type AddReq struct {
Book string `form:"book"`
Price int64 `form:"price"`
}
type AddResp struct {
Ok bool `json:"ok"`
}
type CheckReq struct {
Book string `form:"book"`
}
type CheckResp struct {
Found bool `json:"found"`
Price int64 `json:"price"`
}