Jump to content

Ada Programming/Libraries/Web/AWS

From Wikibooks, open books for an open world

Ada. Time-tested, safe and secure.
Ada. Time-tested, safe and secure.

AWS, the Ada Web Server, is a complete framework to develop Web based applications. The main part of AWS is the embedded web server. This small, yet powerful, Web server can be embedded into your application, so it will be able to talk with a standard Web browser. Around this Web server, a lot of services have been developed.

AWS supports SOAP Web Services and the REST architecture.

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

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.

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;

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

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.

bitcoin.conf

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

See also

[edit | edit source]

Wikipedia

[edit | edit source]

Wikibook

[edit | edit source]
[edit | edit source]
Project Info
https://github.com/adacore/aws/
Download
https://github.com/adacore/aws/releases