cgval
cgval

Reputation: 1096

Yaws Sample Files

I am currently trying to understand and find case studies for web pages using YAWS. Apart from the default ones that come with the sources, anyone knows of any sample pages that I can find?

Thanks,

Upvotes: 2

Views: 1139

Answers (1)

prosseek
prosseek

Reputation: 190809

Building Web Applications with Erlang maybe a good book to read, but it assumes that readers already know a lot about the setup environment and fundamental yaws ideas.

So, you can start from http://yaws.hyber.org/simple.yaws.

I summarize some of the fundamentals with yaws as follows. I run the yaws on Ubuntu.

Setup

Installation

sudo apt-get install yaws and sudo apt-get install erlang will install erlang and yawk.

Configuration

/etc/yaws/yaws.conf is the main configuration file. It loads conf.d/localhost.conf, so you can add your own configuration just like Apache or Nginx does. This is an example that uses 8083 port.

<server localhost>
    port = 8083
    listen = 0.0.0.0
    docroot = /home/ubuntu/webapp/yaws/simple
</server>

Things to consider

In Ubuntu, the directories are only accessible to root, so you should run sudo .. or run sudo chmod g+rw .... In the latter, you should make your account as root and ssl-cert group with sudo usermod -a -G ssl-cert ubuntu.

Working with Nginx webserver

You can use Nginx as a baacke-end with nginx as reverse proxy server. This is an example configuration in /etc/nginx/site-enabled (linked to corresponding /etc/ngix/site-available) directory.

upstream yaws {
        server 127.0.0.1:8083;
        keepalive 8;
}

# the nginx server instance
server {
        listen 0.0.0.0:80;
        server_name yaws.example.com;
        access_log /var/log/yaws/access_yaws.log;
        error_log /var/log/yaws/error_yaws.log;

        location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;

            proxy_pass http://yaws/;
            proxy_redirect off;
        }

        location /excluded {
            return 403;
        } 
}

Server test

You can start the yaws server with sudo server yaws start, and check the yaws is running with ps aux | grep yaws. You can make the yaws server start at bootup time with sudo update-rc.d -f yaws enable.

You can also run and test yaws server interactively with yaws -i command. In this case if you see the error message:

=ERROR REPORT==== 10-Nov-2015::09:44:33 ===
Yaws: Failed to listen 0.0.0.0:8443  : {error,eaddrinuse}

=ERROR REPORT==== 10-Nov-2015::09:44:33 ===
Can't listen to socket: {error,eaddrinuse} 
=INFO REPORT==== 10-Nov-2015::09:44:33 ===
    application: yaws
    exited: {{shutdown,
                 {failed_to_start_child,yaws_server,
                     {badbind,
                         [{yaws_server,start_group,2,
                              [{file,"yaws_server.erl"},{line,264}]},
                          {lists,filtermap,2,[{file,"lists.erl"},{line,1302}]},
                          {yaws_server,init2,5,

The yaws server is running, so you should stop the yaws server with sudo service yaws stop, and run yaws -i again. Then you will see the prompt, if you hit the enter key.

=INFO REPORT==== 10-Nov-2015::09:45:42 ===
Yaws: Listening to 0.0.0.0:8443 for <1> virtual servers:
 - https://localhost:8443 under /usr/share/yaws

=INFO REPORT==== 10-Nov-2015::09:45:42 ===
Yaws: Listening to 0.0.0.0:8083 for <1> virtual servers:
 - http://localhost:8083 under /home/ubuntu/webapp/yaws/simple

1> 

You can run many commands, for example, you can check the path that yaws find the erlang beam code with code:get_path().

1> code:get_path().
["/usr/lib/yaws/ebin",".",
 "/usr/lib/erlang/lib/kernel-2.16.4/ebin",
 "/usr/lib/erlang/lib/stdlib-1.19.4/ebin",
 "/usr/lib/erlang/lib/xmerl-1.3.5/ebin",

Examples

static files

In the configuration, you have docroot /home/ubuntu/webapp/yaws/simple is accessed with port 8083, so you can access static files: i.e., http://...:8083/hello.html.

.yaws files

If the file has .yaws extension yaws compiles it into erlang code dynamically, and stores the source code in /var/cache/yaws/.yaws/yaws/debian_yaws/.

Static yaws file

You can put the html code in the yaws file.

<html>

<h1>Hello world </h1>

</html>

erlang code

You can also put erlang code. Check that out(Arg) -> is the function to render into HTML.

<html>
<h1> Yesssssss </h1>

<erl>
out(Arg) -> {html, "<h2> Hello again </h2>"}.
</erl>

</html>

App mode

You can teach yaws to invoke erlang code. With this configuration:

<server localhost>
    port = 8083
    listen = 0.0.0.0
    docroot = /home/ubuntu/webapp/yaws/simple
    appmods = <pathelem, myappmod>
</server>

Yaws will invoke the myappmod method when it sees pathelem in the URL. For app mode, you should make the myappmod module.

-module(myappmod).
-author('[email protected]').

-include("/usr/lib/yaws/include/yaws_api.hrl").
-compile(export_all).

box(Str) ->
    {'div',[{class,"box"}],
     {pre,[],Str}}.

out(A) ->
    {ehtml,
     [{p,[],
       box(io_lib:format("A#arg.appmoddata = ~p~n"
                         "A#arg.appmod_prepath = ~p~n"
                         "A#arg.querydata = ~p~n",
                         [A#arg.appmoddata,
                          A#arg.appmod_prepath,
                          A#arg.querydata]))}]}.

Then, compile it into beam binary.

erlc myappmod.erl

In interactive mode, as the "." already in the path, but in server mode or in daemon mode, the bean should be in one of the search path. In /etc/yaws/yaws.conf, you can update the ebin directory.

ebin_dir = /usr/lib/yaws/custom/ebin

With this appmode, with this URL.

http://yaws.example.com:8083/zap/pathelem/foo/bar/x.pdf?a=b

The results will be:

A#arg.appmoddata = "foo/bar/x.pdf"
A#arg.appmod_prepath = "/zap/"
A#arg.querydata = "a=b"

I think this enough information one can start reading other doucments.

Upvotes: 7

Related Questions