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.
85 lines
1.9 KiB
85 lines
1.9 KiB
package main |
|
|
|
import ( |
|
"context" |
|
"flag" |
|
"fmt" |
|
"git.diulo.com/mogfee/kit" |
|
user "git.diulo.com/mogfee/kit/api" |
|
"git.diulo.com/mogfee/kit/example/service" |
|
"git.diulo.com/mogfee/kit/middleware/jwt" |
|
"git.diulo.com/mogfee/kit/middleware/logging" |
|
"git.diulo.com/mogfee/kit/middleware/validate" |
|
"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(), |
|
jwt.JWT(), |
|
), |
|
) |
|
//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)) |
|
// }) |
|
// if tr, ok := transport.FromServerContext(ctx); ok { |
|
// fmt.Println(tr.Operation()) |
|
// } |
|
// 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) { |
|
fmt.Println(jwt.FromUserContext(ctx)) |
|
fmt.Println(jwt.FromAuthKeyContext(ctx)) |
|
return &UserAddResponse{Id: request.Name}, nil |
|
//errors.New(500, "xx", "") |
|
}
|
|
|