2017-07-11 50 views
5

Tôi cần triển khai dịch vụ web xà phòng bằng chức năng firebase. Tôi tìm thấy một mô-đun được gọi là soap-node soap-module-github có vẻ đầy hứa hẹn, vì nó tích hợp với express và firebase cho biết nó sử dụng Express cho các cuộc gọi http, nhưng vấn đề là tôi không biết cách tích hợp mô-đun đó với các hàm firebase từ đó, Hàm firebase là trình xử lý các cuộc gọi http được thực hiện bởi các máy khách, bất kỳ sự trợ giúp nào cũng rất hữu ích.Cách sử dụng Chức năng đám mây cho Firebase để viết dịch vụ SOAP?

Đây là mã tôi đã cố gắng tạo ra cho đến nay:

var fs = require('fs'), 
    soap = require('soap'), 
    express = require('express'), 
    lastReqAddress; 
    var server = express(); 
    service = { 
     StockQuoteService: { 
      StockQuotePort: { 
       GetLastTradePrice: function (args, cb, soapHeader) { 
        if (soapHeader) 
         return { 
          price: soapHeader.SomeToken 
         }; 
        if (args.tickerSymbol === 'trigger error') { 
         throw new Error('triggered server error'); 
        } else if (args.tickerSymbol === 'Async') { 
         return cb({ 
          price: 19.56 
         }); 
        } else if (args.tickerSymbol === 'SOAP Fault v1.2') { 
         throw { 
          Fault: { 
           Code: { 
            Value: "soap:Sender", 
            Subcode: { 
             value: "rpc:BadArguments" 
            } 
           }, 
           Reason: { 
            Text: "Processing Error" 
           } 
          } 
         }; 
        } else if (args.tickerSymbol === 'SOAP Fault v1.1') { 
         throw { 
          Fault: { 
           faultcode: "soap:Client.BadArguments", 
           faultstring: "Error while processing arguments" 
          } 
         }; 
        } else { 
         return { 
          price: 19.56 
         }; 
        } 
       }, 

       SetTradePrice: function (args, cb, soapHeader) {}, 

       IsValidPrice: function (args, cb, soapHeader, req) { 
        lastReqAddress = req.connection.remoteAddress; 

        var validationError = { 
         Fault: { 
          Code: { 
           Value: "soap:Sender", 
           Subcode: { 
            value: "rpc:BadArguments" 
           } 
          }, 
          Reason: { 
           Text: "Processing Error" 
          }, 
          statusCode: 500 
         } 
        }; 

        var isValidPrice = function() { 
         var price = args.price; 
         if (isNaN(price) || (price === ' ')) { 
          return cb(validationError); 
         } 

         price = parseInt(price, 10); 
         var validPrice = (price > 0 && price < Math.pow(10, 5)); 
         return cb(null, { 
          valid: validPrice 
         }); 
        }; 

        setTimeout(isValidPrice, 10); 
       } 
      } 
     } 
    }; 
    var wsdl = fs.readFileSync(__dirname + '/../wsdl/stockquote.wsdl', 'utf-8').toString(); 
    server = express(); 

    soapServer = soap.listen(server, '/stockquote', service, wsdl); 

đây là stockquote.wsdl:

<wsdl:definitions name="StockQuote" 
     targetNamespace="http://example.com/stockquote.wsdl" 
     xmlns:tns="http://example.com/stockquote.wsdl" 
     xmlns:xsd1="http://example.com/stockquote.xsd" 
     xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
     xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> 

<wsdl:types> 
    <xsd:schema targetNamespace="http://example.com/stockquote.xsd" xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"> 
     <xsd:element name="TradePriceRequest"> 
      <xsd:complexType> 
       <xsd:all> 
        <xsd:element name="tickerSymbol" type="string"/> 
       </xsd:all> 
      </xsd:complexType> 
     </xsd:element> 
     <xsd:element name="TradePrice"> 
      <xsd:complexType> 
       <xsd:all> 
        <xsd:element name="price" type="float"/> 
       </xsd:all> 
      </xsd:complexType> 
     </xsd:element> 
     <xsd:element name="TradePriceSubmit"> 
      <xsd:complexType> 
       <xsd:all> 
        <xsd:element name="tickerSymbol" type="string"/> 
        <xsd:element name="price" type="float"/> 
       </xsd:all> 
      </xsd:complexType> 
     </xsd:element> 
     <xsd:element name="valid" type="boolean"/> 
    </xsd:schema> 
</wsdl:types> 

<wsdl:message name="GetLastTradePriceInput"> 
    <wsdl:part name="body" element="xsd1:TradePriceRequest"/> 
</wsdl:message> 

<wsdl:message name="GetLastTradePriceOutput"> 
    <wsdl:part name="body" element="xsd1:TradePrice"/> 
</wsdl:message> 

<wsdl:message name="SetTradePriceInput"> 
    <wsdl:part name="body" element="xsd1:TradePriceSubmit"/> 
</wsdl:message> 

<wsdl:message name="IsValidPriceInput"> 
    <wsdl:part name="body" element="xsd1:TradePrice"/> 
</wsdl:message> 

<wsdl:message name="IsValidPriceOutput"> 
    <wsdl:part name="body" element="xsd1:valid"/> 
</wsdl:message> 

