跳轉到內容

Ada 程式設計/庫/Web/AWS

來自 Wikibooks,開放世界中的開放書籍

Ada. Time-tested, safe and secure.
Ada. 經久考驗,安全可靠。

AWS,Ada Web 伺服器,是一個完整的框架,用於開發基於 Web 的應用程式。AWS 的主要部分是嵌入式Web 伺服器。這個小巧但功能強大的 Web 伺服器可以嵌入到您的應用程式中,使其能夠與標準 Web 瀏覽器進行通訊。圍繞這個 Web 伺服器,開發了許多服務。

AWS 支援SOAPWeb 服務REST架構。

示例程式碼

[編輯 | 編輯原始碼]

Hello World

[編輯 | 編輯原始碼]

AWS 的著名 Hello World 演示,一個完整的 Web 伺服器,它將為對localhost8080的每個請求顯示“Hello world!”。

with AWS.Default;
with AWS.Response;
with AWS.Server;
with AWS.Status;

procedure Hello_World is

   WS : AWS.Server.HTTP;

   function HW_CB (Request : AWS.Status.Data) return AWS.Response.Data is
   begin
      return AWS.Response.Build ("text/html", "Hello world !");
   end HW_CB;

begin
   AWS.Server.Start (WS, "Hello World", Callback => HW_CB'Access);

   delay 60.0;

   AWS.Server.Shutdown (WS);
end Hello_World;

設定伺服器配置並等待事件

[編輯 | 編輯原始碼]

可以使用記錄傳遞伺服器的配置引數。還可以使用 AWS 上的內建過程來等待事件。

callbacks.adb

package body Callbacks is

   function HW_CB (Request : AWS.Status.Data) return AWS.Response.Data is
   begin
      return AWS.Response.Build ("text/html", "Hello world !");
   end HW_CB;

end Callbacks;

callbacks.ads

with AWS.Status;
with AWS.Response;

package Callbacks is

   function HW_CB (Request : AWS.Status.Data) return AWS.Response.Data;

end Callbacks;

main.adb

with AWS.Config.Set;
with AWS.Server;

procedure Main is
  use AWS;

  Host : constant String := "localhost";
  Port : constant        := 8080;

  Web_Server : Server.HTTP;
  Web_Config : Config.Object;

begin
   -- Setup

   Config.Set.Server_Host (Web_Config, Host);
   Config.Set.Server_Port (Web_Config, Port);

   -- Start the server

   Server.Start (Web_Server => Web_Server,
                 Callback => Callbacks.HW_CB'Access,
                 Config => Web_Config);

   -- Wait for the Q key

   Server.Wait (Server.Q_Key_Pressed);

   -- Stop the server

   Server.Shutdown (Web_Server);
end Main;

bitcoind JSON-RPC 互動。

bitcoin.adb

with AWS.Client;
with AWS.Headers;
with AWS.Headers.Set;
with AWS.Response;

package body Bitcoin is

   function Get_Wallet_Info return AWS.Response.Data is
      hdrs : AWS.Headers.List := AWS.Headers.Empty_List;
   begin
      AWS.Headers.Set.Add(hdrs, "Content-Type", "text/plain");
      return AWS.Client.Post(URL => "http://127.0.0.1:8332/",
                      Data => "{""jsonrpc"": ""1.0"", ""id"":""test"", ""method"": ""getwalletinfo"", ""params"": []}",
                      User => "bitcoinrpcUSERNAME",
                      Pwd => "bitcoinrpcPASSWORD",
                      Headers => hdrs);
   end Get_Wallet_Info;

end Bitcoin;

透過開啟 Bitcoin Core 並點選選項視窗上的對應按鈕來建立 bitcoin.conf 檔案。以下是示例配置檔案。然後重新開啟 bitcoin-qt 或啟動 bitcoind 守護程序以啟動伺服器。bitcoin-cli 程式和測試網路可用於測試 RPC 命令。

bitcoin.conf

# Expose the RPC/JSON API
server=1
rpcuser=USERNAME
rpcpassword=PASSWORD

另請參閱

[編輯 | 編輯原始碼]

維基百科

[編輯 | 編輯原始碼]
[編輯 | 編輯原始碼]
專案資訊
https://github.com/adacore/aws/
下載
https://github.com/adacore/aws/releases


華夏公益教科書