李伟乐 2 years ago
parent ed2c180cc9
commit 802200c4a7
  1. 78
      example/main.go
  2. 1
      go.mod
  3. 6
      middleware/jwt/jwt.go
  4. 4
      transport/http/context.go

@ -5,19 +5,30 @@ import (
"flag" "flag"
"fmt" "fmt"
"git.diulo.com/mogfee/kit" "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/transport/http" "git.diulo.com/mogfee/kit/transport/http"
http2 "net/http"
) )
var host string var host string
func init() { func init() {
flag.StringVar(&host, "h", "localhost:9093", "") flag.StringVar(&host, "h", "localhost:9922", "")
} }
type cros struct {
h http2.Handler
}
func (c *cros) ServeHTTP(writer http2.ResponseWriter, request *http2.Request) {
request.Header.Set("Access-Control-Allow-Origin", "*")
request.Header.Set("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,PATCH,DELETE")
request.Header.Set("Access-Control-Allow-Credentials", "true")
request.Header.Set("Access-Control-Allow-Headers", "Content-Type,X-Requested-With,Access-Control-Allow-Credentials,User-Agent,Content-Length,Authorization")
request.Header.Set("xxxxx", "11")
fmt.Println("xxx")
c.h.ServeHTTP(writer, request)
}
func main() { func main() {
flag.Parse() flag.Parse()
runApp(host) runApp(host)
@ -25,32 +36,37 @@ func main() {
func runApp(host string) { func runApp(host string) {
hs := http.NewServer( hs := http.NewServer(
http.Address(host), http.Address(host),
http.Middleware( //http.Middleware(
logging.Server(), //logging.Server(),
validate.Server(), //validate.Server(),
jwt.JWT(), //jwt.JWT(),
), //),
http.Filter(func(handler http2.Handler) http2.Handler {
return &cros{
h: handler,
}
}),
) )
//route := hs.Route("/") route := hs.Route("/")
//route.GET("/", func(ctx http.Context) error { route.HEAD("/api/v1/answer/listCategory", func(ctx http.Context) error {
// in := UserAddRequest{Name: "tom"} in := UserAddRequest{Name: "tom"}
// http.SetOperation(ctx, "/api/abc") http.SetOperation(ctx, "/api/abc")
// h := ctx.Middleware(func(ctx context.Context, a any) (any, error) { h := ctx.Middleware(func(ctx context.Context, a any) (any, error) {
// return AddUser(ctx, a.(*UserAddRequest)) return AddUser(ctx, a.(*UserAddRequest))
// }) })
// if tr, ok := transport.FromServerContext(ctx); ok { //if tr, ok := transport.FromServerContext(ctx); ok {
// fmt.Println(tr.Operation()) // fmt.Println(tr.Operation())
// } //}
// out, err := h(ctx, &in) out, err := h(ctx, &in)
// if err != nil { if err != nil {
// return err return err
// } }
// reply, _ := out.(*UserAddResponse) reply, _ := out.(*UserAddResponse)
// reply.Id = host reply.Id = host
// return ctx.Result(200, reply) return ctx.Result(200, reply)
//}) })
user.RegisterUserHTTPServer(hs, &service.UserService{}) //user.RegisterUserHTTPServer(hs, &service.UserService{})
//client, err := clientv3.New(clientv3.Config{ //client, err := clientv3.New(clientv3.Config{
// Endpoints: []string{"127.0.0.1:2379"}, // Endpoints: []string{"127.0.0.1:2379"},
//}) //})
@ -76,8 +92,8 @@ type UserAddResponse struct {
} }
func AddUser(ctx context.Context, request *UserAddRequest) (*UserAddResponse, error) { func AddUser(ctx context.Context, request *UserAddRequest) (*UserAddResponse, error) {
fmt.Println(jwt.FromUserContext(ctx)) //fmt.Println(jwt.FromUserContext(ctx))
fmt.Println(jwt.FromAuthKeyContext(ctx)) //fmt.Println(jwt.FromAuthKeyContext(ctx))
return &UserAddResponse{Id: request.Name}, nil return &UserAddResponse{Id: request.Name}, nil
//errors.New(500, "xx", "") //errors.New(500, "xx", "")
} }

@ -18,7 +18,6 @@ require (
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/coreos/go-semver v0.3.0 // indirect github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/gin-contrib/sse v0.1.0 // indirect github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect

@ -101,5 +101,9 @@ func SetAuthKeyContext(ctx context.Context, key string) context.Context {
return context.WithValue(ctx, authKey{}, key) return context.WithValue(ctx, authKey{}, key)
} }
func FromAuthKeyContext(ctx context.Context) string { func FromAuthKeyContext(ctx context.Context) string {
return ctx.Value(authKey{}).(string) v := ctx.Value(authKey{})
if v == nil {
return ""
}
return v.(string)
} }

@ -158,6 +158,10 @@ func (w *wrapper) Returns(i interface{}, err error) error {
func (w *wrapper) Result(i int, i2 interface{}) error { func (w *wrapper) Result(i int, i2 interface{}) error {
w.w.WriteHeader(i) w.w.WriteHeader(i)
w.res.Header().Set("Access-Control-Allow-Origin", "*")
w.res.Header().Set("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,PATCH,DELETE")
w.res.Header().Set("Access-Control-Allow-Credentials", "true")
w.res.Header().Set("Access-Control-Allow-Headers", "Content-Type,X-Requested-With,Access-Control-Allow-Credentials,User-Agent,Content-Length,Authorization")
return w.router.srv.enc(&w.w, w.req, i2) return w.router.srv.enc(&w.w, w.req, i2)
} }

Loading…
Cancel
Save