<wsdl:portType name="StockQuotePortType"> 
    <wsdl:operation name="GetLastTradePrice"> 
     <wsdl:input message="tns:GetLastTradePriceInput"/> 
     <wsdl:output message="tns:GetLastTradePriceOutput"/> 
    </wsdl:operation> 
    <wsdl:operation name="SetTradePrice"> 
     <wsdl:input message="tns:SetTradePriceInput"/> 
    </wsdl:operation> 
    <wsdl:operation name="IsValidPrice"> 
     <wsdl:input message="tns:IsValidPriceInput"/> 
     <wsdl:output message="tns:IsValidPriceOutput"/> 
    </wsdl:operation> 
</wsdl:portType> 

<wsdl:binding name="StockQuoteSoapBinding" type="tns:StockQuotePortType"> 
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> 
    <wsdl:operation name="GetLastTradePrice"> 
     <soap:operation soapAction="http://example.com/GetLastTradePrice"/> 
     <wsdl:input> 
      <soap:body use="literal"/> 
     </wsdl:input> 
     <wsdl:output> 
      <soap:body use="literal"/> 
     </wsdl:output> 
    </wsdl:operation> 
    <wsdl:operation name="SetTradePrice"> 
     <soap:operation soapAction="http://example.com/SetTradePrice"/> 
     <wsdl:input> 
      <soap:body use="literal"/> 
     </wsdl:input> 
    </wsdl:operation> 
    <wsdl:operation name="IsValidPrice"> 
     <soap:operation soapAction="http://example.com/IsValidPrice"/> 
     <wsdl:input> 
      <soap:body use="literal"/> 
     </wsdl:input> 
    </wsdl:operation> 
</wsdl:binding> 

<wsdl:service name="StockQuoteService"> 
    <wsdl:port name="StockQuotePort" binding="tns:StockQuoteSoapBinding"> 
     <soap:address location="http://localhost:5002/stockquote"/> 
    </wsdl:port> 
</wsdl:service> 

Tôi có googled rất tốt Tôi chỉ không tìm thấy một số đường dẫn, tôi cũng đã tìm kiếm các chức năng của google và tích hợp của chúng với xà phòng, vì các chức năng firebase chỉ là các chức năng đám mây của google được sử dụng cho firebase

+0

bạn có tìm thấy giải pháp này không? – faruk

Trả lời

2

Peeking vào mã nguồn cho node-soap, bạn sẽ có thể trực tiếp vượt qua _requestListener-onRequest chức năng Chức năng của Cloud:

exports.stockquote = functions.https.onRequest(soapServer._requestListener) 
+0

Tôi đã thử điều này và nhận được lỗi 'không thể đọc tài sản' đăng nhập 'của undefined' – faruk

1

Bạn có thể sử dụng express với các chức năng điện toán đám mây hiện nay:

server = express(); 
server.listen(5002, function() { 
    soap.listen(server, '/stockquote', service, wsdl); 
}); 

exports.stockquote = functions.https.onRequest(server); 

Đặt tuyến đường trong firebase.json:

"rewrites": [ 
    { 
    "source": "/stockquote", 
    "function": "stockquote" 
    } 
] 

Khi thử nghiệm trên ứng dụng khách bằng javascript don ' t quên thay đổi các thiết bị đầu cuối để ghi đè localhost trong wsdl:

var soap = require('soap'); 
var url = 'https://[your-project-id].firebaseapp.com/stockquote?wsdl'; 
var args = {tickerSymbol: 'some symbol', price: 100.0}; 

var options = { 
    'endpoint' : 'https://[your-project-id].firebaseapp.com/stockquote' 
}; 

soap.createClient(url, options, function(err, client) { 
    if (err) throw err; 
    //print service in json 
    console.log(client.describe()); 
    client.GetLastTradePrice(args, function(err, result) { 
     if(err) 
      console.log("err = "+ err.message); 
     console.log(result); 
     res.status(200).send(result); 
    }); 
}); 
2

Bạn đang trên con đường đúng đắn,

Nếu bạn muốn con đường GCF của bạn cho máy chủ được http://myfunctions.domain.com/stockquote/

sau đó cuối cùng của bạn dòng trong tệp js phải là soapServer = soap.listen(server, '/', service, wsdl) và sau đó, trong chức năng Google Cloud của bạn index.js nhập:

exports.stockquote = functions.https.onRequest(server)

Bạn sẽ phải đảm bảo rằng các yêu cầu SOAP của bạn sẽ đến điểm cuối với dấu gạch chéo ở cuối. Nếu bạn không có quyền kiểm soát của khách hàng hiện tại thì bạn có thể thêm vào trình xử lý URL của riêng bạn sẽ xem URL và thêm / vào URL mà chức năng của bạn nhận được.

tức là .: exports.stockquote = functions.https.onRequest(gcfURLHandler(server));

nơi gcfURLHandler được định nghĩa là

function gcfURLHandler(handler){ 
    return (req, res) => { 
     if(!req.url || !req.path) { 
      req.url = "/" + (req.url || ''); 
     } 
     handler(req, res) 
    } 
} 

figured này ra từ một bình luận here. (cũng có các mẹo khác trong mã gốc)

Tôi đã làm việc trong tuần trước và thấy câu hỏi chưa được trả lời.Mất rất nhiều đào xung quanh để cuối cùng con số nó ra. Hy vọng rằng điều này sẽ giúp những người khác tìm cách làm tương tự!

Các vấn đề liên quan