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.
 
 

113 lines
3.0 KiB

package main
import (
"context"
"flag"
"fmt"
"git.diulo.com/mogfee/kit"
"git.diulo.com/mogfee/kit/middleware"
"git.diulo.com/mogfee/kit/transport"
"git.diulo.com/mogfee/kit/transport/http"
http2 "net/http"
)
var host string
func init() {
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() {
flag.Parse()
runApp(host)
}
func runApp(host string) {
hs := http.NewServer(
http.Address(host),
//http.Middleware(
//logging.Server(),
//validate.Server(),
//jwt.JWT(),
//),
//http.Filter(func(handler http2.Handler) http2.Handler {
// return &cros{
// h: handler,
// }
//}),
http.Middleware(func(handler middleware.Handler) middleware.Handler {
return func(ctx context.Context, a any) (any, error) {
if tr, ok := transport.FromServerContext(ctx); ok {
header := tr.ReplyHeader()
header.Set("Access-Control-Allow-Origin", "*")
header.Set("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,PATCH,DELETE")
header.Set("Access-Control-Allow-Credentials", "true")
header.Set("Access-Control-Allow-Headers", "Content-Type,X-Requested-With,Access-Control-Allow-Credentials,User-Agent,Content-Length,Authorization")
}
return handler(ctx, a)
}
}),
)
route := hs.Route("/")
route.GET("/api/v1/answer/listCategory", 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) {
//tr, _ := transport.FromServerContext(ctx)
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) {
//fmt.Println(jwt.FromUserContext(ctx))
//fmt.Println(jwt.FromAuthKeyContext(ctx))
return &UserAddResponse{Id: request.Name}, nil
//errors.New(500, "xx", "")
}