Beispiel #1
0
func (f *Frontend) registerMethod(method kit.Method) {
	f.client.Register(method.GetName(), func(args []interface{}, kwargs map[string]interface{}, details map[string]interface{}) (result *turnpike.CallResult) {
		methodName := method.GetName()
		fmt.Printf("WAMP method %v |\n data: %v |\n details: %v\n", methodName, kwargs, details)

		request := kit.NewRequest()
		request.SetFrontend("wamp")
		request.SetPath("/method/" + methodName)
		request.SetData(kwargs)

		var response kit.Response

		// Find session.
		sessionId := uint(details["session_id"].(turnpike.ID))
		session := f.sessions[sessionId]
		if session == nil {
			s, err := f.registry.UserService().StartSession(nil, "wamp")
			if err != nil {
				response = kit.NewErrorResponse(err)
			} else {
				f.sessions[sessionId] = s
				session = s
			}
		}

		request.SetSession(session)
		if session.GetUser() != nil {
			request.SetUser(session.GetUser())
		}

		// Run before middlewares.
		for _, middleware := range f.beforeMiddlewares {
			resp, skip := middleware(f.registry, request)
			if skip {
				panic("WAMP frontend middlewares do not support skipping.")
			} else if resp != nil {
				response = resp
				break
			}
		}

		// Run the method.
		if response == nil {
			responder := func(r kit.Response) {
				response = r
			}

			finishedChannel, err := f.registry.App().RunMethod(methodName, request, responder, true)
			if err != nil {
				response = kit.NewErrorResponse(err)
			} else {
				<-finishedChannel
			}
		}

		// Run after middlewares.
		for _, middleware := range f.afterMiddlewares {
			resp, skip := middleware(f.registry, request, response)
			if skip {
				panic("WAMP frontend middlewares do not support skipping.")
			}
			if resp != nil {
				response = resp
			}
		}

		return &turnpike.CallResult{Kwargs: response.GetData().(map[string]interface{})}
	}, nil)
}
Beispiel #2
0
func HttpHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params, registry kit.Registry, handler kit.RequestHandler) {
	config := registry.Config()
	header := w.Header()

	// Set Access-Control headers.
	allowedOrigins := config.UString("accessControl.allowedOrigins", "*")
	header.Set("Access-Control-Allow-Origin", allowedOrigins)

	methods := config.UString("accessControl.allowedMethods", "GET, POST, PUT, DELETE, OPTIONS, PATCH")
	header.Set("Access-Control-Allow-Methods", methods)

	allowedHeaders := config.UString("accessControl.allowedHeaders", "Authentication, Content-Type, X-Requested-With, Accept, Accept-Language, Content-Language")
	header.Set("Access-Control-Allow-Headers", allowedHeaders)

	// If it is an options request, just respond with 200.
	if r.Method == "OPTIONS" {
		w.WriteHeader(200)
		return
	}

	request := kit.NewRequest()
	request.SetFrontend("http")
	request.SetPath(r.URL.String())
	request.SetHttpMethod(r.Method)
	request.SetHttpRequest(r)
	request.SetHttpResponseWriter(w)

	for _, param := range params {
		request.Context.Set(param.Key, param.Value)
	}

	queryVals := r.URL.Query()
	for key := range queryVals {
		vals := queryVals[key]
		if len(vals) == 1 {
			request.Context.Set(key, vals[0])
		} else {
			request.Context.Set(key, vals)
		}
	}

	response, skip := processRequest(registry, request, handler)
	if skip {
		return
	}

	// If a data reader is set, write the data of the reader.
	reader := response.GetRawDataReader()
	if reader != nil {
		w.WriteHeader(response.GetHttpStatus())
		io.Copy(w, reader)
		reader.Close()
		return
	}

	// If raw data is set, write the raw data.
	rawData := response.GetRawData()
	if rawData != nil {
		w.WriteHeader(response.GetHttpStatus())
		w.Write(rawData)
		return
	}

	registry.Logger().Panicf("Invalid response with no raw data: %+v", response)
}