Ada Programming/Libraries/Web/AWS

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Ada. Time-tested, safe and secure.

AWS is a complete framework to develop Web based applications. The main part of the framework is the embedded web server. This small yet powerful Web server can be embedded into your application so your application will be able to talk with a standard Web browser like Microsoft Internet Explorer or Firefox for example. Around this Web server a lot of services have been developed.

AWS supports SOAP Web Services.

See also: AWS web site

Sample code[edit | edit source]

Hello World[edit | edit source]

The famous Hello World demo for AWS, a complete Web server that will display "Hello world!" for every requests made to localhost on port 8080.

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
      return AWS.Response.Build ("text/html", "Hello world !");
   end HW_CB;

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

   delay 60.0;

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

Setting server configuration and waiting for an event[edit | edit source]

It is possible to pass configuration parameters for the server using a record. It is also possible to use a built-in procedure on AWS to wait for an event.


package body Callbacks is

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

end Callbacks;

with AWS.Status;
with AWS.Response;

package Callbacks is

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

end Callbacks;


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;

   -- 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;

REST[edit | edit source]

Interface with bitcoind JSON-RPC.


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;
      AWS.Headers.Set.Add(hdrs, "Content-Type", "text/plain");
      return AWS.Client.Post(URL => "",
                      Data => "{""jsonrpc"": ""1.0"", ""id"":""test"", ""method"": ""getwalletinfo"", ""params"": []}",
                      User => "bitcoinrpcUSERNAME",
                      Pwd => "bitcoinrpcPASSWORD",
                      Headers => hdrs);
   end Get_Wallet_Info;

end Bitcoin;

Create the bitcoin.conf file by opening Bitcoin Core and clicking on the corresponding button on the options window. The following is an example configuration file. Then reopen bitcoin-qt or start the bitcoind daemon to start the server. The bitcoin-cli program and the testnet network can be used for testing RPC commands.


# Expose the RPC/JSON API

See also[edit | edit source]

Wikipedia[edit | edit source]

Wikibook[edit | edit source]

External links[edit | edit source]