- 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.
- FuelPHP Application Development Blueprints
- Kali Linux Web Penetration Testing Cookbook
- LabVIEW 2018 虛擬儀器程序設計
- 大學計算機基礎實驗教程
- JavaScript Unlocked
- PyTorch自然語言處理入門與實戰
- 機器人Python青少年編程開發實例
- Mastering OpenCV 4
- Microsoft System Center Orchestrator 2012 R2 Essentials
- Oracle從入門到精通(第5版)
- Android開發案例教程與項目實戰(在線實驗+在線自測)
- 微服務從小白到專家:Spring Cloud和Kubernetes實戰
- Learning Apache Karaf
- JSP程序設計實例教程(第2版)
- Python 3 數據分析與機器學習實戰