You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

78 lines
1.7 KiB

package main
import (
"context"
"flag"
"fmt"
"git.diulo.com/mogfee/kit"
"git.diulo.com/mogfee/kit/example/service"
"git.diulo.com/mogfee/kit/middleware/logging"
"git.diulo.com/mogfee/kit/middleware/validate"
user "git.diulo.com/mogfee/kit/proto/v1"
"git.diulo.com/mogfee/kit/registry/etcd"
"git.diulo.com/mogfee/kit/transport/http"
clientv3 "go.etcd.io/etcd/client/v3"
)
var host string
func init() {
flag.StringVar(&host, "h", "localhost:9093", "")
}
func main() {
flag.Parse()
runApp(host)
}
func runApp(host string) {
hs := http.NewServer(
http.Address(host),
http.Middleware(
logging.Server(),
validate.Server(),
),
)
route := hs.Route("/")
route.GET("/", func(ctx http.Context) error {
in := UserAddRequest{Name: "tom"}
http.SetOperation(ctx, "/api/abc")
h := ctx.Middleware(func(ctx context.Context, a any) (any, error) {
return AddUser(ctx, a.(*UserAddRequest))
})
out, err := h(ctx, &in)
if err != nil {
return err
}
reply, _ := out.(*UserAddResponse)
reply.Id = host
return ctx.Result(200, reply)
})
user.RegisterUserHTTPServer(hs, &service.UserService{})
client, err := clientv3.New(clientv3.Config{
Endpoints: []string{"127.0.0.1:2379"},
})
if err != nil {
panic(err)
}
app := kit.New(
kit.Name("kit-server"),
kit.Version("v1.0"),
kit.ID(host),
kit.Server(hs),
kit.Registrar(etcd.New(client)),
)
fmt.Println(app.Run())
fmt.Println(app.Stop())
}
type UserAddRequest struct {
Name string
}
type UserAddResponse struct {
Id string
}
func AddUser(ctx context.Context, request *UserAddRequest) (*UserAddResponse, error) {
return &UserAddResponse{Id: request.Name}, nil
//errors.New(500, "xx", "")
}