示例#1
0
文件: example.go 项目: wtertius/gorpc
func main() {
	hm := gorpc.NewHandlersManager("github.com/sergei-svistunov/gorpc", gorpc.HandlersManagerCallbacks{})

	hm.MustRegisterHandler(test_handler1.NewHandler())

	// API
	http.Handle("/", http_json.NewAPIHandler(hm, nil, http_json.APIHandlerCallbacks{
		OnError: func(ctx context.Context, w http.ResponseWriter, req *http.Request, resp interface{}, err *gorpc.CallHandlerError) {
			log.Println(err.Error())
		},
		OnPanic: func(ctx context.Context, w http.ResponseWriter, r interface{}, trace []byte, req *http.Request) {
			log.Println(r, "\n", string(trace))
		},
	}))

	// Docs
	http.Handle("/swagger.json", http_json.NewSwaggerJSONHandler(hm, 0, http_json.SwaggerJSONCallbacks{}))
	http.Handle("/docs/", http.StripPrefix("/docs", swagger_ui.NewHTTPHandler()))

	// Client SDK
	http.Handle("/client.go", http_json_adapter.NewHandler(hm))

	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		log.Fatal(err)
	}

	//	e := client.NewExample(nil, nil, client.Callbacks{})
	//	_, err = e.TestHandler1V1(context.Background(), client.TestHandler1V1Args{})
	//	if apiErr, ok := err.(*client.ServiceError); ok {
	//		log.Println(apiErr.Code, apiErr.Error())
	//	} else {
	//		panic(err)
	//	}
}
示例#2
0
func main() {
	hm := gorpc.NewHandlersManager("github.com/sergei-svistunov/gorpc", gorpc.HandlersManagerCallbacks{}, &DummyCache{}, 0)

	if err := hm.RegisterHandler(test_handler1.NewHandler()); err != nil {
		panic(err)
	}

	http.Handle("/", http_json.NewAPIHandler(hm, http_json.APIHandlerCallbacks{}))
	http.Handle("/swagger.json", http_json.NewSwaggerJSONHandler(hm))
	http.Handle("/docs/", http.StripPrefix("/docs", swagger_ui.NewHTTPHandler()))

	http.ListenAndServe(":8080", nil)
}
示例#3
0
// Benchmarks
func BenchmarkHttpJSON_CallWithRequiredArguments_Success(b *testing.B) {
	hm := gorpc.NewHandlersManager("github.com/sergei-svistunov/gorpc", gorpc.HandlersManagerCallbacks{})
	if err := hm.RegisterHandler(test_handler1.NewHandler()); err != nil {
		b.Fatal(err.Error())
	}

	handler := NewAPIHandler(hm, cache.NewMapCache(), APIHandlerCallbacks{})
	request, _ := http.NewRequest("GET", "/test/handler1/v1/?req_int=123", nil)
	recorder := httptest.NewRecorder()

	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		handler.ServeHTTP(recorder, request)
	}
}
func TestHandlerManager_PrepareParameters_V6(t *testing.T) {
	hm := gorpc.NewHandlersManager("github.com/sergei-svistunov/gorpc", gorpc.HandlersManagerCallbacks{})
	hm.RegisterHandler(handler1.NewHandler())

	jsonReq := `
		{
			"f1": [
				[
					{
						"f11": {
							"f111": "test"
						}
					}
				]
			]
		}`

	var jsonValues map[string]interface{}
	if err := json.Unmarshal([]byte(jsonReq), &jsonValues); err != nil {
		t.Fatal(err)
	}

	pg := &JsonParametersGetter{
		values: jsonValues,
	}

	hanlderVersion := hm.FindHandler("/test/handler1", 6)
	if hanlderVersion == nil {
		t.Fatal("Handler wasn't found")
	}

	v, err := hm.UnmarshalParameters(context.TODO(), hanlderVersion, pg)
	if err != nil {
		t.Fatal(err)
	}

	arr := *(v.Interface().(*handler1.V6Request).F1)
	if *(arr[0][0].F11.F111) != "test" {
		t.Fatalf("Error in parsing V6")
	}
}
func TestHandlerManager_PrepareParameters_SliceInSlice(t *testing.T) {
	hm := gorpc.NewHandlersManager("github.com/sergei-svistunov/gorpc", gorpc.HandlersManagerCallbacks{})
	hm.RegisterHandler(handler1.NewHandler())

	pg := &JsonParametersGetter{
		values: map[string]interface{}{
			"slice_in_slice": []interface{}{
				[]interface{}{
					map[string]interface{}{
						"f1": json.Number("1"),
					},
					map[string]interface{}{
						"f1": json.Number("2"),
						"f2": json.Number("20"),
					},
					map[string]interface{}{
						"f1": json.Number("3"),
						"f2": nil,
					},
				},
			},
		},
	}

	hanlderVersion := hm.FindHandler("/test/handler1", 4)
	if hanlderVersion == nil {
		t.Fatal("Handler wasn't found")
	}

	v, err := hm.UnmarshalParameters(context.TODO(), hanlderVersion, pg)
	if err != nil {
		t.Fatal(err)
	}

	if v.Interface().(*handler1.V4Request).SliceInSlice[0][1].F1 != 2 {
		t.Fatalf("Error in parsing slice in slice")
	}
}
示例#6
0
func main() {
	hm := gorpc.NewHandlersManager("github.com/sergei-svistunov/gorpc-tutor/server/handlers", gorpc.HandlersManagerCallbacks{})

	hm.MustRegisterHandlers(
		handlerHelloWorld.NewHandler(),
		handlerCalcSum.NewHandler(),
	)

	// Base API location
	http.Handle("/", http_json.NewAPIHandler(hm, nil, http_json.APIHandlerCallbacks{}))

	// Docs
	http.Handle("/swagger.json", http_json.NewSwaggerJSONHandler(hm, 0, http_json.SwaggerJSONCallbacks{}))
	http.Handle("/docs/", http.StripPrefix("/docs", swagger_ui.NewHTTPHandler()))

	// Client SDK
	http.Handle("/client.go", adapter.NewHandler(hm))

	if err := http.ListenAndServe(":8080", nil); err != nil {
		panic(err)
	}

}
示例#7
0
func (s *HttpJSONSute) SetupTest() {
	hm := gorpc.NewHandlersManager("github.com/sergei-svistunov/gorpc", gorpc.HandlersManagerCallbacks{})
	s.NoError(hm.RegisterHandler(test_handler1.NewHandler()))

	s.server = httptest.NewUnstartedServer(NewAPIHandler(hm, cache.NewMapCache(), APIHandlerCallbacks{}))
}
示例#8
0
func (s *SwaggerJSONSute) SetupTest() {
	hm := gorpc.NewHandlersManager("github.com/sergei-svistunov/gorpc", gorpc.HandlersManagerCallbacks{})
	s.NoError(hm.RegisterHandler(test_handler1.NewHandler()))

	s.server = httptest.NewUnstartedServer(NewSwaggerJSONHandler(hm, 0, SwaggerJSONCallbacks{}))
}