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.

79 lines
1.7 KiB

2 years ago
package main
import (
2 years ago
"context"
2 years ago
"flag"
2 years ago
"fmt"
2 years ago
"git.diulo.com/mogfee/kit"
2 years ago
user "git.diulo.com/mogfee/kit/api"
2 years ago
"git.diulo.com/mogfee/kit/example/service"
"git.diulo.com/mogfee/kit/middleware/logging"
2 years ago
"git.diulo.com/mogfee/kit/middleware/validate"
"git.diulo.com/mogfee/kit/registry/etcd"
2 years ago
"git.diulo.com/mogfee/kit/transport/http"
2 years ago
clientv3 "go.etcd.io/etcd/client/v3"
2 years ago
)
2 years ago
var host string
func init() {
flag.StringVar(&host, "h", "localhost:9093", "")
}
2 years ago
func main() {
2 years ago
flag.Parse()
runApp(host)
}
func runApp(host string) {
2 years ago
hs := http.NewServer(
2 years ago
http.Address(host),
2 years ago
http.Middleware(
logging.Server(),
2 years ago
validate.Server(),
2 years ago
),
)
route := hs.Route("/")
2 years ago
route.GET("/", func(ctx http.Context) error {
2 years ago
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)
2 years ago
reply.Id = host
2 years ago
return ctx.Result(200, reply)
})
user.RegisterUserHTTPServer(hs, &service.UserService{})
2 years ago
client, err := clientv3.New(clientv3.Config{
Endpoints: []string{"127.0.0.1:2379"},
})
if err != nil {
panic(err)
}
2 years ago
app := kit.New(
kit.Name("kit-server"),
kit.Version("v1.0"),
2 years ago
kit.ID(host),
2 years ago
kit.Server(hs),
2 years ago
kit.Registrar(etcd.New(client)),
2 years ago
)
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) {
2 years ago
return &UserAddResponse{Id: request.Name}, nil
//errors.New(500, "xx", "")
2 years ago
}