官术网_书友最值得收藏!

Starting a web server

The first thing we need for a web application is a web server. In this section we will see how to start a web server using Opa.

A simple example

As a web application, resources such as web pages, images, and audios need to be served to users; therefore, we need an HTTP server. Let's think for a moment about how we would do that in PHP. The typical setup would be an Apache HTTP server with mod_php5 installed.

With Opa, things are a bit different. We not only implement our application, but also the whole HTTP server. In fact, our web application and its web server are basically the same. Our code will be translated into Node.js script after compilation, and will be run with Node.js. The benefit of integrating the server with a web application is increased security. Let's just start with a simple example:

Server.start(Server.http, {text: "hello Opa"})

Save this code into a file, 301.opa for example, then compile and run it. The two concluding dashes are needed to launch the web application after it has completed the compilation:

$ opa 301.opa –-

The output will be:

Http serving on http://localhost:8080

Type this address in your browser and you will see something like this:

The server module

We have started a web server and run our first Opa web application with the function Server.start. Let's now take a detailed look at this function:

void start(Server.conf arg1, Server.handler handler)

The function starts a web server with two parameters, the first is configuration information and the second is request handler. The Server.conf type is the configuration for the server. It is a record type with the following fields:

type Server.conf = {
  int port,                    //port server runs on
  ip netmask,                  //netmask
  Server.encryption encryption,//secure config if using https
  String name                  //server name
}

In most cases, we do not want to define all the elements in this type. We can extend from the static value Server.http. Server.http is a predefined default configuration with port equal to 8080 and the server protocol is http, and the default configuration for https is Server.https. In the following two lines, we are reusing the Server.http configuration, replacing port 8080 by port 8088 by using the instruction with port: 8088.

conf = {Server.http with port: 8088}
Server.start(conf,{text: "Hello Opa!"})
Note

You can also run your application with the –p option to change the port, which will override this.

Our web server will need to answer differently to different requests, depending on which URL was being requested. Therefore, we will need Server.handler to handle these requests. The Server.handler type is much more complicated than the Server.conf type. It defines how our web server will handle the incoming requests. It's a variant with eight cases:

type Server.handler = 
  {string text} or
  {string title, (-> xhtml) page} or
  {stringmap(resource) resources} or
  {(Uri.relative -> resource) dispatch} or
  {Server.filter filter, (Uri.relative -> resource) dispatch} or
  {Server.registrable_resource register} or
  {Parser.general_parser(resource) custom} or
  list(Server.handler)

In the example at the beginning of this chapter, we used the simplest case—{string text}. It accepts all the requests and just shows some text on the page. Let's see how the second case, {string title, (-> xhtml) page}, works:

Server.start(Server.http, {
  title: "Opa world"
  page : function(){ <h1>Hello Opa!</h1> }
})

The second case also handles all the requests, but it servers a single page. The field page is a function with the type void -> xhtml, which indicates that the function accepts no parameter and produces a value of the type xhtml. We will talk about XHTML later; the result looks like this:

We can notice from this screenshot that, compared to the first example, what has changed is that the web page we sent to the browser includes HTML markup that the web browser renders as a heading type.

主站蜘蛛池模板: 古蔺县| 元氏县| 伊川县| 英德市| 灵丘县| 安溪县| 包头市| 鄂托克前旗| 沙湾县| 开原市| 建阳市| 深州市| 介休市| 孟村| 汕头市| 茌平县| 武定县| 明溪县| 读书| 铜川市| 茂名市| 望江县| 榆中县| 河西区| 张家界市| 天峻县| 漾濞| 右玉县| 洱源县| 金昌市| 宁明县| 施甸县| 英山县| 泊头市| 柞水县| 石阡县| 远安县| 长海县| 赤城县| 萨迦县| 永泰县|