add bookstore example
This commit is contained in:
33
example/bookstore/api/bookstore.api
Normal file
33
example/bookstore/api/bookstore.api
Normal 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)
|
||||
}
|
||||
27
example/bookstore/api/bookstore.go
Normal file
27
example/bookstore/api/bookstore.go
Normal 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()
|
||||
}
|
||||
13
example/bookstore/api/etc/bookstore-api.yaml
Normal file
13
example/bookstore/api/etc/bookstore-api.yaml
Normal 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
|
||||
12
example/bookstore/api/internal/config/config.go
Normal file
12
example/bookstore/api/internal/config/config.go
Normal 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
|
||||
}
|
||||
28
example/bookstore/api/internal/handler/addhandler.go
Normal file
28
example/bookstore/api/internal/handler/addhandler.go
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
28
example/bookstore/api/internal/handler/checkhandler.go
Normal file
28
example/bookstore/api/internal/handler/checkhandler.go
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
24
example/bookstore/api/internal/handler/routes.go
Normal file
24
example/bookstore/api/internal/handler/routes.go
Normal 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),
|
||||
},
|
||||
})
|
||||
}
|
||||
38
example/bookstore/api/internal/logic/addlogic.go
Normal file
38
example/bookstore/api/internal/logic/addlogic.go
Normal 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
|
||||
}
|
||||
38
example/bookstore/api/internal/logic/checklogic.go
Normal file
38
example/bookstore/api/internal/logic/checklogic.go
Normal 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
|
||||
}
|
||||
23
example/bookstore/api/internal/svc/servicecontext.go
Normal file
23
example/bookstore/api/internal/svc/servicecontext.go
Normal 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)),
|
||||
}
|
||||
}
|
||||
20
example/bookstore/api/internal/types/types.go
Normal file
20
example/bookstore/api/internal/types/types.go
Normal 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"`
|
||||
}
|
||||
Reference in New Issue
Block a user