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

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.

主站蜘蛛池模板: 澳门| 惠来县| 望城县| 长岭县| 大悟县| 固原市| 循化| 中阳县| 无锡市| 北川| 临潭县| 陆川县| 永康市| 稷山县| 大渡口区| 天门市| 城市| 咸阳市| 炉霍县| 新昌县| 社旗县| 广灵县| 长沙县| 子洲县| 大厂| 景德镇市| 白河县| 喀喇沁旗| 阳山县| 香港| 大丰市| 大英县| 达孜县| 北辰区| 株洲市| 陕西省| 都江堰市| 南和县| 响水县| 和田县| 碌曲县|