- Opa Application Development
- Li Wenbo
- 641字
- 2021-08-20 16:49:39
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.
- The Android Game Developer's Handbook
- ThinkPHP 5實戰(zhàn)
- 深入淺出Electron:原理、工程與實踐
- UML+OOPC嵌入式C語言開發(fā)精講
- Java程序設計與實踐教程(第2版)
- Learn WebAssembly
- 實戰(zhàn)Java高并發(fā)程序設計(第3版)
- C# 8.0核心技術(shù)指南(原書第8版)
- Mastering Git
- DevOps 精要:業(yè)務視角
- Python編程入門(第3版)
- Ionic Framework By Example
- Java性能權(quán)威指南
- IBM Cognos TM1 Cookbook
- Unity Certified Programmer:Exam Guide