본문으로 건너뛰기

Yaws Web Framework

Yaws is an Erlang web server and application server framework.

It is designed to be lightweight, efficient, and scalable, making it a popular choice for building high-performance web applications. Yaws stands for Yet Another Web Server, but it is much more than just a web server. It provides a complete framework for developing web applications in Erlang, including support for dynamic content generation, session management, and more.

History

Yaws was first released in 2001 by Claes Wikström as an alternative to the existing web servers available at that time. It was initially developed to address the limitations of other web servers in terms of performance and scalability. Over the years, Yaws has gained popularity among Erlang developers and has become a widely used framework for building web applications.

Features

  1. Lightweight and Scalable: Yaws is known for its lightweight nature, making it highly efficient and scalable. It is designed to handle a large number of concurrent connections without compromising performance.

  2. Dynamic Content Generation: Yaws provides built-in support for generating dynamic content. It allows developers to embed Erlang code directly into HTML templates, enabling the creation of dynamic web pages.

    <html>
    <body>
    <h1>Welcome to Yaws!</h1>
    <p>Current date and time: <strong>{erlang:localtime()}</strong></p>
    </body>
    </html>

    In the above example, the {erlang:localtime()} expression is evaluated at runtime, and the current date and time are inserted into the HTML page.

  3. HTTP and HTTPS Support: Yaws supports both HTTP and HTTPS protocols, allowing developers to secure their web applications using SSL/TLS encryption.

  4. Virtual Hosting: Yaws supports virtual hosting, enabling multiple websites to be hosted on the same server instance. Each virtual host can have its configuration, including separate document roots, log files, and access controls.

  5. Session Management: Yaws provides session management capabilities, allowing developers to store and retrieve session data. This feature is essential for building stateful web applications.

    -include_lib("yaws_api.hrl").

    out(Arg) ->
    SessionData = yaws_api:find_session_data(Arg),
    Count = case SessionData of
    undefined -> 0;
    _ -> SessionData
    end,
    yaws_api:replace_session_data(Arg, Count + 1),
    {html, io_lib:format("Count: ~p", [Count + 1])}.

    In the above example, the find_session_data function retrieves the session data, and the replace_session_data function increments the session count. The output is an HTML page displaying the current count.

  6. WebSocket Support: Yaws supports WebSocket communication, allowing real-time bidirectional communication between the server and the client.

    -include_lib("yaws_api.hrl").

    websocket_handle_message({text, Message}, State) ->
    {reply, {text, "You said: " ++ Message}, State}.

    In the above example, the websocket_handle_message function is called whenever a text message is received from the client. It replies with a message containing the client's input.

  7. URL Rewriting: Yaws supports URL rewriting, allowing developers to map URLs to specific resources or handlers. This feature is useful for creating clean and user-friendly URLs.

    -include_lib("yaws_api.hrl").

    out(A) ->
    case yaws_api:find_uri(A) of
    [] -> {ehtml, "404 Not Found"};
    [{"/about", _}] -> about_page();
    [{"/contact", _}] -> contact_page();
    _ -> {ehtml, "Welcome to Yaws!"}
    end.

    In the above example, the find_uri function retrieves the requested URI, and based on its value, different functions are called to generate the appropriate output.

For more information about Yaws and its features, you can visit the official website: https://yaws.hyber.org/

Examples

  1. Hello World: A simple Yaws application that displays "Hello, World!" when accessed.

    -module(hello_world).
    -export([out/1]).

    out(_Arg) ->
    {html, "Hello, World!"}.

    When accessed through a web browser, the output will be an HTML page displaying the message "Hello, World!".

  2. URL Parameter: An example of how to handle URL parameters in Yaws.

    -module(url_param).
    -export([out/1]).

    out(Arg) ->
    Name = yaws_api:parse_query_arg("name", Arg),
    {html, io_lib:format("Hello, ~s!", [Name])}.

    When accessed with a URL like http://localhost:8000/url_param?name=John, the output will be an HTML page displaying the message "Hello, John!".

  3. Static File Serving: An example of serving static files using Yaws.

    -module(static_files).
    -export([out/1]).

    out(_Arg) ->
    {content, {file, "path/to/file.html"}}.

    When accessed, Yaws will serve the static file located at the specified path.

These examples demonstrate some of the core features of Yaws and how they can be implemented. By leveraging the power of Erlang and the flexibility of Yaws, developers can build high-performance web applications with ease.

This tutorial only scratches the surface of what Yaws has to offer. For more advanced features and detailed documentation, refer to the official Yaws website: https://yaws.hyber.org/.