func()

in internal/pkg/core/router/service.go [121:173]


func (rs *RouterService) createResourceHandler(resource artifacts.Resource) http.HandlerFunc {
	handler := func(w http.ResponseWriter, r *http.Request) {
		// Create message context
		msgContext := synctx.CreateMsgContext()

		// Set request body into message context properties
		msgContext.Properties["http_request_body"] = r.Body

		// Set path parameters into message context properties
		pathParamsMap := make(map[string]string)
		for _, pathParam := range resource.URITemplate.PathParameters {
			pathParamsMap[pathParam] = r.PathValue(pathParam)
		}
		msgContext.Properties["uriParams"] = pathParamsMap

		// Set query parameters into message context properties
		queryParams := r.URL.Query()

		// If there are predefined query parameters, map each to their corresponding variable
		if len(resource.URITemplate.QueryParameters) > 0 {
			// Create a map to store the variable mappings
			queryVarMap := make(map[string]string)

			// Loop through each predefined query parameter
			for paramName, varName := range resource.URITemplate.QueryParameters {
				// Get the value from the request
				if values, exists := queryParams[paramName]; exists && len(values) > 0 {
					// Map the query parameter value to the variable name
					queryVarMap[varName] = values[0]
				}
			}

			// Store the variable mapping in the message context
			msgContext.Properties["queryParams"] = queryVarMap
		}

		// Process through mediation pipeline
		success := resource.Mediate(msgContext)

		// Write response
		if success {
			for name, value := range msgContext.Headers {
				w.Header().Set(name, value)
			}
			if msgContext.Message.RawPayload != nil {
				w.Write(msgContext.Message.RawPayload)
			}
		} else {
			http.Error(w, "Internal server error", http.StatusInternalServerError)
		}
	}
	return handler
}