| 
 | 1 | +Nav: [Workbook](../workbook.md) | [Handling Requests: Form/Query Parameter](/workbook/handling_request/form.md)  | 
 | 2 | + | 
 | 3 | + | 
 | 4 | +## EWF basic service  | 
 | 5 | + | 
 | 6 | +##### Table of Contents    | 
 | 7 | +- [Basic Structure](#structure)    | 
 | 8 | +- [Service to Generate Plain Text](#text)   | 
 | 9 | +	- [Source code](#source_1) 	  | 
 | 10 | +- [Service to Generate HTML](#html)  | 
 | 11 | +	- [Source code](#source_2) 	  | 
 | 12 | + | 
 | 13 | + | 
 | 14 | +<a name="structure"/>  | 
 | 15 | +## EWF service structure  | 
 | 16 | + | 
 | 17 | +The following code describes the basic structure of an EWF basic service that handles HTTP requests.  | 
 | 18 | + | 
 | 19 | +```eiffel  | 
 | 20 | +class  | 
 | 21 | +	SERVICE_TEMPLATE  | 
 | 22 | +
  | 
 | 23 | +inherit  | 
 | 24 | +	WSF_DEFAULT_SERVICE  -- Todo explain this, and the concept of launchers and connectors ()  | 
 | 25 | +
  | 
 | 26 | +create  | 
 | 27 | +	make_and_launch  | 
 | 28 | +
  | 
 | 29 | +feature -- Basic operations  | 
 | 30 | +
  | 
 | 31 | +	execute (req: WSF_REQUEST; res: WSF_RESPONSE)  | 
 | 32 | +		do  | 
 | 33 | +			-- To read incoming HTTP request, we need to use `req'  | 
 | 34 | +
  | 
 | 35 | +			-- May require talking to databases or other services.	  | 
 | 36 | +   | 
 | 37 | +			-- To send a response we need to setup, the status code and  | 
 | 38 | +			-- the response headers and the content we want to send out our client  | 
 | 39 | +		end  | 
 | 40 | +end  | 
 | 41 | +```  | 
 | 42 | + | 
 | 43 | +When using the "nino" connector, by default the service listens on port 80, but often this port is already used by other applications, so it is recommended to use another port.  | 
 | 44 | +To define another port, redefine the feature `initialize' and set up a new port number using the service options (see below).  | 
 | 45 | + | 
 | 46 | +```eiffel  | 
 | 47 | +class  | 
 | 48 | +	SERVICE_TEMPLATE  | 
 | 49 | +inherit  | 
 | 50 | +	WSF_DEFAULT_SERVICE  | 
 | 51 | +		redefine  | 
 | 52 | +			initialize  | 
 | 53 | +		end  | 
 | 54 | +
  | 
 | 55 | +create  | 
 | 56 | +	make_and_launch  | 
 | 57 | +
  | 
 | 58 | +feature {NONE} -- Initialization  | 
 | 59 | +
  | 
 | 60 | +	initialize  | 
 | 61 | +			-- Initialize current service.  | 
 | 62 | +			-- on port 9090  | 
 | 63 | +		do  | 
 | 64 | +			set_service_option ("port", 9090)  | 
 | 65 | +		end  | 
 | 66 | +
  | 
 | 67 | +feature -- Basic operations  | 
 | 68 | +
  | 
 | 69 | +	execute (req: WSF_REQUEST; res: WSF_RESPONSE)  | 
 | 70 | +			-- Execute the incoming request.  | 
 | 71 | +		do  | 
 | 72 | +			-- To read incoming HTTP requires, we need to use `req'  | 
 | 73 | +
  | 
 | 74 | +			-- May require talking to databases or other services.	  | 
 | 75 | +   | 
 | 76 | +			-- To send a response we need to setup, the status code and  | 
 | 77 | +			-- the response headers and the content we want to send out client  | 
 | 78 | +		end  | 
 | 79 | +end  | 
 | 80 | +```  | 
 | 81 | + | 
 | 82 | +The **WSF_REQUEST** gives access to the incoming data; the class provides features to get information such as request method, form data, query parameters, uploaded files, HTTP request headers, and hostname of the client among others.   | 
 | 83 | +The **WSF_RESPONSE** provides features to define the response with information such as HTTP status codes (10x,20x, 30x, 40x, and 50x), response headers (Content-Type, Content-Length, etc.) and obviously the body of the message itself.  | 
 | 84 | + | 
 | 85 | +**SERVICE_TEMPLATE** is the root class of our example, it launches the application, using the corresponding connector, Which connector? this depends how you want to run it cgi, fcgi or nino. For development is recommended to use Nino, a standalone web server written in Eiffel, and run the execution within the EiffelStudio debugger. For production fcgi (or cgi) using Apache or another popular web server.  | 
 | 86 | + | 
 | 87 | +The **SERVICE_TEMPLATE** class inherits from _WSF_DEFAULT_SERVICE_ class, and this one also inherits from other interfaces. Let’s describe them in a few words.  | 
 | 88 | + | 
 | 89 | +  | 
 | 90 | + | 
 | 91 | +**WS_LAUNCHABLE_SERVICE** inherit from **WS_SERVICE** class, which is the low level entry point in EWF, handling each incoming request with a single procedure ```execute (req: WSF_REQUEST; res: WSF_RESPONSE) ...```. And also provides a way to launch our application using different kind of connectors. Below a [BON diagram] (http://www.bon-method.com/index_normal.htm) showing the different kind of connectors.  | 
 | 92 | + | 
 | 93 | +  | 
 | 94 | + | 
 | 95 | +A basic EWF service inherits from **WSF_DEFAULT_SERVICE** (for other options see [?]).  | 
 | 96 | +And then you only need to implement the **execute** feature, get data from the request *req* and write the response in *res*.  | 
 | 97 | + | 
 | 98 | +<a name="text"/>  | 
 | 99 | +## A simple Service to Generate Plain Text.  | 
 | 100 | + | 
 | 101 | +Before to continue, it is recommended to review the getting started guided.  | 
 | 102 | + | 
 | 103 | +```eiffel  | 
 | 104 | +class  | 
 | 105 | +	APPLICATION  | 
 | 106 | +
  | 
 | 107 | +inherit  | 
 | 108 | +	WSF_DEFAULT_SERVICE  | 
 | 109 | +		redefine  | 
 | 110 | +			initialize  | 
 | 111 | +		end  | 
 | 112 | +
  | 
 | 113 | +create  | 
 | 114 | +	make_and_launch  | 
 | 115 | +
  | 
 | 116 | +feature {NONE} -- Initialization  | 
 | 117 | +
  | 
 | 118 | +	initialize  | 
 | 119 | +			-- Initialize current service.  | 
 | 120 | +		do  | 
 | 121 | +			set_service_option ("port", 9090)  | 
 | 122 | +		end  | 
 | 123 | +
  | 
 | 124 | +feature -- Basic operations  | 
 | 125 | +
  | 
 | 126 | +	execute (req: WSF_REQUEST; res: WSF_RESPONSE)  | 
 | 127 | +			-- Execute the incomming request  | 
 | 128 | +		do  | 
 | 129 | +				-- To send a response we need to setup, the status code and  | 
 | 130 | +				-- the response headers.  | 
 | 131 | +			res.put_header ({HTTP_STATUS_CODE}.ok, <<["Content-Type", "text/plain"], ["Content-Length", "11"]>>)  | 
 | 132 | +			res.put_string ("Hello World")  | 
 | 133 | +		end  | 
 | 134 | +
  | 
 | 135 | +end  | 
 | 136 | +```  | 
 | 137 | +<a name="source_1"></a>  | 
 | 138 | +##### Source code  | 
 | 139 | +The source code is available on Github. You can get it by running the command:  | 
 | 140 | + | 
 | 141 | +```git clone https://github.com/EiffelWebFramework/ewf_examples.git```  | 
 | 142 | + | 
 | 143 | +The example of simple service that generate plain text response is located in the directory $PATH/ewf_examples/workbook/basics/simple, where $PATH is where you run ```git clone``` . Just double click on the simple.ecf file and select the simple_nino target or if you prefer the command line, run the command:  | 
 | 144 | + | 
 | 145 | +```estudio -config simple.ecf -target simple_nino```  | 
 | 146 | + | 
 | 147 | +<a name="html"></a>  | 
 | 148 | +## A Service to Generate HTML.  | 
 | 149 | +To generate HTML, it's needed  | 
 | 150 | + | 
 | 151 | +1. Change the Content-Type : "text/html"  | 
 | 152 | +2. Build an HTML page  | 
 | 153 | + | 
 | 154 | +```eiffel  | 
 | 155 | +class  | 
 | 156 | +	APPLICATION  | 
 | 157 | +
  | 
 | 158 | +inherit  | 
 | 159 | +	WSF_DEFAULT_SERVICE  | 
 | 160 | +		redefine  | 
 | 161 | +			initialize  | 
 | 162 | +		end  | 
 | 163 | +
  | 
 | 164 | +create  | 
 | 165 | +	make_and_launch  | 
 | 166 | +
  | 
 | 167 | +feature {NONE} -- Initialization  | 
 | 168 | +
  | 
 | 169 | +	initialize  | 
 | 170 | +			-- Initialize current service.  | 
 | 171 | +		do  | 
 | 172 | +			set_service_option ("port", 9090)  | 
 | 173 | +		end  | 
 | 174 | +
  | 
 | 175 | +feature -- Basic operations  | 
 | 176 | +
  | 
 | 177 | +	execute (req: WSF_REQUEST; res: WSF_RESPONSE)  | 
 | 178 | +			-- Execute the incomming request  | 
 | 179 | +		do  | 
 | 180 | +				-- To send a response we need to setup, the status code and  | 
 | 181 | +				-- the response headers.  | 
 | 182 | +			res.put_header ({HTTP_STATUS_CODE}.ok, <<["Content-Type", "text/html"], ["Content-Length", web_page.count.out]>>)  | 
 | 183 | +			res.put_string (web_page)  | 
 | 184 | +		end  | 
 | 185 | +
  | 
 | 186 | +
  | 
 | 187 | +	web_page: STRING = "[ 	  | 
 | 188 | +	<!DOCTYPE html>  | 
 | 189 | +	<html>  | 
 | 190 | +		<head>  | 
 | 191 | +			<title>Resume</title>  | 
 | 192 | +		</head>  | 
 | 193 | +		<body>  | 
 | 194 | +			Hello World  | 
 | 195 | +		</body>  | 
 | 196 | +	</html>  | 
 | 197 | +]"  | 
 | 198 | +
  | 
 | 199 | +end  | 
 | 200 | +```  | 
 | 201 | +##### Source code  | 
 | 202 | +The source code is available on Github. You can get it by running the command:  | 
 | 203 | + | 
 | 204 | +```git clone https://github.com/EiffelWebFramework/ewf_examples.git```  | 
 | 205 | + | 
 | 206 | +The example of the service that generates HTML is located in the directory $PATH/ewf_examples/workbook/basics/simple_html, where $PATH is where you run ```git clone``` . Just double click on the simple_html.ecf file and select the simple_html_nino target or if you prefer the command line, run the command:  | 
 | 207 | + | 
 | 208 | +```estudio -config simple_html.ecf -target simple_html_nino```  | 
 | 209 | + | 
 | 210 | +Nav: [Workbook](../workbook.md) |  [Handling Requests: Form/Query Parameter](/workbook/handling_request/form.md)  | 
 | 211 | + | 
0 commit comments