\input texinfo    @c -*- texinfo -*-
@c %**start of header
@setfilename artanis.texi
@settitle GNU Artanis web-framework Manual
@documentencoding UTF-8
@documentlanguage en
@syncodeindex pg cp
@c %**end of header

@finalout
@titlepage
@title GNU Artanis
@author Mu Lei known as NalaGinrut
@end titlepage

@contents

@ifnottex
@node Top
@top GNU Artanis web-framework Manual
@end ifnottex

@menu
* Introduction::
* License::
* Installation::
* Configuration::
* Hello World::
* Scheme Basics::
* GNU Artanis Basics::
* The Art command line::
* URL remapping::
* Route context::
* Page status handling::
* MVC::
* Query String::
* Layouts and Rendering in GNU Artanis::
* Databases::
* RESTful API::
* MIME::
* Upload files::
* Sessions::
* Cookies::
* Authentication::
* Cache::
* Shortcuts::
* Websocket Experimental::
* Ragnarok server core::
* Key-Value Database::
* I18n::
* Utils::
* Debug mode::
* Appendix A GNU Free Documentation License::
@end menu

@node Introduction
@chapter Introduction
@anchor{#introduction}

@verbatim
Copyright (C)  2024  Mu Lei known as NalaGinrut.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3
or any later version published by the Free Software Foundation;
with no Invariant Sections, no Front-Cover Texts, and no Back-Cover
Texts.  A copy of the license is included in the section entitled 'GNU
Free Documentation License'.
@end verbatim

This manual documents GNU Artanis, a fast, monolithithic web framework of Scheme.

A @uref{http://en.wikipedia.org/wiki/Web_application_framework,web application framework (WAF)} is a software framework that is designed to support the development of dynamic websites, web applications, web services and web resources. This framework aims to alleviate the overhead associated with common activities in web development. GNU Artanis provides several tools for web development: database access, templating frameworks, session management, @uref{http://en.wikipedia.org/wiki/Rewrite_engine,URL-remapping} for @uref{http://en.wikipedia.org/wiki/Representational_state_transfer,RESTful}, page caching, and more.

Guile is the GNU Ubiquitous Intelligent Language for Extensions, the official extension language for the @uref{http://www.gnu.org/,GNU operating system}. Guile is also an interpreter and compiler for other dynamic programming languages including Scheme.

@uref{http://en.wikipedia.org/wiki/Scheme_%28programming_language%29,Scheme} is a functional programming language and one of the two main dialects of the programming language @uref{http://en.wikipedia.org/wiki/Lisp_(programming_language),Lisp}. Scheme follows a minimalist design philosophy specifying a small standard core with powerful tools for language extension.

@menu
* Conventions used in this manual::
* No warranty::
@end menu

@node Conventions used in this manual
@section Conventions used in this manual
@anchor{#conventions-used-in-this-manual}
In this manual the following syntax is used to demonstrate the use of the API:

@verbatim
(api-name arg1 arg2 #:key0 val0 ... [optional-arg1 <- default-value1] ...)
@end verbatim

If you are new to Scheme, it is recommended that you read the @emph{Basic in Scheme} chapter first.

@node No warranty
@section No warranty
@anchor{#no-warranty}
We distribute software in the hope that it will be useful, but without any warranty. No author or distributor of this software accepts responsibility to anyone for the consequences of using it or for whether it serves any particular purpose or works at all, unless they say so in writing. This is exactly the same warranty that proprietary software companies offer: none.

@node License
@chapter License
@anchor{#license}
GNU Artanis is @uref{http://www.gnu.org/philosophy/free-sw.html,Free Software}. GNU Artanis is under the terms of the GNU GPLv3+ and GNU LGPLv3+. See the files COPYING and COPYING.LESSER in toplevel of source code.

This manual is published under the terms of the @uref{http://www.gnu.org/copyleft/fdl.html,GNU Free Documentation License} 1.3 or later.

@strong{You must be aware there is no warranty whatsoever for GNU Artanis. This is described in full in the This is described in full in the licenses.}

@node Installation
@chapter Installation
@anchor{#installation}

@menu
* For Guix users::
* For Ubuntu/Debian users to install dependencies::
* For hardcore users::
* For contributors::
@end menu

@node For Guix users
@section For Guix users
@anchor{#for-guix-users}
GNU Guix is the advanced functional & transactional package manager, we recommend you to try it. You may read the @uref{https://guix.gnu.org/manual/en/html_node/Installation.html,GNU Guix installation} .

@verbatim
guix install artanis
@end verbatim

@node For Ubuntu/Debian users to install dependencies
@section For Ubuntu/Debian users to install dependencies
@anchor{#for-ubuntudebian-users-to-install-dependencies}

@verbatim
apt-get -qq -y install texinfo guile-3.0 guile-3.0-dev build-essential automake git autoconf libtool libmariadb-dev-compat libmariadb-dev libnss3 libnss3-dev

apt-get -qq -y install gettext redis redis-server libcurl4-openssl-dev
@end verbatim

@node For hardcore users
@section For hardcore users
@anchor{#for-hardcore-users}
@strong{Install GNU Guile-3.0.5 or higher version:}

Since GNU Artanis-0.5, GNU Guile-3+ is required.

Install pre-requisites (typically available in linux package distributions): a C compiler, [[@url{https://www.gnu.org/software/autoconf/]autoconf}], [[@url{https://www.gnu.org/software/automake/]automake}], [[@url{https://sourceware.org/libffi/]libffi}], [[@url{https://curl.se/libcurl/]libcurl}], [[@url{https://www.gnu.org/software/libtool/]libtool} and libltdl], [[@url{https://www.gnu.org/software/libunistring/]libunistring}], [[@url{https://www.perl.org/]perl}], [[@url{https://firefox-source-docs.mozilla.org/security/nss/index.html]libnss} (Network Security Services)], [[@url{https://tiswww.case.edu/php/chet/readline/rltop.html]readline}] and [[@url{http://zlib.net/]zlib}].

@verbatim
wget -c ftp://ftp.gnu.org/gnu/guile/guile-3.0.10.tar.xz
tar xvf guile-3.0.10.tar.xz
cd guile-3.0.10 && ./configure && make #(NOTE: this may take a very long time)
sudo make install
@end verbatim

I would NOT recommend trying to compile/install Guile from Git repo, since it'll take a long time.

@strong{Install dependencies:}

@itemize
@item
guile-dbi-2.1.8 (The tarball included guile-dbd) @strong{[Optional]}
@end itemize

@verbatim
wget -c https://github.com/opencog/guile-dbi/archive/guile-dbi-2.1.8.tar.gz

tar xvzf guile-dbi-2.1.8.tar.gz
cd guile-dbi-guile-dbi-2.1.7/guile-dbi && ./autogen.sh --no-configure && ./configure && make
sudo make install
cd ../..
@end verbatim

@itemize
@item
guile-dbd @strong{[Optional]}. The dbd plugins connect to an actual database server.
@end itemize

@verbatim

cd guile-dbi-guile-dbi-2.1.8/guile-dbd-mysql && ./autogen.sh --no-configure && ./configure && make
sudo make install
@end verbatim

MySQL is used for the examples in this manual. You may find dbd plugins for other databases @emph{here}. The installation process is identical.

@itemize
@item
guile-curl for the client module.
@end itemize

@verbatim
git clone https://github.com/spk121/guile-curl.git
cd guile-curl && git checkout v0.9
./bootstrap && ./configure --prefix=/usr && make -j
make install
ln -s /usr/lib/guile/3.0/extensions/libguile-curl.* /usr/lib/
ldconfig
@end verbatim

@strong{@strong{NOTE:}} You may need to make necessary soft links according to above script.

@itemize
@item
guile-redis:
@end itemize

@verbatim
git clone https://github.com/aconchillo/guile-redis.git
cd guile-redis
git checkout -b 2.2.0
autoreconf -vif
./configure --prefix=/usr
make
make install
@end verbatim

@itemize
@item
guile-json:
@end itemize

@verbatim
git clone https://github.com/aconchillo/guile-json.git
cd guile-json
git checkout -b 4.7.3
autoreconf -iv
./configure --prefix=/usr
make -j5
sudo make install
sudo ldconfig
@end verbatim

@itemize
@item
libnss (Network Security Services):
@end itemize

Please follow the [[@url{https://firefox-source-docs.mozilla.org/security/nss/build.html}] official guide]. It's better to install it with your distro's pkg manager.

@strong{Install the latest GNU Artanis:}

,#+begin_src null wget -c @url{http://ftp.gnu.org/gnu/artanis/artanis-latest.tar.bz2} tar xvjf artanis-latest.tar.bz2 cd artanis-latest && ./autogen.sh --no-configure && ./configure && make sudo make install

@node For contributors
@section For contributors
@anchor{#for-contributors}
First of all, thank you for contributing! You may clone the main git repository, or the mirror on GitLab:

@verbatim
git clone git://git.savannah.gnu.org/artanis.git

# mirror on GitLab
git clone https://gitlab.com/hardenedlinux/artanis.git
@end verbatim

@node Configuration
@chapter Configuration
@anchor{#configuration}
A configuration file is required when Artanis is run for the first time.

@itemize
@item
If you're using minimum mode, say, all code are in a script file without application directory. The configure file must be named @code{/etc/artanis/artanis.conf}.

@item
If you're using application directory, the configure file @code{conf/artanis.conf} will be generated automatically for you.

@end itemize

@menu
* Config APIs::
@end menu

@node Config APIs
@section Config APIs
@anchor{#config-apis}
To change the default configurations:

@verbatim
(conf-set! key value)
;;e.g
(conf-set! 'debug-mode #t)
@end verbatim

To get the current configuration:

@verbatim
(get-conf key)
;;e.g
(get-conf '(server charset))
@end verbatim

To get the current hostname in GNU Artanis environment.

@verbatim
(current-myhost)
@end verbatim

@node Hello World
@chapter Hello World
@anchor{#hello-world}

@menu
* Use Guile REPL and verify GNU Artanis installation::
* Simple HTTP server::
* Try simple URL remapping::
* More complex URL remapping::
* Regex in URL remapping::
* Database operating::
@end menu

@node Use Guile REPL and verify GNU Artanis installation
@section Use Guile REPL and verify GNU Artanis installation
@anchor{#use-guile-repl-and-verify-gnu-artanis-installation}
If you are already familiar with Guile, you may skip this section.

Type `guile' in your console to enter the Guile REPL. You should see the following text displayed on your screen:

@verbatim
GNU Guile 3.0.9
Copyright (C) 1995-2023 Free Software Foundation, Inc.

Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'.
This program is free software, and you are welcome to redistribute it
under certain conditions; type `,show c' for details.

Enter `,help' for help.
scheme@(guile-user)>
@end verbatim

Welcome to Guile world! We are now going to play with GNU Artanis. Before we start, we need to check that GNU Artanis is installed correctly:

@strong{(Just type them, you don't have to understand them at present)}

@verbatim
,use (artanis artanis)
artanis-version
@end verbatim

The expected output should be similar to this:

@verbatim
$1 = "GNU Artanis-x.x.x"
@end verbatim

@node Simple HTTP server
@section Simple HTTP server
@anchor{#simple-http-server}
Run this code in your console:

@verbatim
guile -c "(use-modules (artanis artanis))(init-server)(run)"
## You'll see this screen:
Anytime you want to quit just try Ctrl+C, thanks!
http://127.0.0.1:3000
@end verbatim

Assuming there's a file named "index.html" in the current path. Now you may try @url{http://localhost:3000/index.html} in your browser. It's just simply fetching static file by the URL: @url{http://localhost:3000/path/filename}

@node Try simple URL remapping
@section Try simple URL remapping
@anchor{#try-simple-url-remapping}
Type these code in Guile REPL:

@verbatim
(use-modules (artanis artanis))
(init-server)
(get "/hello" (lambda () "hello world"))
(run #:port 8080)
@end verbatim

Now you can visit @url{http://localhost:8080/hello} with your browser, and (hopefully) see the result.

/If you encounter "[EXCEPTION] @emph{favicon.ico is abnormal request" , please just ignore that warning.}

Let me explain the code:

@itemize
@item
@emph{line 1:} Load GNU Artanis module, (artanis artanis) is the name.
@end itemize

@itemize
@item
@emph{line 2:} The first argument @emph{get} is GNU Artanis' API correspondence to the GET method of the HTTP protocol. The second argument "/hello" is the URL rule to register showing in the address line of e.g. firefox. The third argument is the handler which will be triggered if the registered URL rule is hit.

@item
@emph{line 3:} Run the GNU Artanis web server, and listen on socket port 8080.

@end itemize

You may type Ctrl+C to quit and stop the server, see also the message printed on the screen accordingly.

@node More complex URL remapping
@section More complex URL remapping
@anchor{#more-complex-url-remapping}
Try this code:

@verbatim
(use-modules (artanis artanis))
(init-server)
(get "/hello/:who"
  (lambda (rc)
    (format #f "<p>hello ~a</p> " (params rc "who"))))
(run #:port 8080)
@end verbatim

Now you can try @url{http://localhost:8080/hello/artanis} in your browser.

There are two differences compared to the simpler example:

@itemize
@item
1. The special rule, "@code{/hello/:who}", @emph{:who} means you can use @emph{params} to refer to the value of the par of the URL with the key "who". Like: @code{(params rc "who")}.

@item
2. You may have noticed that the handler is being defined as an anonymous function with @emph{lambda} has one argument @emph{rc}. It means @emph{route context} which preserves all the related context information. Many GNU Artanis APIs need it, e.g. @emph{params}.

@end itemize

And @emph{format} is a Scheme lib function. It is similar to @emph{sprintf} in the C language, which outputs text with a formatted pattern. The second argument #f (means FALSE) indicates that the formatted output should be returned in a string rather than printed out.

@node Regex in URL remapping
@section Regex in URL remapping
@anchor{#regex-in-url-remapping}
You can use regular expressions as a URL rule argument.

@verbatim
(use-modules (artanis artanis))
(init-server)
(get "/.+\\.(png|gif|jpeg)" static-page-emitter)
(run #:port 8080)
@end verbatim

@emph{static-page-emitter} is a GNU Artanis API that sends a static file (images, data files) to the client.

@node Database operating
@section Database operating
@anchor{#database-operating}
GNU Artanis supports mysql/postgresql/sqlite3. We use mysql as an example here.

Please ensure that your DB service was started before you run this code.

@strong{@emph{If you encounter any problems, it's very likely it's with your DB config.}}

You can use a DB (such as mysql) with GUI tools such as "adminer", independently of the running web-server, e.g. artanis-based.

@verbatim
(use-modules (artanis artanis))
(init-server)
(define conn (connect-db 'mysql #:db-username "your_db_username"
                         #:db-name "your_db_name" #:db-passwd "your_passwd"))
(define mtable (map-table-from-DB conn))
((mtable 'create 'Persons '((name varchar 10)
                            (age integer)
                            (email varchar 20)))
 'valid?)
;; ==> #t
(mtable 'set 'Persons #:name "nala" #:age 20 #:email "nala@artanis.dev")
(mtable 'get 'Persons #:columns '(name email))
;; ==> ((("name" . "nala") ("email" . "nala@artanis.com")))
@end verbatim

@itemize
@item
@emph{map-table-from-DB} is the GNU Artanis API handling tables in a DB. Here, we define this mapping as the var @emph{mtable}.
@end itemize

@itemize
@item
We can use @emph{mtable} to handle tables, you can get the values from a table with the 'get command.
@end itemize

@itemize
@item
@emph{mtable} is a function which accepts the first argument as a command, say 'create, which is a command to create a new table. 'set is used to insert/update values in the table. And the 'get command to fetch the values of specific columns.
@end itemize

@itemize
@item
The second argument of @emph{mtable} is the name of the table as you can guess. Please note that it is case sensitive, while the column name isn't.
@end itemize

@itemize
@item
The @emph{@strong{'create}} command returns a function too, which also accepts an argument as a command. Here, we use the @emph{@strong{'valid?}} command to check if the table has been created successfully.
@end itemize

This was just a simple introduction. You may read the DB section in this manual for details.

Of course, you can use DB in your web application.

@verbatim
(get "/dbtest" #:conn #t ; apply for a DB connection from pool
     (lambda (rc)
       (let ((mtable (map-table-from-DB (:conn rc))))
         (object->string
          (mtable 'get 'Persons #:columns '(name email))))))

(run #:use-db? #t #:dbd 'mysql #:db-username "your_db_username"
     #:db-name "your_db_name" #:db-passwd "your_passwd" #:port 8080)
@end verbatim

Now, try loading @url{http://localhost:8080/dbtest} in your browser.

Here is quick explanation:

@itemize
@item
The keyword-value pair @code{#:conn #t} means applying for a DB connection from connection-pool. Then you can use @code{(:conn rc)} to get the allocated connection for DB operations.

@item
Finally, the handler needs to return a string as the HTTP response body, so in this example,

@end itemize

we have to use the Guile API @emph{object->string} to convert the query result into a string.

@emph{Exercise: Return a beautiful table in HTML rather than using object->string.}

@node Scheme Basics
@chapter Scheme Basics
@anchor{#scheme-basics}
This chapter introduces some useful documents to help you understand Scheme language. Feel free to come back here if you have any problems with the Scheme syntax.

Scheme was introduced in 1975 by Gerald J. Sussman and Guy L. Steele Jr. and was the first dialect of Lisp to fully support lexical scoping, first-class procedures, and continuations. In its earliest form it was a small language intended primarily for research and teaching, supporting only a handful of predefined syntactic forms and procedures. Scheme is now a complete general-purpose programming language, though it still derives its power from a small set of key concepts. Early implementations of the language were interpreter-based and slow, but Guile Scheme is trying to implement sophisticated compiler that generate better optimized code, and even a plan for AOT compiler generated native code in the future.

@menu
* For newbies::
* For Pythonistas::
* For Rubyist::
* For deep learners::
@end menu

@node For newbies
@section For newbies
@anchor{#for-newbies}
If you're not familiar with Scheme and Guile in particular, here is a simple tutorial for you.

If you already know the basics of the Scheme language, please feel free to skip this section.

I would recommend newbies to type/paste the code in Guile REPL following the guide in tutorial: @uref{http://web-artanis.com/scheme.html,Learn Scheme in 15 minutes}

And here's a nice section in the Guile manual for basics in Scheme: @uref{https://www.gnu.org/software/guile/manual/guile.html#Hello-Scheme_0021,Hello Scheme}

Please don't spend too much time on these tutorials, the purpose is to let newbies get a little familiar with the grammar of Scheme.

@node For Pythonistas
@section For Pythonistas
@anchor{#for-pythonistas}
These are good pythonic articles for Pythoners:

@enumerate 
@item
@uref{http://draketo.de/proj/guile-basics/,Guile basics from the perspective of a Pythonista}
@item
@uref{http://draketo.de/proj/py2guile,Going from Python to Guile Scheme}
@end enumerate

Still, please don't spend too much time on them, the purpose is to let newbies get a little familiar with the grammar of Scheme.

@node For Rubyist
@section For Rubyist
@anchor{#for-rubyist}
Here's a great article for Rubyist to learn Scheme:

@enumerate 
@item
@uref{http://wiki.call-cc.org/chicken-for-ruby-programmers,Scheme for ruby programmers}
@end enumerate

@node For deep learners
@section For deep learners
@anchor{#for-deep-learners}
These two books are very good for learning Scheme seriously:

@enumerate 
@item
@uref{http://www.scheme.com/tspl4/,The Scheme Programming Language}
@item
@uref{http://mitpress.mit.edu/sicp/,Structure and Interpretation of Computer Programs(SICP)}
@end enumerate

Please don't bother reading them if you simply want to use GNU Artanis to build your web application/site in few minutes.

And if you really want to try to work these books seriously, please ignore GNU Artanis before you are done with them.

But once you're done reading them @strong{carefully}, you may want to write a new GNU Artanis all by yourself!

Hold your horses. ;-)

@node GNU Artanis Basics
@chapter GNU Artanis Basics
@anchor{#gnu-artanis-basics}

@menu
* Web application structure::
* How to run a site with GNU Artanis::
* Initialization::
* Registering handlers for HTTP methods::
* Emit a Response::
* Running the server::
* Working with Nginx::
@end menu

@node Web application structure
@section Web application structure
@anchor{#web-application-structure}

@verbatim
├── app
│   ├── controllers
│   ├── models
│   ├── protocols
│   └── views
├── conf
│   ├── artanis.conf
│   └── README
├── db
│   ├── migration
│   └── sm
├── ENTRY
├── lib
├── log
├── prv
├── pub
│   ├── css
│   ├── img
│   │   └── upload
│   └── js
├── README
├── sys
│   ├── i18n
│   │   ├── json
│   │   ├── po
│   │   └── sxml
│   └── pages
└── test
    ├── benchmark
    │   └── README
    ├── functional
    └── unit
@end verbatim

@itemize
@item
@strong{@strong{lib}} is where you put your own modules for the app, and the third-part modules could be put into it. The load path will be added automatically when the server core start.

@item
@strong{@strong{log}} is expected to put logs, however, it's not actually used yet.

@item
@strong{@strong{prv}} is a place to put your private stuffs, like API token or password in text. This should never be added to git.

@end itemize

@menu
* Temporary files::
@end menu

@node Temporary files
@subsection Temporary files
@anchor{#temporary-files}
Since GNU Artanis 1.1.0, the @strong{@strong{tmp}} directory was moved to @strong{@strong{/tmp/your_app_name-xxxxx}}. Each time you run @strong{@strong{art work}}, the related temporary directory will be regenerated. The purpose is to make application folder functional and clean. We support GNU Guix to make the application folder read-only.

@node How to run a site with GNU Artanis
@section How to run a site with GNU Artanis
@anchor{#how-to-run-a-site-with-gnu-artanis}
This is the simplest way to run a site:

@verbatim
#!/bin/env guile
!#
(use-modules (artanis artanis))
(init-server)
(get "/hello" (lambda () "hello world"))
(run)
@end verbatim

@node Initialization
@section Initialization
@anchor{#initialization}
It's better to use (init-server) to init GNU Artanis.

@verbatim
(init-server #:statics '(png jpg jpeg ico html js css)
             #:cache-statics? #f #:exclude '())
@end verbatim

@code{#:statics} specifies the static files with the file name extensions. GNU Artanis is based on URL remapping, so the requested URL will have to end in the requested file name, matching the string defined, and returning the file without any extra defintions per file type. By default, it covers the most common static file types.

@code{#:cache-statics?} controls whether the static files should be cached.

@code{#:exclude} specifies the types should be excluded. This is useful when you want to generate files dynamically. Even JavaScript/CSS could be generated dynamically, so it depends your design.

@node Registering handlers for HTTP methods
@section Registering handlers for HTTP methods
@anchor{#registering-handlers-for-http-methods}
Please read @emph{URL handling}.

@node Emit a Response
@section Emit a Response
@anchor{#emit-a-response}

@verbatim
(response-emit body #:status 200 #:headers '() #:mtime (current-time))
@end verbatim

@strong{body} is the response body, it can be a bytevector or literal string (in HTML).

@code{#:status} is the HTTP status, 200 in default, which means OK.

@code{#:headers} lets you specify custom HTTP headers. The headers must follow a certain format. Please read @uref{http://www.gnu.org/software/guile/manual/html_node/HTTP-Headers.html#Response-Headers,Response Headers} for details.

@code{#:mtime} specifies the modified time in the response. GNU Artanis will generate it for you when not defined.

@verbatim
(emit-response-with-file filename [headers <- '()])
@end verbatim

@strong{filename} is the filename to be sent as a response.

[headers] is the custom HTTP headers.

@node Running the server
@section Running the server
@anchor{#running-the-server}

@verbatim
(run #:host #f #:port #f #:debug #f #:use-db? #f
     #:dbd #f #:db-username #f #:db-passwd #f #:db-name #f)
@end verbatim

@emph{keywords with the value #f, as default, will get the values from the config file.}

But you can define them as well.

@code{#:host} the hostname.

@code{#:port} the socket port of the server.

@code{#:debug} set #t if you want to enable debug mode. Logs will be more verbose.

@code{#:use-db?} set #t if you want to use DB, and GNU Artanis will initialize DB connections.

@code{#:dbd} choose a dbd. These are the supported three: mysql, postgresql, and sqlite3.

@code{#:db-username} the username of your DB server.

@code{#:db-passwd} the DB password for the user above.

@code{#:db-name} the DB name to use.

@node Working with Nginx
@section Working with Nginx
@anchor{#working-with-nginx}
You may try GNU Artanis+Nginx with a reverse proxy.

@strong{@emph{Although GNU Artanis has good server core, I would recommend you use Nginx as the front server. In addition to the enhanced performance, it'll also be less vulnerable to attacks.}}

These are some sample lines for /etc/nginx/nginx.conf:

@verbatim
location / {
proxy_pass http://127.0.0.1:1234;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
@end verbatim

To make it work, restart Nginx after editing the file:

@verbatim
sudo service nginx restart
@end verbatim

And run GNU Artanis:

@verbatim
(run #:port 1234)
@end verbatim

@node The Art command line
@chapter The Art command line
@anchor{#the-art-command-line}
GNU Artanis provides the @strong{art} command line tool to save users' time.

@menu
* art create::
* art draw::
* art api::
* art migrate::
* art work::
@end menu

@node art create
@section art create
@anchor{#art-create}
If you want to set up your site/app in an application directory, and take advantage of the MVC features, you have to use this command to create the application directory first.

@verbatim
art create proj_path
@end verbatim

@node art draw
@section art draw
@anchor{#art-draw}
This command will generate the specified components:

@verbatim
Usage:
  art draw <component> NAME [options]

component list:
  model
  controller
  migration
  view
  lib

Options:
  -h, [--help]     # Print this screen
  -d, [--dry]      # Dry run but do not make any changes
  -f, [--force]    # Overwrite files that already exist
  -s, [--skip]     # Skip files that already exist
                   # If -s and -f are both provided, -f will be enabled
  -q, [--quiet]    # Suppress status output

Example:
  art draw model myblog
@end verbatim

Please see @emph{MVC} to learn more about how to use these components.

@node art api
@section art api
@anchor{#art-api}
This command is used to generate RESTful API skeleton.

@verbatim
Usage:
  art api [options]

Options:
  -h, [--help]     # Print this screen
  -d, [--dry]      # Dry run but do not make any changes
  -f, [--force]    # Overwrite files that already exist
  -s, [--skip]     # Skip files that already exist
                   # If -s and -f are both provided, -f will be enabled
  -q, [--quiet]    # Suppress status output
  -l, [--list]     # List all defined APIs
  -m, [--mode]     # Specify the WebAPI mode, the default is "restful"
  -v, [--ver]      # Specify the WebAPI version, omit it to auto detect
  -c, [--create]   # Create WebAPI

Example:
  art api -c
  art api -c -v v3
@end verbatim

Please see @emph{RESTful API} for more details.

@node art migrate
@section art migrate
@anchor{#art-migrate}
Migrate is used for Database migration.

@verbatim
Usage:
  art migrate operator name [OPTIONS]

Operators:
  up
  down

OPTIONS:
  VERSION=version
@end verbatim

Please see @emph{Migration} for more detail.

@node art work
@section art work
@anchor{#art-work}
This command is used to start the server when run in a project directory:

@verbatim
Usage:
  art work [options]

Options:
  -c, [--config=CONFIG]          # Specify config file
                                   Default: conf/artanis.conf
                                            if no, /etc/artanis/artanis.conf
  -h, [--host=HOST]              # Specify the network host
                                   Default: 0.0.0.0
  -d, [--usedb]                  # Whether to use Database
                                   Default: false
  -b, [--dbd=DBD]                # Specify DBD, mysql/postgresql/sqlit3
                                   Default: mysql
  -n, [--name=DATABASE_NAME]     # Database name
                                   Default: artanis
  -w, [--passwd=PASSWD]          # Database password
                                   Default: none
  -u, [--user=USER]              # Database user name
                                   Default: root
  -p, [--port=PORT]              # Specify listening port
                                   Default: 3000
  -g, [--debug]                  # Debug mode
                                   Default: disable
  -s, [--server=SERVER]          # Specify server core
                                   Default: Ragnarok (New server core since 0.2)
  --refresh                      # Clean caches, and force to re-compile all source code.
  --help                         # Show this screen
@end verbatim

@itemize
@item
For server core alternatives, please see @code{server.config} in config.
@item
For Database (DBD) alternatives, please see @code{database.config} in config.
@end itemize

@strong{NOTE:} Please make sure use @code{art work --refresh} to recompile WebApp code each time you upgrade GNU Artanis. And if you want to clean the caches for debug, @code{--refresh} is also your friend.

@node URL remapping
@chapter URL remapping
@anchor{#url-remapping}

@menu
* Introduction to URL remapping::
* URL handling::
* OPTIONS method::
* Get parameters from a URL::
* Redirecting::
@end menu

@node Introduction to URL remapping
@section Introduction to URL remapping
@anchor{#introduction-to-url-remapping}
URL remapping is used to modify a web URL's appearance to provide short, pretty or fancy, search engine friendly URLs. It's largely used in modern WAFs(web application framework) to provide RESTful web APIs.

@node URL handling
@section URL handling
@anchor{#url-handling}
According to RFC2616, the methods include GET, POST, etc. However, because of the naming conflict, GNU Artanis provides the methods by this list:

@itemize
@item
get
@item
post
@item
put
@item
patch
@item
page-delete
@item
page-options
@end itemize

@emph{In GNU Artanis the HEAD method is handled by the server, so you can't define specific handlers directly for it within GNU Artanis.}

Usage:

@verbatim
(method rule handler)
@end verbatim

And the handler could be one of two types, depending on your needs:

@verbatim
(lambda ()
  ...
  ret)

(lambda (rc)
  ...
  ret)
@end verbatim

@strong{ret} also has two types:

@itemize
@item
1. literal string as the returned response body

@item
2. See @emph{Emit a Response}

@end itemize

@verbatim
(get "/hello" (lambda () "hello world"))
@end verbatim

For a POST method:

@verbatim
(post "/auth" (lambda (rc) ...))
@end verbatim

@node OPTIONS method
@section OPTIONS method
@anchor{#options-method}
OPTIONS method is used to return the other methods supported by the server-side at the given URL. By default in GNU Artanis, for a specified URL, every method whose handler was registerted would be added to its own OPTIONS list.

If you want to deny certain methods for security consideration, you may need to set @strong{server.allowedmethods} in config.

@strong{NOTE:} If you're not trying to use Cross-Origin Resource Sharing (CORS), then you may not need to care about it.

@node Get parameters from a URL
@section Get parameters from a URL
@anchor{#get-parameters-from-a-url}

@verbatim
(params rc name)
;; e.g
(get "/hello/:who" (lambda (rc) (params rc "who")))
@end verbatim

@node Redirecting
@section Redirecting
@anchor{#redirecting}

@verbatim
(redirect-to rc path #:status 301 #:type '(text/html) #:headers '())
;; e.g
(get "/aaa" (lambda (rc) (redirect-to rc "/bbb")))
(get "/bbb" (lambda () "ok bbb"))
@end verbatim

The @strong{path} could be 2 possible patterns:

@itemize
@item
URI created by @uref{https://www.gnu.org/software/guile/manual/html_node/URIs.html,string->uri}. For example, @code{(redirect-to rc (string->uri "https://nalaginrut.com/feed/atom"))}.
@item
A relative path which will finally be converted to absolute URL append the @strong{host.addr}. For example, @code{(redirect-to rc "/login")}.
@end itemize

@strong{NOTE:} redirect-to will always use absolute URL implicitly. Although the standard supports relative URL, there maybe some problems when you're behind a proxy. So we use absolute URL to avoid these issues.

@node Route context
@chapter Route context
@anchor{#route-context}
Route context is a struct type object which encapsulates the necessary information for the server from the current request context. We named it @emph{route} because it's related to the route of @emph{URL remapping}. Usually it's passed to the page handler as a unique argument. It's supposed to provide sufficient data about the current request.

@verbatim
(HTTP-METHOD URL-rule (lambda (<route-context>) ...))
;; e.g:
(get "/hello" (lambda (rc) "world")) ; rc is <route-context> type
@end verbatim

@menu
* Route context APIs::
@end menu

@node Route context APIs
@section Route context APIs
@anchor{#route-context-apis}

@verbatim
(rc-path <route-context>)
@end verbatim

@itemize
@item
Get the requested path, that is to say, the actual URI visited by the client.
@end itemize

@verbatim
;; e.g
(get "/hello/world" (lambda (rc) (rc-path rc)))
;; visit localhost:3000/hello/world or from any port you specified
;; the result is "/hello/world".
(get "/hello/:who" (lambda (rc) (rc-path rc)))
;; visit localhost:3000/hello/world or from any port you specified
;; the result is "/hello/world".
@end verbatim

@verbatim
(rc-req <route-context>)
@end verbatim

@itemize
@item
Get the current HTTP request wrapped in record-type. About HTTP request please see @uref{https://www.gnu.org/software/guile/manual/html_node/Requests.html,HTTP Request}. It stores HTTP request of Guile.
@end itemize

@verbatim
(rc-body <route-context>)
@end verbatim

@itemize
@item
Get the current request body:
@itemize
@item
For a regular HTTP request, the body should be a bytevector;
@item
For a Websocket request, the body should be @emph{Websocket frame} as a record-type.
@end itemize

@end itemize

@verbatim
(rc-method <route-context>)
@end verbatim

@itemize
@item
Get the current requested HTTP method.
@end itemize

@verbatim
(rc-conn <route-context>)
@end verbatim

@itemize
@item
Get the current DB connection if you've requested one, please checkout @emph{DB shortcut}.
@end itemize

@verbatim
(rc-qt <route-context>)
@end verbatim

@itemize
@item
Get query table, which is a key-value list parsed from @emph{query string}.
@end itemize

@verbatim
(rc-handler <route-context>)
@end verbatim

@itemize
@item
Get the current request handler. The tricky part is that you can only get this handler within this handler unless you can go no where to run @emph{rc-handler} correctly.
@itemize
@item
It's on your own risk to use this API. But now that we have powerful first class lambda, you may do some magic. Well, depends on you.
@end itemize

@end itemize

@verbatim
(rc-mtime <route-context>) ; getter
(rc-mtime! <route-context>) ; setter
@end verbatim

@itemize
@item
You may set it in the handler to return you customized modified time. For static pages, the mtime is set automatically. But sometimes people may want to set it in a dynamic generated page.
@end itemize

@verbatim
(rc-cookie <route-context>)
@end verbatim

@itemize
@item
The cookies parsed from request header.
@end itemize

@verbatim
(rc-set-cookie! <route-context>)
@end verbatim

@itemize
@item
Set response cookie from server side. If you want to return cookies to the client, please use it.
@end itemize

There're other APIs in @emph{route-context}, but they're largely used for internals of Artanis, rarely useful for users. So we don't list them here.

@node Page status handling
@chapter Page status handling
@anchor{#page-status-handling}

@menu
* HTTP status code::
* The default behaviour of status handling::
* Dynamic status page generating::
@end menu

@node HTTP status code
@section HTTP status code
@anchor{#http-status-code}
HTTP response status codes are the error numbers show the status of a specific HTTP request. The responses are grouped in five classes:

@itemize
@item
Informational responses (100--199)
@item
Successful responses (200--299)
@item
Redirects (300--399)
@item
Client errors (400--499)
@item
Server errors (500--599)
@end itemize

@node The default behaviour of status handling
@section The default behaviour of status handling
@anchor{#the-default-behaviour-of-status-handling}
On success, GNU Artanis returns the status code 200.

If there's any issue prevents GNU Artanis to generate requested result successfully, then it'll throw the relavent exception somewhere issue happened. And finally it will be caught by the server-core. In theory, the status code other than 200 will trigger a system page generating operation. By default, status pages are put in @strong{/etc/artanis/pages}, but you may override it in @strong{your_app_folder/sys/pages}. Each system page named with the status code, say, @strong{404.html}. So you may easily customize your preferred status page.

@node Dynamic status page generating
@section Dynamic status page generating
@anchor{#dynamic-status-page-generating}
Sometimes you may need more complex status page generating. For example, you want to put a random public service advertising when the visitors encountered a 404 (page missing). Then you can do it like this:

@verbatim
(http-status status-code thunk-handler)
@end verbatim

@itemize
@item
@strong{status-code} must be an integer between 100~599.
@item
@strong{Thunk} implies a function without any argument.
@end itemize

Let's see an example:

@verbatim
;; You may put this code in any controller module.
(http-status 404
 (lambda ()
   (view-render "psd_404" (the-environment))))
@end verbatim

In this example, "psd" stands for public-service-advertising, and "psd_404" tells the @strong{view-render} to find "psd_404.html" in @strong{your_app_folder/sys/pages/psd_404.html}. So you can render the HTML template to generate a status page. Of course, there're a few ways to implement the similar idea. If you don't want to render template on the backend, you may generate a JSON and let the frontend framework to generate it.

@node MVC
@chapter MVC
@anchor{#mvc}
MVC is Model-View-Controller, the most classic architectural pattern for implementing user interfaces. It divides a given software application into three interconnected parts, so as to separate internal representations of information from the ways that information is presented to or accepted from the user.

@menu
* Controllers/Views::
* Models::
* Model mapping::
@end menu

@node Controllers/Views
@section Controllers/Views
@anchor{#controllersviews}
Let's make a controller named @emph{article}:

@verbatim
art draw controller article show edit
@end verbatim

@emph{show} and @emph{edit} are the name of methods for the controller named @emph{article}.

This will generate both a @strong{controller} and a @strong{view} for @emph{article}:

@verbatim
drawing    controller article
working    Controllers `article.scm'
create     app/controllers/article.scm
working    Views `article'
create     app/views/article/show.html.tpl
create     app/views/article/edit.html.tpl
@end verbatim

These three files are generated:

@verbatim
app/controllers/article.scm
app/views/article/show.html.tpl
app/views/article/edit.html.tpl
@end verbatim

Based on this, the controller @emph{article} will have two methods mapped to the URL rule, @emph{show} and @emph{edit}. As part of the @emph{view} component. An HTML template is generated for each method. For the @emph{show} method for example, the view file @strong{show.html.tpl} is created. For the @emph{controller} component, you get a @emph{show} method handler, as:

@verbatim
(article-define show
  (lambda (rc)
    "<h1>This is article#show</h1><p>Find me in app/views/article/show.html.tpl</p>"
    ;; TODO: add controller method `show'
    ;; uncomment this line if you want to render view from template
    ;; (view-render "show")
    ))
@end verbatim

Of course, you're free to use or not use these templates. If you want to use the @emph{view template}, just uncomment the last line @code{(view-render "show")}.

@strong{NOTE:} The views template generated by MVC will defaultly announce FreeJS. The reason was well explained in @emph{The Javascript Trap}. It's optional, you may remove it with your free will, but I put it there in the hope that you can support free software with us.

For more detail about template in Views, please see @emph{Layouts and Rendering in GNU Artanis}.

@node Models
@section Models
@anchor{#models}
Models contains operations of database.

@menu
* Create models::
* Define table in Model::
* Tables dependencies::
* Model API::
@end menu

@node Create models
@subsection Create models
@anchor{#create-models}
A Model a is usually connected to the same named Controller. However, in practical, users may create multiple Models for one Controller. Hence, GNU Artanis creates Model outside Controller, rather than autogenerate a Model when creating Controller.

@verbatim
art create mytest
cd mytest

# Create model
art draw model user
# drawing    model user
# working    Models `user.scm'

# check result
ls app/models/
# user.scm
@end verbatim

In this case, we created a Model named @strong{@strong{user}}. A file named @strong{@strong{user.scm}} is in @strong{@strong{app/models}} now, let's open it:

@verbatim
(import (artanis mvc model))
;; Model user definition of mytest
;; Please add your license header here.
;; This file is generated automatically by GNU Artanis.
(create-artanis-model user) ; DO NOT REMOVE THIS LINE!!!
@end verbatim

It's done! These code are self-explain.

Next, let's try to define an @strong{@strong{user}} table.

@node Define table in Model
@subsection Define table in Model
@anchor{#define-table-in-model}
In this example, we craete a table like this:

@verbatim
+-------------------------------------------------------------+
| Field | Type        | Null | Key | Default | Extra          |
|-------+-------------+------+-----+---------+----------------|
| id    | auto        | NO   | PRI | NULL    | auto_increment |
| name  | char-field  | NO   |     | NULL    |                |
| email | char-field  | NO   |     | NULL    |                |
+-------------------------------------------------------------+
@end verbatim

For Model definition syntax in GNU Artanis, we have:

@verbatim
(create-artanis-model
 user  ; model name
 (:deps) ; deps list
 (id auto (#:primary-key))
 (name char-field (#:not-null #:maxlen 128))
 (email char-field (#:not-null #:maxlen 128))
 )
@end verbatim

@strong{@strong{NOTE:}} If you have definition, and you've imported in any controller, when booting GNU Artanis with src_scheme[:exports code](art work), then the defined table will be created automatically in the specified database. Don't worry, it will be created only once.

@strong{@strong{NOTE:}} The Model name @strong{@strong{must}} be the same with Model file's name.

@node Tables dependencies
@subsection Tables dependencies
@anchor{#tables-dependencies}
Since the tables will be auto-created when booting the server core, however, the MVC doesn't know the dependency chain. To help the auto-creation doing correct work, you may want to specify the table dependencies in the table definition:

For example, you have another Model named @strong{@strong{article}} which is designed for your blog system. Obviously, it's related to @strong{@strong{user}} table since every article requires at least one user as an author.

@verbatim
(create-artanis-model
 article  ; model name
 (:deps user) ; deps list
 (id auto (#:primary-key))
 (author_id tinyint (#:not-null))
 (title char-field (#:not-null #:maxlen 128))
 (timestamp bigint (#:unsigned #:not-null))
 )
@end verbatim

You may realized we've added src_scheme[:exports code]((:deps user)) to hint the MVC system that @strong{@strong{article}} table should be created after @strong{@strong{user}} table. So when it meets @strong{@strong{article}} Model during booting, it'll make sure to create @strong{@strong{user}} then @strong{@strong{article}}.

@strong{@strong{NOTE:}}: src_scheme[:exports code]((:deps models_list @dots{})) @strong{@strong{NOTE:}} This feature is useful when you do DB migration.

@node Model API
@subsection Model API
@anchor{#model-api}
The model definition API:

src_scheme[:exports code]((create-artanis-model model_name (:deps deps_list @dots{}) fields_def @dots{}))

The @strong{@strong{fields_def}} syntax is:

src_scheme[:exports code]((field_name type (options @dots{})))

@enumerate 
@item
Field Type

To unify API amoung all the DB backend supported by GNU Artanis, we provide abstract types:

@itemize
@item
auto: auto incremental index
@item
tiny-integer:
@item
small-integer:
@item
big-integer: 0~2^64
@item
text: long text
@item
boolean:
@item
float
@item
double
@item
char-field: varchar
@itemize
@item
you need to specify src_scheme[:exports code](#:maxlen positive_integer) in the options.
@end itemize

@item
date-field
@itemize
@item
These options imply src_scheme[:exports code](#:no-edit)
@item
src_scheme[:exports code](#:auto-now)
@item
src_scheme[:exports code](#:auto-now-once)
@end itemize

@end itemize

@strong{@strong{NOTE:}} if these types can't meet your requirements, just write the known type for your configured DB backend supported types, it will be passed directly to the backend, however, without the framework specific feature like type checking.

The @strong{@strong{options}} has specific syntax for different DB backend.

@item
MySQL/MariaDB options

The specific options are:

@itemize
@item
src_scheme[:exports code](#:not-null)
@item
src_scheme[:exports code](#:null)
@item
src_scheme[:exports code](#:default) to speficy default value.
@item
src_scheme[:exports code](#:unique)
@item
src_scheme[:exports code](#:unique-key)
@item
src_scheme[:exports code](#:primary-key)
@item
src_scheme[:exports code](#:key) to specify keys.
@item
src_scheme[:exports code](#:auto-incremental)
@item
src_scheme[:exports code](#:comment) for commenting.
@item
src_scheme[:exports code](#:column-format)
@item
src_scheme[:exports code](#:storage mode) DISK or MEMORY mode.
@item
src_scheme[:exports code](#:signed)
@item
src_scheme[:exports code](#:unsigned)
@item
src_scheme[:exports code](#:zerofill)
@end itemize

@item
PostgreSQL options

PostgreSQL migration hasn't been supported yet@dots{}patches are welcome.

@item
SQLite3 options

@itemize
@item
src_scheme[:exports code](#:not-null)
@item
src_scheme[:exports code](#:null)
@item
src_scheme[:exports code](#:default value)
@item
src_scheme[:exports code](#:unique)
@item
src_scheme[:exports code](#:unique-key)
@item
src_scheme[:exports code](#:primary-key)
@item
src_scheme[:exports code](#:key)
@item
src_scheme[:exports code](#:auto-increment)
@item
src_scheme[:exports code](#:auto-now-once) for timestamp.
@end itemize

@end enumerate

@node Model mapping
@section Model mapping
@anchor{#model-mapping}
If you defined a model, for example, an article table:

@verbatim
(create-artanis-model
 article  ; model name
 (:deps user) ; deps list
 (id auto (#:primary-key))
 (author_id tinyint (#:not-null))
 (title char-field (#:not-null #:maxlen 128))
 (timestamp bigint (#:unsigned #:not-null))
 )
@end verbatim

There will be a closure named @strong{@strong{$article}} in this module, and could be exported. The @strong{@strong{$}} sign is auto added to the name of the table name. We call it @strong{@strong{model mapping}} so that you can control this table easily.

@menu
* Table get::
* Table set::
* Table existance::
* Table schema get::
* Table create::
* Table remove::
* Table modify::
* Further reading::
@end menu

@node Table get
@subsection Table get
@anchor{#table-get}

@verbatim
($article 'get #:columns (columns ...)
               #:functions '() ; put SQL functions here, e.g, '((count Article.name))
               #:ret 'all ; values: top | all | postive-number
               #:group-by '(column-name ...)
               #:order-by '(column-name ... asc) ;; or '(column-name ... desc)
               #:condition "" ; (where #:name "nala")
               #:foreach '() ; #:foreach '(city ("sz" "bj" "sh"))
               #:dump #f ; set #t if you want to check the SQL string
               #:mode 'raw ; raw to return all rows; getter to return a getter function)
@end verbatim

@node Table set
@subsection Table set
@anchor{#table-set}
You can update value to field in a natural way.

@verbatim
($article 'set #:key value ...)

;; for example
($article 'set #:title "new title" #:timestamp (current-time)
               #:condition (where #:id 1))
@end verbatim

@node Table existance
@subsection Table existance
@anchor{#table-existance}
Check table existance:

@verbatim
($article 'table-exists?)
@end verbatim

Check fields existance:

@verbatim
($article 'exists? fields ...)
@end verbatim

For the case-insensitively.

@verbatim
($article 'ci-exists? fields ...)
@end verbatim

@node Table schema get
@subsection Table schema get
@anchor{#table-schema-get}

@verbatim
($article 'schema)
@end verbatim

@node Table create
@subsection Table create
@anchor{#table-create}

@verbatim
($article 'create fields-definitions ...)
@end verbatim

@strong{NOTE: This is the low-level API called by the model or migration to create a new table. It is not recommended you to use it directly.}

@node Table remove
@subsection Table remove
@anchor{#table-remove}

@verbatim
($article 'remove)
@end verbatim

@strong{NOTE: This is the low-level API called by the model or migration to remove a table. It is not recommended you to use it directly.}

@node Table modify
@subsection Table modify
@anchor{#table-modify}

@verbatim
($article 'mod definitions ...)
@end verbatim

@strong{NOTE: This is the low-level API called by the model or migration to modify a table. It is not recommended you to use it directly.}

@node Further reading
@subsection Further reading
@anchor{#further-reading}
For modifying tables, you should read @emph{Migration}.

For other DB operation, please read @emph{FPRM}.

@node Query String
@chapter Query String
@anchor{#query-string}
A query string is a special part of a URL:

@verbatim
http://example.com/over/there?name=ferret&color=purple
@end verbatim

In this example, "name" and "color" are query strings with the values "ferret" and "purple" respectively. It's useful to pass parameters to the server side like this.

GNU Artanis provides a convenient API to handle query strings.

@menu
* Query string from GET::
* Query string from POST::
@end menu

@node Query string from GET
@section Query string from GET
@anchor{#query-string-from-get}
The query string would be encoded in the URL on the GET method.

@verbatim
http://example.com/over/there?name=ferret&color=purple
@end verbatim

Please notice that URL-remapping supports regex. So you could register a URL rule like this:

@verbatim
(get "/there?"
  (lambda (rc)
    (get-from-qstr rc "name")))
@end verbatim

Or it will throw 404 since URL-remapping failed to hit the rule with the query string.

@node Query string from POST
@section Query string from POST
@anchor{#query-string-from-post}
The query string would be encoded in the HTTP body on the POST method.

There's only a slight difference when you pass query string by POST instead of with GET: you don't need a URL rule using a regex, so the "?" is unnecessary here.

@verbatim
(post "/there" #:from-post 'qstr
      (lambda (rc)
        (:from-post rc 'get "name")))
@end verbatim

Please notice that @code{#:from-post 'qstr} is necessary when you're trying to get data from POST. And you should use @code{:from-post} to get related data from query-string.

@verbatim
#:from-post <mode>
@end verbatim

The mode includes:

@itemize
@item
@code{#t} or @code{'qstr}: handle query-string for you.
@item
@code{'json}: returns a parsed json as hashtable.
@item
@code{'qstr-safe}: similar to 'qstr, but try to eliminate evil HTML entities first.
@item
@code{'bv}: returns the body as bytevector.
@item
@code{'store rest ...}: It's for @emph{Upload files}.
@end itemize

The reason to design :from-post is for the efficient purpose. Artanis will not try to auto parse POST body as query-string for at least 2 reasons:

@itemize
@item
1. It may not be query-string, maybe json, or uploaded file
@item
2. It may be long query-string, and could be deleyed to parse. This is
@end itemize

useful to avoid redundant parsing. However, each time you call @code{:from-post}, it will parse the query-string again, the correct way to fetch multiple values is:

@verbatim
(:from-post rc 'get-vals "key1" "key2" "key3")

;; For example:
;; let-values is imported from srfi-11
(let-values (((title sub-title old-passwd new-passwd)
              (:from-post rc 'get-vals "title" "sub-title"
                          "old-passwd" "new-passwd")))
  ..........
  )
@end verbatim

BTW, you may get the parsed query-string as an assoc-list as well:

@verbatim
(let ((qstr (:from-post rc 'get)))
  (assoc-ref qstr "key-1"))
@end verbatim

@node Layouts and Rendering in GNU Artanis
@chapter Layouts and Rendering in GNU Artanis
@anchor{#layouts-and-rendering-in-gnu-artanis}

@menu
* Templating::
* Templating for Pythoners::
* Templating for Rubyists::
* Templating APIs::
* Embedded Templates::
* SXML Templates::
@end menu

@node Templating
@section Templating
@anchor{#templating}
Templating provides a way to mix programmatic code into HTML.

@node Templating for Pythoners
@section Templating for Pythoners
@anchor{#templating-for-pythoners}
If you're familiar with Django, which implemented a DSL(Domain Specific Language) to express presentation rather than program logic. Templating in GNU Artanis follows a different philosophy.

Templating in GNU Artanis, is just writing Scheme code in the HTML document. Why? Because of the philosophy of FP(Functional Programming), everything is a function. So, @code{(filesizeformat size)} is easy enough to grasp for anyone with scheme experience. It's just a simple function calling in prefix-notation. There's no need to implement DSL like @code{size|filesizeformat} to increase the complexity of code. Let alone the syntax is very different from Python.

The syntax @code{size | filesizeformat} follows postfix-notation, used in stack-based languages, say Forth. Such a language used to delegate another programming paradigm named concatenative programming. It's very different from the paradigm of Scheme (functional programming), and the paradigm of Python (imperative programming).

The philosophy of GNU Artanis templating is to bring it into correspondence with the paradigm of the language. And reduce unnecessary complexities. @uref{http://en.wikipedia.org/wiki/KISS_principle,KISS}.

@node Templating for Rubyists
@section Templating for Rubyists
@anchor{#templating-for-rubyists}
Templating in GNU Artanis looks very similar to Rails.

The Rails code:

@verbatim
<% if( @fullscreen == 1 ) %>
<%= "<div class='full'><p>...</p></div>" %>
<% end %>
@end verbatim

And the same function in GNU Artanis code:

@verbatim
<% (if (= fullscreen 1) %>
       <% "<div class='full'><p>...</p></div>" %>
       <% ) %>
@end verbatim

@node Templating APIs
@section Templating APIs
@anchor{#templating-apis}

@verbatim
(tpl->response filename/sxml [environment <- (the-environment)] [escape? <- #f])

(tpl->html filename/sxm [environment <- (the-environment)] [escape? <- #f])
@end verbatim

@emph{The difference is that tpl->html returns a string, but tpl->response returns an HTTP object response.}

[environment] is the environment you want to use. We often ignore it. If you want to ref some vars defined outside your template string, you need to pass this variable.

[escape?] If you want to char-escape the HTML with the returned string, set it to #t.

There are two main ways of writing templates:

@node Embedded Templates
@section Embedded Templates
@anchor{#embedded-templates}
Example: Write a tpl file named "my.tpl":

@verbatim
<html>
  <p> <%= "This is tpl test!" %> </p>
  <p> <% (format #t "And this is ~a" (getcwd)) %> </p>
  <p> <%= external-var %> </p>
</html>
@end verbatim

The filename extension ".tpl" is mandatory when using the MVC. Since the MVC will find the template by detecting controller name automatically.

If you don't use the MVC, and are rather writing all in one script file loading GNU Artanis modules. Then you don't need to follow this rule.

@strong{NOTE:} Don't wrap code in double-quotes, for example:

@verbatim
<a href="<%= my-url %>">click me</a> <!-- Wrong! -->
<a href=<%= my-url %> >click me</a> <!-- Correct! -->
@end verbatim

If you need to output a double-quoted string, please use @code{object->string} to convert in Scheme first.

@verbatim
<a href=<%= (object->string my-url) %> >click me</a> <!-- If my-url is not properly quoted -->
@end verbatim

@verbatim
(get "/test"
  (lambda (rc)
    (let ((external-var 123))
      (tpl->response "my.tpl" (the-environment)))))
(run #:port 8080)
@end verbatim

In this case, make sure to put my.tpl in the same path as your GNU Artanis code.

Since @strong{external-var} is defined outside the file "my.tpl", and it's bound in @emph{let} with 123, you have to pass (the-environment). Or the template render will complain aobut not being able to find the variable @strong{external-var}.

If you don't need to refer to any external vars, just use @code{(tpl->response "file.tpl")}.

To test, access @url{http://localhost:3000/test} in your browser.

@menu
* Template special commands::
@end menu

@node Template special commands
@subsection Template special commands
@anchor{#template-special-commands}
GNU Artanis provides special helper commands.

Please notice that GNU Artanis constrains the path of sources in the application directory for security reasons. The resources files, CSS, JS etc, should be put int @strong{pub} directory in the application directory, or the client won't be able to access them.

These special commands are useful to expand the path for you, and they should be added into the tamplate file, for example:

@verbatim
<html>
  <head>
    <@icon favicon.ico %>
      <@js functions.js %>
        <@css blog.css %>
  </head>

  <@include sidebar.html %>

    <body>
      ...
    </body>
</html>
@end verbatim

@strong{NOTE:} The command name is prefixed with @strong{@@}, as, @strong{@@include}, @strong{@@css}, etc. Please do not seperate the @strong{@@}, or it will throw exception.

You can include html files with the @strong{include} command:

@verbatim
;; @include is the command name, not <@ include filename %>
<@include filename.html %>
@end verbatim

This will be expanded like this:

@verbatim
/current_toplevel/pub/filename.html
@end verbatim

@strong{NOTE:} Please make sure the included file is in the @strong{pub} directory in the application directory.

To refer to a CSS file:

@verbatim
<@css filename.css %>
@end verbatim

This will be expanded like this:

@verbatim
<link rel="stylesheet" href="/css/filename.css">
@end verbatim

To refer to a JS (javascript) file in the HTML head:

@verbatim
<@js filename.js %>
@end verbatim

This will be expanded like this:

@verbatim
<script type="text/javascript" src="/js/filename.js"> </script>
@end verbatim

To specify an icon for the domain:

@verbatim
<@icon favicon.ico %>
@end verbatim

This will be expanded like this:

@verbatim
<link rel="icon" href="/img/favicon.ico" type="image/x-icon">
@end verbatim

@node SXML Templates
@section SXML Templates
@anchor{#sxml-templates}
@uref{http://en.wikipedia.org/wiki/SXML,SXML} is an alternative syntax for writing XML data, using the form of S-expressions.

SXML is to Scheme as JSON is to ECMAScript(the so-called Javascript). Maybe this explains it clearer.

One benefit of SXML is that it takes advantage of the quasiquote in Scheme. Please search in the internet "scheme quasiquote" for more details.

This is an SXML expression:

@verbatim
(tpl->response '(html (body (p (@ (id "content")) "hello world"))))
@end verbatim

The above would result the following HTML code:

@verbatim
<html><body><p id="content">hello world</p></body></html>
@end verbatim

Sometimes you may need quasiquote to refer to a variable, for example:

@verbatim
(let ((content "hello world"))
  (tpl->response `(html (body (p (@ (id "content")) ,content)))))
@end verbatim

Here, the "html" block is being quoted with the backtick, which in combination with a "," character before the variable name, makes the variable be referred to instead of just passing a string.

@node Databases
@chapter Databases
@anchor{#databases}

@menu
* DB init hooks::
* DB connection pool::
* Migration::
* ORM problem::
* SSQL::
* FPRM experimental::
* SQL Mapping experimental::
@end menu

@node DB init hooks
@section DB init hooks
@anchor{#db-init-hooks}
Sometimes you need to do some configurations before using DB, Artanis provide an API for that, you should put it to ENTRY file before run the server.

For example, assuming you're using MySQL/MariaDB, and you need to configure it to UTF-8, you should add these lines to you ENTRY file. Or if you're using minimal mode without application folder, just put it before running the server.

@verbatim
(run-when-DB-init!
 (lambda (conn)
   (DB-query conn "set names utf8;")))
@end verbatim

@strong{NOTE: Don't forget `!' here, it implies the side-effects in Scheme!}

@node DB connection pool
@section DB connection pool
@anchor{#db-connection-pool}
GNU Artanis will create a connection pool when you run @code{art work}. Its size is decided by @code{db.poolsize}. The pool is increasing by default for development and test. It is strongly recommended you to set @code{db.pool} to @code{fixed} when you deploy. The increasing pool will cause the effects that is similar to the memory leak. If you set it to fixed, then the request handler will be safely scheduled when there's no available DB connection, and it will be awake when it's available.

@node Migration
@section Migration
@anchor{#migration}
Migrations provide a way to do complicated modification of tables in a database by GNU Artanis. Here's an example.

First, draw a migration:

@verbatim
# art draw migration person
drawing    migration person
working    Migration `20151107040209_person.scm'
@end verbatim

You'll see something similar like above.

Then you'd edit the file db/migration/20151107040209_person.scm:

@verbatim
(migrate-up
 (create-table
  'person
  '(id auto (#:primary-key))
  '(name char-field (#:not-null #:maxlen 10))
  '(age tiny-integer (#:not-null))
  '(email char-field (#:maxlen 20))))

(migrate-down
 (drop-table 'person))
@end verbatim

Then you run the @strong{up} command for migration:

@verbatim
art migrate up person
@end verbatim

Then migrate-up function will be called, and this will create a table named @emph{person}:

@verbatim
+-------+---------------------+------+-----+---------+----------------+
| Field | Type                | Null | Key | Default | Extra          |
+-------+---------------------+------+-----+---------+----------------+
| id    | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | varchar(10)         | NO   |     | NULL    |                |
| age   | tinyint(4)          | NO   |     | NULL    |                |
| email | varchar(20)         | YES  |     | NULL    |                |
+-------+---------------------+------+-----+---------+----------------+
@end verbatim

If you run the @strong{down} command of migration, as:

@verbatim
art migrate down person
@end verbatim

The table @emph{person} will be dropped.

@node ORM problem
@section ORM problem
@anchor{#orm-problem}
ORM stands for Object Relational Mapping, which is a popular approach to handle relational DB nowadays, especially for Object-Oriented Programming.

Of course, Guile has it's own Object System named @uref{https://www.gnu.org/software/guile/manual/html_node/GOOPS.html#GOOPS,GOOPS}. Users can use OOP with it. And it's possible to implement ORM in GNU Artanis as well.

However, FP fans realized that they don't have to use OOP if they can use FP features reasonably.

Besides, there're some critics about ORM:

@itemize
@item
@uref{http://martinfowler.com/bliki/OrmHate.html,ORM Hate}

@item
@uref{http://blogs.tedneward.com/2006/06/26/The+Vietnam+Of+Computer+Science.aspx,Vietnam of Computer Science}

@item
@uref{http://blog.codinghorror.com/object-relational-mapping-is-the-vietnam-of-computer-science/,Object-Relational Mapping is the Vietnam of Computer Science}

@end itemize

Here are some known ways to solve ORM related problems:

@itemize
@item
1. @strong{@emph{Give up ORM}}.
@end itemize

@itemize
@item
2. @strong{@emph{Give up relational storage model}}. Don't use a relational DB. Use another DB style, such as No-SQL. Well, this way is not cool when you must use a relational DB.
@end itemize

@itemize
@item
3. @strong{@emph{Manual mapping}}. Write SQL code directly. It's fine sometimes. But the code increases when things get complicated. Refactoring and reusing would be worth to consider.
@end itemize

@itemize
@item
4. @strong{@emph{Limited ORM}}. Limiting the utility of ORM. And use ORM to solve part of your work rather than whole, depends on you. This may avoid some problems.
@end itemize

@itemize
@item
5. @strong{@emph{SQL related DSL}}. Design a new language. Microsoft's LINQ is such a case.
@end itemize

@itemize
@item
6. @strong{@emph{Integration of relational concepts into frameworks}}. Well, harder than 5, but worth to try.
@end itemize

@itemize
@item
7. @strong{@emph{Stateless}}. This is the critical hit to counter complexity and unreliability.
@end itemize

Basically, GNU Artanis has no ORM yet, and maybe never will. GNU Artanis is trying to experiment new ways to solve the problems of ORM.

GNU Artanis provides three ways to complete this mission. All of them, are @strong{experimental} at present.

@itemize
@item
SSQL (1,3,5)

@item
FPRM (4,7)

@item
SQL Mapping (1,3,6)

@end itemize

@node SSQL
@section SSQL
@anchor{#ssql}
The concept of SSQL is to write SQL in @uref{https://en.wikipedia.org/wiki/S-expression,s-expression}.

Usage:

@verbatim
(->sql sql-statement)
(where #:key val ... [literal string])
(having #:key val ... [literal string])
(/or conds ...)
(/and conds ...)
@end verbatim

For example:

@verbatim
(->sql select * from 'Persons (where #:city "Tokyo"))
(->sql select '(age name) from 'Persons (where "age < 30"))
@end verbatim

The SQL update command is quite different to SQL grammar. Example:

@verbatim
(->sql update 'table set (list (list phone_number "08080001234")) (where #:name "john"))
@end verbatim

@node FPRM experimental
@section FPRM (experimental)
@anchor{#fprm-experimental}
FPRM stands for Functional Programming Relational Mapping. It's a new word I invented. But it's not new concept. FP here indicates @strong{stateless}.

@emph{FPRM is still experimental and work-in-progress.}

@menu
* Connect to DB server::
* Map DB table::
* Create table::
* Get columns from table::
* Set values to table::
* Drop a table::
* Check existence of table::
* Get schema of a table::
@end menu

@node Connect to DB server
@subsection Connect to DB server
@anchor{#connect-to-db-server}

@verbatim
;; usage 1:
(connect-db dbd init-str)

;; usage 2:
(connect-db dbd #:db-name "artanis" #:db-username "root" #:db-passwd ""
            #:proto "tcp" #:host "localhost" #:port 3306)
@end verbatim

@itemize
@item
@strong{dbd} is a string. It must match any of these: "mysql", "postgresql", or "sqlite3".
@end itemize

@itemize
@item
@strong{init-str} is a string for DB init, for example:
@end itemize

@verbatim
(connect-db "mysql" "root:123:artanis:tcp:localhost:3306")
@end verbatim

@itemize
@item
@code{#:db-name} specifies the DB name.
@end itemize

@itemize
@item
@code{#:db-username} specifies the DB username.
@end itemize

@itemize
@item
@code{#:proto} specifies the socket protocol, which is related to the DB server of your choice.
@end itemize

@itemize
@item
@code{#:host} specifies the host name.
@end itemize

@itemize
@item
@code{#:port} specifies the socket port.
@end itemize

@node Map DB table
@subsection Map DB table
@anchor{#map-db-table}
This step will generate an new instance (as a closure) mapped to database table or view. In ORM, it is often called an @uref{http://www.martinfowler.com/eaaCatalog/activeRecord.html,Active Record.} It maps the database view to a class object.

There are two main differences to SSQL:

@itemize
@item
FPRM doesn't create an object for each table. It maps the whole database, and generates an SQL for each table as you use it. So it might be lighter compared to an ORM object.
@item
FPRM doesn't maintain any states at all. It's stateless as an object (Not in the database).
@end itemize

These two points may decrease the power of FPRM, but our main philosophy in GNU Artanis is that

@itemize
@item
@emph{The best way to control DB is SQL, don't bother with other guile schemes.}
@end itemize

That means we're not going to develop a complicated ORM in GNU Artanis, but a promising way to interact with SQL easily. This is what @emph{SQL Mapping} provided. FPRM aims to reduce states & complexity to provide reliability. And SQL-Mapping will provide a convenient way to handle complex SQL queries for better performance and security (SQL-Injection, etc).

@verbatim
(define m (map-table-from-DB rc/conn))
@end verbatim

@strong{rc/conn} can be a route-context or a DB connection.

map-table-from-DB returns a function, named @strong{m} here for simplicity.

@node Create table
@subsection Create table
@anchor{#create-table}

@verbatim
(m 'create table-name defs #:if-exists? #f #:primary-keys '() #:engine #f)
@end verbatim

@itemize
@item
@strong{table-name} specifies the name of the DB table.
@end itemize

@itemize
@item
@strong{defs} is a list to define the type of columns. For example:
@end itemize

@verbatim
'((name varchar 10) (age integer) (email varchar 20))
@end verbatim

@itemize
@item
@code{#:if-exists?} has two kinds of possible options:
@itemize
@item
'@strong{overwrite} and '@strong{drop}, which will overwrite the existing table.
@item
'@strong{ignore} means to ignore the table when it already exists.
@end itemize

@item
@code{#:primary-keys} specifies the primary keys in the created table.
@end itemize

@itemize
@item
@code{#:engine} specifies the DB engine. It depends on what dbd you chose.
@end itemize

@node Get columns from table
@subsection Get columns from table
@anchor{#get-columns-from-table}

@verbatim
(m 'get table-name #:columns '(*) #:functions '() #:ret 'all
   #:group-by #f #:order-by #f)
@end verbatim

@itemize
@item
@code{#:column} is the list of columns to get.
@end itemize

@itemize
@item
@code{#:functions} is a function to call, e.g:
@end itemize

@verbatim
#:functions '((count Persons.Lastname))
@end verbatim

@itemize
@item
@code{#:ret} specifies how to return the result, there are three options:
@itemize
@item
'all for returning all results
@item
'top for returning the first result
@item
integer (larger than 0), to give it the end of a range from 0 to this number to give as result.
@end itemize

@item
@code{#:group-by} used in conjunction with the aggregate functions to group the result-set by one or more columns.
@end itemize

@itemize
@item
@code{#:order-by} used to sort the result-set by one or more columns.
@end itemize

For example, to get Lastname and City column, and return the first result.

@verbatim
(m 'get 'Persons #:columns '(Lastname City) #:ret 'top)
@end verbatim

@node Set values to table
@subsection Set values to table
@anchor{#set-values-to-table}

@verbatim
(m 'set table-name . kargs)
@end verbatim

@strong{kargs} is a var-list that takes key-value arguments.

For example:

@verbatim
(m 'set 'Persons #:name "nala" #:age 99 #:email "nala@artanis.com")
@end verbatim

@node Drop a table
@subsection Drop a table
@anchor{#drop-a-table}

@verbatim
(m 'drop table-name)
@end verbatim

@node Check existence of table
@subsection Check existence of table
@anchor{#check-existence-of-table}

@verbatim
;; case sensitive
(m 'exists? table-name . columns)
;; or for case-insensitive
(m 'ci-exists? table-name . columns)
@end verbatim

For example:

@verbatim
(m 'exists? 'Persons 'city 'lastname)
@end verbatim

@node Get schema of a table
@subsection Get schema of a table
@anchor{#get-schema-of-a-table}

@verbatim
(m 'schema table-name)
@end verbatim

@emph{NOTE: all the returned names of the schema will be in lowercase.}

@node SQL Mapping experimental
@section SQL Mapping (experimental)
@anchor{#sql-mapping-experimental}
To be continued @dots{}

@node RESTful API
@chapter RESTful API
@anchor{#restful-api}
GNU Artanis provides a command @code{art api} to generate RESTful API skeleton, and the API version can be managed.

@verbatim
art api -c

creating   restful API v1
create     app/api/v1.scm
@end verbatim

The generated file @strong{app/api/v1.scm} will contain such a line:

@verbatim
(define-restful-api v1) ; DO NOT REMOVE THIS LINE!!!
@end verbatim

@strong{v1} is the default version, if you may specify your prefered API version, for example, @strong{v3}:

@verbatim
art api -c -v v3
@end verbatim

@code{-c} means to create.

You should use @code{api-define} to define the RESTful API:

@verbatim
(api-define rule handler)
@end verbatim

@code{api-define} is an overloaded method of the controller define function. So you may use it like what you do with the controller in MVC. Here's a pseudo example:

@verbatim
(define (auth-err)
  (scm->json `((status ,status) (reason "Auth error!"))))

(api-define article/list
  (options #:mime 'json #:with-auth auth-err)
  (lambda (rc) ...))
@end verbatim

Although the registered URL is @code{"article/list"}, the actual API would be @code{"v1/article/list"}, depends on the specified API version.

@node MIME
@chapter MIME
@anchor{#mime}
@code{#:mime} method is used to return the proper MIME type in the HTTP response.

@verbatim
#:mime type ; for registering type
(:mime rc body) ; to emit the reponse with the proper MIME
@end verbatim

@menu
* JSON::
* CSV::
* XML::
* SXML::
@end menu

@node JSON
@section JSON
@anchor{#json}
GNU Artanis integrates the third-party module @uref{https://github.com/aconchillo/guile-json,guile-json} to parse json. You can use the #:mime method to handle JSON:

@verbatim
(get "/json" #:mime 'json
     (lambda (rc)
       (let ((j (scm->json-string '(("name" . "nala") ("age" . 15)))))
         (:mime rc j))))
@end verbatim

For example:

@verbatim
(define my-json
  '((name . nala) (age . 15)
    (read_list
     ("book1" "The interpreter and structure of Artanis")
     ("book2" "The art of Artanis programming"))))
(scm->json-string my-json) ; scm->json will print a json string
;; ==> {"name" : "nala",
;;      "age" : 15,
;;      "read_list" : {"book2" : "The art of Artanis programming",
;;                     "book1" : "The interpreter and structure of Artanis"}}
@end verbatim

@code{scm->json} will return a hash-table to represent a JSON object.

If you need to format JSON as a string to return to the client, please use @code{scm->json-string}.

@node CSV
@section CSV
@anchor{#csv}
GNU Artanis integrates the third-party module @uref{https://github.com/NalaGinrut/guile-csv,guile-csv} to parse csv. You can use the #:mime method to handle CSV:

@verbatim
(get "/csv" #:mime 'csv
     (lambda (rc)
       (:mime rc '(("a" "1") ("b" "2")))))
@end verbatim

@node XML
@section XML
@anchor{#xml}
In Scheme, XML is handled with SXML. Another way would be to append the text to a common string.

@verbatim
(get "/xml" #:mime 'xml
     (lambda (rc)
       (:mime rc '(*TOP* (WEIGHT (@ (unit "pound"))
                                 (NET (@ (certified "certified")) "67")
                                 (GROSS "95"))))))
@end verbatim

The rendered result to the client will be:

@verbatim
<WEIGHT unit="pound">
  <NET certified="certified">67</NET>
  <GROSS>95</GROSS>
</WEIGHT>
@end verbatim

@node SXML
@section SXML
@anchor{#sxml}
You can use SXML to replace XML for exchanging data format. This way saves some bandwidth.

@verbatim
(get "/sxml" #:mime 'sxml
     (lambda (rc)
       (:mime rc '((a 1) (b 2)))))
@end verbatim

@node Upload files
@chapter Upload files
@anchor{#upload-files}
If you want to be able to upload files, store-uploaded-files is your friend.

@menu
* Receive an upload from the client::
* Send an upload to a Server::
@end menu

@node Receive an upload from the client
@section Receive an upload from the client
@anchor{#receive-an-upload-from-the-client}
The typical configuration of an uploading WebAPI looks like this:

@verbatim
(post "/upload" #:from-post '(store #:path "upload" #:sync #f)
      (lambda (rc)
        (case (:from-post rc 'store)
          ((success) (response-emit "upload succeeded!"))
          ((none) (response-emit "No uploaded files!"))
          (else (response-emit "Impossible! please report bug!")))))
@end verbatim

However, you may use the low-level API for more configurations as well:

@verbatim
(store-uploaded-files rc #:path (current-upload-path)
                      #:uid #f
                      #:gid #f
                      #:simple-ret? #t
                      #:mode #o664
                      #:path-mode #o775
                      #:sync #f)
@end verbatim

@strong{rc} is the route-context.

@code{#:path} is the specified path to put uploaded files.

@code{#:uid} is new UID for the uploaded files, #f uses the default UID.

@code{#:gid} specifies the GID.

@code{#:simple-ret?} specifies the mode of return:

@itemize
@item
if #t, there're only two possible return value, 'success for success, 'none for nothing has been done.
@item
if #f, and while it's successful, it returns a list to show more details: (success size-list filename-list).
@end itemize

@code{#:mode} chmod files to mode.

@code{#:path-mode} chmod upload path to mode.

@code{#:sync} sync while storing files.

@node Send an upload to a Server
@section Send an upload to a Server
@anchor{#send-an-upload-to-a-server}
Although GNU Artanis is often used in server-side, we provide this function for users to upload files from the client.

@verbatim
(upload-files-to uri pattern)
@end verbatim

@strong{uri} is standard HTTP URL:

@verbatim
scheme://[user:password@]domain:port/path?query_string#fragment_id
@end verbatim

@strong{pattern} should be: ((file filelist @dots{}) (data datalist @dots{})), for example:

@verbatim
(upload-files-to "ftp://nala:123@myupload.com/"
                 '((data ("data1" "hello world"))
                   (file ("file1" "filename") ("file2" "filename2"))))
@end verbatim

@node Sessions
@chapter Sessions
@anchor{#sessions}
You can use @code{#:session mode} to define a URL rule handler.

@verbatim
(post "/auth" #:session mode
      (lambda (rc) ...))
@end verbatim

@strong{mode} can be:

@itemize
@item
#t or 'spawn, to spawn a new session, the name of the SID is "sid" by default.
@item
`(spawn ,sid) to specify the name of the sid to spawn.
@item
`(spawn ,sid ,proc) to specify the name of the sid and a proc to @strong{define your own session spawner}.
@end itemize

And the APIs of the session is :session

@verbatim
(:session rc cmd)
@end verbatim

@strong{cmd} can be:

@itemize
@item
'check to check the session with name "sid".
@item
`(check ,sid) to check the session with a specified sid name.
@item
'check-and-spawn to check "sid" first, if it doesn't exist, then spawn new.
@item
`(check-and-spawn ,sid) to do the same as above, but with specifying the sid name.
@item
`(check-and-spawn-and-keep ,sid) to check, then spawn, then keep, specifying the sid name.
@item
'spawn to spawn a session with the name "sid".
@item
'spawn-and-keep to spawn a session then keep with the name "sid".
@item
'drop to remove the current session by "sid".
@item
`(drop ,sid) to remove the session by the specifed sid.
@end itemize

@menu
* Session backend::
@end menu

@node Session backend
@section Session backend
@anchor{#session-backend}
Artanis provide several backends for implementing sessions in the lower-level. Please take a look at the description of @code{session.backend} in your artanis.conf.

@node Cookies
@chapter Cookies
@anchor{#cookies}
You can use @code{#:cookies mode} to define a URL rule handler.

@verbatim
(get "/certain-rule" #:cookies mode
     (lambda (rc) ...))
@end verbatim

@strong{mode} can be:

@itemize
@item
('names names @dots{}) to create cookie objects with specified names.
@item
('custom (names @dots{}) maker setter getter modifier) to specify a more complicated customized cookie handler.
@end itemize

Cookie object is used to create/modify the cookie that will return to the client automatically. This is the instance of a cookie which can be controlled in server-side.

If you want to get the cookie from the client, you should use @code{:cookies-value}.

And the APIs:

@verbatim
(:cookies-set! rc cookie-name key val)

(:cookies-ref rc cookie-name key)

(:cookies-setattr! rc cookie-name #:expir #f #:domain #f
                   #:path #f #:secure #f #:http-only #f)

(:cookies-remove! rc key) ; remove cookie from client
(:cookies-value rc name) ; get cookie from the client
@end verbatim

For example:

@verbatim
(get "/cookie" #:cookies '(names cc)
     (lambda (rc)
       (:cookies-set! rc 'cc "sid" "123321")
       "ok"))

(get "/cookie/:expires" #:cookies '(names cc)
     (lambda (rc)
       ;; link the cookie object with actual cookie
       (:cookies-set! rc 'cc "sid" (:cookies-value rc "sid"))
       (:cookies-setattr! rc 'cc #:expir (string->number (params rc "expires")))
       "ok"))
@end verbatim

You should link the actual cookie "sid" to the created cookie 'cc, then you can control it. Otherwise it doesn't work.

You can use these commands in your console to see the results:

@verbatim
curl --head localhost:3000/cookie
# and
curl --head localhost:3000/cookie/120
@end verbatim

@node Authentication
@chapter Authentication
@anchor{#authentication}

@menu
* Init Authentication::
* Basic Authentication::
* Common Authentication::
* Login Authentication::
* Authenticate checking::
* Customized Authentication::
* Websocket Authentication::
* HMAC::
@end menu

@node Init Authentication
@section Init Authentication
@anchor{#init-authentication}
GNU Artanis provides flexible mechanism for authentication.

You can use @code{#:auth mode} to define a URL rule handler.

@verbatim
(get "/certain-rule" #:auth mode
     (lambda (rc) ...))
@end verbatim

@strong{mode} can be:

@itemize
@item
SQL as @emph{string template}. You can write your own custom SQL string to fetch & check your username and password.
@item
@code{('basic (lambda (rc user passwd) ...))} init Basic Authentication mode. @emph{user} is the username, and @emph{passwd} is the password.
@item
@code{('table table-name username-field passwd-field)} init a common Authentication mode. @strong{The passwd will be encrypted by the default algorithm}.
@item
@code{('table table-name username-field passwd-field crypto-proc)} similar to the above item, but encrypt passwd with crypto-proc.
@item
@code{(table-name crypto-proc)}, so the passwd field will be "passwd" and username will be "username" by default.You can encrypt the passwd with crypto-proc.
@end itemize

Available crypto-proc helper functions listed here:

@verbatim
(string->md5 <string>)
(string->sha-1 <string>)
(string->sha-224 <string>)
(string->sha-256 <string>)
(string->sha-384 <string>)
(string->sha-512 <string>)
@end verbatim

NOTE: Please make sure that the @code{username-field} and @code{passwd-field} must be the same with the field name specifed in the submit form of you web page code.

For example, if there is a form on you page:

@verbatim
<input type="password" name="passwd">
@end verbatim

Please notice that name of password input was specified to @code{"passwd"}.

Then you should write authentication like this:

@verbatim
(post "/auth" #:auth '(table user "user" "passwd") #:session #t
      (lambda (rc) ...))
@end verbatim

Please notice that the @code{"passwd"} here is the same with what you specified in the form.

@node Basic Authentication
@section Basic Authentication
@anchor{#basic-authentication}
The HTTP Basic authentication (BA) implementation is the simplest technique for enforcing access control to web resources, as it doesn't require cookies, session identifiers, or login pages. But rather uses static, standard HTTP headers, which means that no extra handshakes are necessary for the connection.

The BA mechanism provides no protection for the transmitted credentials. They are merely encoded with Base64, but not encrypted or hashed in any way. For that reason, Basic Authentication is typically used over HTTPS.

@emph{@strong{GNU Artanis doesn't support HTTPS at present. There are plans to support it in the future.}}

Let's see a simple example:

@verbatim
(define (my-checker rc user passwd)
  (and (string=? user "jack") (string=? passwd "123")))

(post "/bauth" #:auth `(basic ,my-checker)
      (lambda (rc)
        (if (:auth rc)
            "auth ok"
            (throw-auth-needed))))
@end verbatim

Another simple way to provide authentication is to compare the passsword stored in a database table:

@verbatim
(post "/bauth" #:auth `(basic Person username passwd)
      (lambda (rc) ... ))
@end verbatim

NOTE: Assuming @strong{username} and @strong{passwd} are columns of the Person table.

You have to define your own checker with the anonymous function @code{(lambda (rc u p) ...)}. #t to show success, and #f to fail.

APIs:

@itemize
@item
@code{(:auth rc)} will check if Basic Authentication succeeded, #f if not.
@item
@code{(throw-auth-needed)} is a useful helper function to ask for auth in client side.
@end itemize

@node Common Authentication
@section Common Authentication
@anchor{#common-authentication}
There are multiple authentication methods that can be used by developers. Most of them are sort of tricky hacks. Here are the most common.

The most common, and relatively safe way to authenticate, is to use the POST method, and check the username and passwd from a table in the DB.

There are several ways to provide authentication.

The simplest case is for @emph{String Template}:

@verbatim
#:auth "string-template"
@end verbatim

If you save the account data in a database table, then you can use the table mode:

@verbatim
#:auth `(table ,table-name [,username-field] [,passwd-field] [,salt-field] [,hmac])
@end verbatim

NOTE: The square-braced @strong{[args]} above are optional.

The default values for the optional items are:

@itemize
@item
username-field: username
@item
passwd-field: passwd
@item
salt-field: salt
@end itemize

TODO: remove the brackets for salt-field, as it says below that it's not optional. And specify whether "optional" means that can be skipped or set to #f to use the default value.

@strong{GNU Artanis requires a salted password, it's not optional.}

So please prepare a field in a table for the salt string. It's your responsibility to generate a salt string, please see @emph{Random String Generator}. When authenticating, please specify the salt field name in the salt-field argument.

For hmac item, please see @emph{HMAC}.

@node Login Authentication
@section Login Authentication
@anchor{#login-authentication}
Usually, when doing a login, you will need both @code{#:auth} and @code{#:session} options for a long time session. The first step is to authenticate, if it's successful, then spawn a new session for this request.

Here is a simple example:

@verbatim
(post "/auth"
 #:auth '(table user "user" "passwd")
 #:session #t
 (lambda (rc)
   (cond
    ((:session rc 'check) "auth ok (session)")
    ((:auth rc)
     (:session rc 'spawn)
     "auth ok")
    (else (redirect-to rc "/login?login_failed=true")))))
@end verbatim

@strong{NOTE: The passwd will be encrypted by the default algorithm.}

@node Authenticate checking
@section Authenticate checking
@anchor{#authenticate-checking}
If certain page requires authencation first, then how to check it properly?

In GNU Artanis, the @strong{session-id} is the only token to check if the client has already been authenticated. By default, the @strong{session-id} is named as @strong{SID} in cookies. You may use @code{#:with-auth} to do all the works automatically for you.

@verbatim
(get "/dashboard"
  #:with-auth <options>
  (lambda (rc)
    (view-render "dashboard" (the-environment))))
@end verbatim

For example, assuming you have a page @strong{/dashboard} requires login, then you may set @code{#:with-auth} with certain option. We will explain this option later. Each time user visit @strong{/dashboard} page, GNU Artanis will check if there's valid @strong{session-id} from client's cookies, if yes, then run the handler to generate the response; if no, then jump to the related auth failed handler, depends on the option you specified.

Here's available @strong{options}:

@itemize
@item
@code{#t} means the default failure activity: redirect to @strong{/login} page.
@item
URL in string, specify the login page URL. For example, @code{#:with-auth "/admin/login"}.
@item
@code{'status} will return a 401 page with status code 401, which could be checked by cilent.
@item
@strong{Thunk} can be used to generate customized response by users. A thunk is a function without parameter. For example, @code{#:with-auth (lambda () (scm->json-string '((status . 401) (reason . "No auth"))))}. This is useful to customize your protocol in JSON for RESTful API.
@end itemize

@strong{NOTE:} Different from other @emph{shortcuts}, there's no @code{:with-auth} @strong{apply} for user customized operations inside the handler. If you use @code{#:with-auth}, then all related works are handled by GNU Artanis.

@node Customized Authentication
@section Customized Authentication
@anchor{#customized-authentication}

@verbatim
;; (define (checker username passwd) ...)
#:auth `(post ,username ,passwd ,checker)
@end verbatim

This mode will parse and return @strong{username} and @strong{password} by the specified field name in query-string. What matters here is that you can write your own checker for customization.

@node Websocket Authentication
@section Websocket Authentication
@anchor{#websocket-authentication}
See @emph{Websocket authencation}

@node HMAC
@section HMAC
@anchor{#hmac}
@uref{https://en.wikipedia.org/wiki/HMAC,HMAC} is a hash-based message authentication code. It's dangerous to store the passwd in a raw string. A safer way is to salt then hash with a strong cryptograpic hash function when storing the passwd.

The default salt is a random string got from the operating system. And the default cryptographic hash function is SHA256. You can set your own HMAC function, as in this example:

@verbatim
(define (my-hmac passwd salt)
  (string->sha-512 (format #f "~a-~a-~a" passwd salt (current-time))))

(post "/auth" #:auth `(table user "user" "passwd" "salt" ,my-hmac)
      ...... )
@end verbatim

The default HMAC function is:

@verbatim
(define (default-hmac passwd salt)
  (string->sha-256 (string-append passwd salt)))
@end verbatim

For more on hash functions, please refer to @emph{Cryptographic hash functions}.

@node Cache
@chapter Cache
@anchor{#cache}

@menu
* On web caching::
* Cache APIs::
@end menu

@node On web caching
@section On web caching
@anchor{#on-web-caching}
Web caching is very important nowadays. This section discusses proper web caching. It is not a full product guide document, but it may help to understand how to cache data in GNU Artanis.

(to be continued@dots{})

@node Cache APIs
@section Cache APIs
@anchor{#cache-apis}
You can use @code{#:cache mode} to define a URL rule handler.

@verbatim
(get "/certain-rule" #:cache mode
     (lambda (rc) ...))
@end verbatim

@strong{@emph{NOTE}}: the default value of "maxage" (3600 seconds) is defined by @code{cache.maxage} in @code{/etc/artanis/artanis.conf}.

@strong{mode} can be:

@itemize
@item
@code{#t} to enable caching the page.
@item
@code{#f} to disable caching the page explicitly. It's the default value.
@item
@code{('static [maxage <- 3600])} to be used for static files. The URL rule must be a real path to a static file.
@item
@code{(filename [maxage <- 3600])} to cache a static file. This is useful when you don't want to reveal the actual path of the static file, but use a fake URL for it.
@item
@code{('public filename [maxage <- 3600])} to allow proxies cache the content of specified static file. If HTTP authentication is required, responses are automatically set to "private".
@item
@code{('private filename [maxage <- 3600])} to not allow proxies cache the content of specified static file.
@end itemize

Let's set a simple cache setting for dynamic content:

@verbatim
(get "/new" #:cache #t
     (lambda (rc)
       (:cache rc "hello world")))
@end verbatim

If you want to cache a static file, and permit proxies cache the content:

@verbatim
(get "/hide" #:cache '(public "/some.html")
     (lambda (rc)
       (:cache rc)))
@end verbatim

But, if your current URL rule is used for authentication (once you use @code{#:auth}), the cache will be changed to @strong{private} even if you specify @strong{public}.

@verbatim
(get "/pauth"
  #:auth `(basic ,(lambda (rc u p) (and (string=? u "nala")
                                        (string=? p "123"))))
  #:cache '(public "/some.html") ; will be changed to 'private' forcely.
  (lambda (rc) (:cache rc)))
@end verbatim

@node Shortcuts
@chapter Shortcuts
@anchor{#shortcuts}

@menu
* What is shortcuts?::
* Database connection::
* Raw SQL::
* String template::
* SQL-Mapping shortcut unfinished::
* Authentication shortcut::
@end menu

@node What is shortcuts?
@section What is shortcuts?
@anchor{#what-is-shortcuts}
@emph{shortcuts} are a series of special functions. They're used to simplify the complex operations, according to the configuration specified by the related keyword, set after a URL-rule.

It was named @emph{OHT} which stands for @emph{Optional Handler Table}, which indicates the basic principle to be implemented. But it was too hard to remember. So let's just call it @emph{shortcut}.

Anyway, you may find them in the module @uref{https://gitlab.com/NalaGinrut/artanis/blob/master/artanis/oht.scm,(artanis oht)}.

It's good practice to use @emph{shortcuts} as possible and avoid calling low-level APIs.

Each shortcut consists of 2 parts: @strong{config} and @strong{apply}.

@strong{config} is to configure a certain service for the specific URL rule. This configuration will only be availble to this URL rule, and independent to other registered URL rules.

@strong{apply} is used to call specific functions related to your configuration in the @strong{config} step. The first argument of the @strong{apply} method must be a @code{route-context} (@emph{route context}).

@node Database connection
@section Database connection
@anchor{#database-connection}
This is a useful feature to use when you connect to a database. The shortcut provides a way to interact with the raw connection. The connection is fetched from the connection pool, which is created at GNU Artanis's start up.

@verbatim
;; config
#:conn #t

;; apply
(:conn <route-context> [sql])
@end verbatim

@itemize
@item
The second argument is optional, if it's missing, then @code{:conn} will return the raw connection after applying @code{(:conn rc)}.
@itemize
@item
NOTE: If you haven't set @code{#:conn #t}, and applied @code{(:conn rc)}, then @code{(rc-conn rc)} will return @code{#f}. This is why you shouldn't use low-level @code{(rc-conn rc)}.
@end itemize

@item
If you pass a second argument, it should be a valid SQL query string. The returned value is described in @emph{DB connection pool}.
@itemize
@item
You can create the SQL query string with @emph{SSQL}.
@end itemize

@end itemize

@node Raw SQL
@section Raw SQL
@anchor{#raw-sql}
This shortcut is useful for a simple one-shot query.

@verbatim
;; config
#:raw-sql sql

;; apply
(:raw-sql <route-context> mode)
@end verbatim

@strong{Sql} must be a valid SQL query string.

@strong{Mode} is one of:

@itemize
@item
'all for getting all the results.
@item
'top for getting the first result.
@item
A positive integer to indicate how many results should be returned.
@end itemize

@node String template
@section String template
@anchor{#string-template}
This is a shortcut for @emph{string template}. Sometimes it's useful when you just need a quick way to use a string template. It doesn't support multi templates, so if you do need to, please use the traditional @emph{String Template}.

@verbatim
;; config
#:str "string template"

;; apply
(:str <route-context> key-values ...)
@end verbatim

Please checkout @emph{string template} to find out how to use the @emph{string-template} and @emph{key-values}.

@node SQL-Mapping shortcut unfinished
@section SQL-Mapping shortcut (unfinished)
@anchor{#sql-mapping-shortcut-unfinished}
This is related to @emph{SQL-Mapping}, which is still experimental, maybe you should wait for the next version if you wish to use it.

@verbatim
;; config
#:sql-mapping config-patterns

;; apply
(:sql-mapping <route-context> command ...)
@end verbatim

@strong{config-patterns} can be any of:

@itemize
@item
@code{#t} enable the simple sql-mapping.
@item
@code{`(path ,path ,name)} Fetch the sql-mapping with @emph{name} in the specified @emph{path}.
@itemize
@item
@emph{name} must be an object of the symbol type.
@item
@emph{path} must be an object of the string type, and an existing path in your filesystem.
@end itemize

@item
@code{`(add ,name ,sql-template)} Fetch the sql-mapping with @emph{name} rendered from @emph{sql-template}.
@itemize
@item
@emph{name} should be an object of the symbol type.
@item
@emph{sql-template} is described in more detail in @emph{SQL-Mapping}.
@end itemize

@end itemize

@node Authentication shortcut
@section Authentication shortcut
@anchor{#authentication-shortcut}
See @emph{Authentication}.

@node Websocket Experimental
@chapter Websocket (Experimental)
@anchor{#websocket-experimental}

@menu
* Websocket introduction::
* Websocket basic usage::
* Websocket named-pipe::
* Websocket APIs::
* Websocket frame::
* Websocket opcode::
* Websocket authentication::
@end menu

@node Websocket introduction
@section Websocket introduction
@anchor{#websocket-introduction}
Websockets are becoming more and more important for modern web development. GNU Artanis is trying to provide an industrial strength and efficient Websocket implementation. Websockets are important for GNU Artanis's design. Please see @emph{Principles} for more details.

@node Websocket basic usage
@section Websocket basic usage
@anchor{#websocket-basic-usage}
In GNU Artanis, a Websocket handling is triggered by setting it on a specific URL. You should use @code{#:websocket} to configure the Websocket. Here's the API definition:

@verbatim
#:websocket (proto protocol-name ['inexclusive])
@end verbatim

@strong{'inexclusive} is optional. By default, each Websocket connection only servs one client. However, sometimes we do need to multicast the messages to several clients. Enable @strong{'inexclusive} is the easiest way, but it may cause the similar issue to file-description-leaking. There're two suggestions:

@itemize
@item
Set and check security token to prevent malicious programs establish massive connections.
@item
If you want to do muticasting as in pub/sub, then use different token or named-pipe to subscribe. And maintain a subscribers table for multicasting.
@end itemize

Here's a simple example for common cases:

@verbatim
(use-modules (artanis artanis))

(get "/echo" #:websocket '(proto echo)
     (lambda (rc)
       (:websocket rc 'payload)))

(run #:port 3000)
@end verbatim

In this simple test, we choose the simplest @strong{echo} protocol of the Websocket. This will return back the string sent from the client. Let's also write a simple javascript function for the web frontend:

@verbatim
function WebSocketTest()
{
    if ("WebSocket" in window)
    {
        document.write("<p>WebSocket is supported by your Browser!</p>");

        // Let us open a web socket
        var ws = new WebSocket("ws://localhost:3000/echo");

        ws.onopen = function()
        {
            // Web Socket is connected, send data using send()
            ws.send("hello world");
            document.write("<p>Message is sent...</p>");
        };

        ws.onmessage = function (evt)
        {
            var received_msg = evt.data;
            document.write("<p>hello welcome...</p>");
        };

        ws.onclose = function()
        {
            // websocket is closed.
            document.write("<p>Connection is closed...</p>");
        };

        window.onbeforeunload = function(event) {
            socket.close();
        };
    }
    else
    {
        // Your browser doesn't support WebSockets
        document.write("<p>WebSocket NOT supported by your Browser!</p>");
    }
}
@end verbatim

@node Websocket named-pipe
@section Websocket named-pipe
@anchor{#websocket-named-pipe}
Artanis provides the named-pipe based on Websocket, which is very useful to interact between the client and server. @emph{NOTE: The Websocket named-pipe is very useful to implement server-push messaging.}

Here is the critical API:

@verbatim
(named-pipe-subscribe <route-context>)
@end verbatim

Let's try a simple example:

@verbatim
(get "/robot" #:websocket '(proto echo) named-pipe-subscribe)
@end verbatim

Here we register a WebAPI named @code{"/robot"}, and configure it a Websocket API by @code{#:websocket '(proto echo)}. As you may see, we specify its protocol to a simple echo protocal, it is to say, it will redirect what it receives. Each time it receives a message, it will redirect the message to the specified named-pipe, and the subscriber of that named-pipe will get the message instantly. The name of the pipe is specified by the client like this:

@verbatim
var ws = new WebSocket("ws://localhost:3000/robot?artanis_named_pipe=robot-1");
@end verbatim

The query-string @code{"artanis_named_pipe"} is a special key in Artanis, you MUST use this key to specify the pipe name. Here we specify the pipe name as @code{"robot-1"}.

Now we setup the named-pipe, however, we still need to setup another WebAPI to let the client send message:

@verbatim
(get "/welcome/:whom/:what" #:websocket 'send-only
     (lambda (rc)
       (:websocket rc 'send (params rc "whom") (params rc "what"))
       "ok"))
@end verbatim

The configure @code{"#:websocket 'send-only"} means this API is only for sending, so that it's half-duplex. And another critical Websocket API here:

@verbatim
(:websocket <route-context> 'send <pipe-name> <data>)
@end verbatim

Each time you send data from the WebAPI, for example:

@verbatim
curl localhost:3000/welcome/robot-1/nala
@end verbatim

In our simple example of @uref{https://gitlab.com/NalaGinrut/artanis/blob/master/examples/websocket-named-pipe.html,Websocket named-pipe test}, you should run The named-pipe will receive the message @code{"nala"}, and the page on the browser will print @code{"hello welcome...nala"} instantly.

@node Websocket APIs
@section Websocket APIs
@anchor{#websocket-apis}
@strong{NOTE: The Websocket is in a very preliminary stage. It only support echo.}

@menu
* Websocket configuration::
* Websocket application::
@end menu

@node Websocket configuration
@subsection Websocket configuration
@anchor{#websocket-configuration}

@verbatim
#:websocket `(proto ,protocol_name)
@end verbatim

@strong{protocol_name} can be:

@itemize
@item
@strong{'echo} for a simple echo test. @strong{NOTE:} More protocols will be added in the future.

@verbatim
#:websocket simple_pattern
@end verbatim

@strong{simple_pattern} can be:

@itemize
@item
@strong{#t} or @strong{'raw} will enable a WebSocket on this URL without specifying the protocol. So you will get the raw data from the decoded payload.

@verbatim
#:websocket `(redirect ,ip/usk)
@end verbatim

@end itemize

This is used for redirecting a Websocket stream to another address. ip/usk is an ip or a unix-socket. The string regexp pattern has to match:

@itemize
@item
@code{^ip://(?:[0-9]@{1,3}\\.)@{3@}[0-9]@{1,3@}(:[0-9]@{1,5@})?$@}

@item
@code{^unix://[a-zA-Z-_0-9]+\\.socket$}

@verbatim
#:websocket `(proxy ,protocol)
@end verbatim

@end itemize

Setup a proxy with a specific protocol handler. Unlike the regular proxy approach, the proxy in Artanis doesn't need a listening port. Since it's always 80/443 or a custom port. The client has to have websocket support. And be able to access the specified URL to establish a websocket connection. The rest is the same as with a regular proxy.

@end itemize

@node Websocket application
@subsection Websocket application
@anchor{#websocket-application}

@verbatim
(:websocket <route-context> command)
@end verbatim

@strong{command} can be:

@itemize
@item
@strong{'payload} to get the decoded data from the client. It's decoded from Websocket frame automatically. So you don't have to parse the frame.
@end itemize

@node Websocket frame
@section Websocket frame
@anchor{#websocket-frame}
GNU Artanis provides a Websocket data frame struct, as defined in @uref{https://tools.ietf.org/html/rfc6455,RFC6455}.

The frame will not be decoded or parsed into a record-type, but will be kept as the binary frame read from the client, and use bitwise operations to fetch fields. This kind of `lazy' design saves much time as it doesn't parse unused fields, and makes it easier to redirect without any serialization. If users want to get a certain field, Artanis provides APIs for fetching them. Users can decide how to parse the frames by themselves, which we think is more efficient.

Here are the APIs you can use:

@verbatim
(websocket-frame? <websocket-frame>)
@end verbatim

@verbatim
;; parser: bytevector -> custom data frame
(websocket-frame-parser <websocket-frame>)
@end verbatim

@strong{websocket-frame-parser} is the registered reader for the protocol specified by the @code{#:websocket} configuration. The protocol is customizable based on protobuf. @emph{NOTE: Custom protocols support hasn't been implemented yet.}

@verbatim
(websocket-frame-head <websocket-frame>)
(websocket-frame-final-fragment? <websocket-frame>)
(websocket-frame-opcode <websocket-frame>)
(websocket-frame-payload <websocket-frame>)
(websocket-frame-mask <websocket-frame>)
@end verbatim

To get the Websocket frame information. See @uref{https://tools.ietf.org/html/rfc6455#page-27,Data framing} for details.

@itemize
@item
@strong{head} returns the first 2 bytes in the data frame.
@item
@strong{final-fragment} returns true if it's the last frame in a session.
@item
@strong{opcode} returns the opcode in the frame. Refer to @emph{Websocket opcode}.
@item
@strong{payload} returns the actual encoded data.
@item
@strong{mask} returns the frame mask.
@end itemize

@node Websocket opcode
@section Websocket opcode
@anchor{#websocket-opcode}
Opcode defines the interpretation of "Payload data". If an unknown opcode is received, the receiving endpoint's WebSocket connection will fail.

@verbatim
;;  check if it's a continuation frame
(is-continue-frame? opcode)

;;  check if it's text frame
(is-text-frame? opcode)

;; check if it's binary frame
(is-binary-frame? opcode)

;; check if it's control frame
(is-control-frame? opcode)
(is-non-control-frame? opcode)

;;  websocket requires closing
(is-close-frame? opcode)

;;  check if it's a ping frame
(is-ping-frame? opcode)

;;  check if it's a pong frame
(is-pong-frame? opcode)

;;  %xB-F are reserved for further control frames
(is-reserved-frame? opcode)
@end verbatim

@node Websocket authentication
@section Websocket authentication
@anchor{#websocket-authentication-1}
The Websocket authentication hasn't been unsupported yet.

@node Ragnarok server core
@chapter Ragnarok server core
@anchor{#ragnarok-server-core}

@menu
* Ragnarok introduction::
* Principles::
* Features::
* Ragnarok APIs::
@end menu

@node Ragnarok introduction
@section Ragnarok introduction
@anchor{#ragnarok-introduction}
Since version 0.2, GNU Artanis has started to use a strong server core for high concurrency. Its name is Ragnarok. In the philosophy of the design of GNU Artanis, everything is meant to be flexible and customizable. The server core is no exception. In case Ragnarok doesn't suit your needs, you're free to use something else.

Ragnarok doesn't use any popular library for handling events (libev/libuv etc @dots{}). It's a brand new server core based on epoll and @uref{https://en.wikipedia.org/wiki/Delimited_continuation,delimited continuations}.

@node Principles
@section Principles
@anchor{#principles}
A basic characteristic of Ragnarok is the use of co-routines. These co-routines are implemented with @uref{https://en.wikipedia.org/wiki/Delimited_continuation,delimited continuations}. There are no OS/kernel controlled threads, like pthread, for scheduling @emph{request-handlers} in Ragnarok. All the tasks are scheduled by a userland scheduler. And the task is nothing but just a special continuation. The key difference between this and a regular @uref{https://en.wikipedia.org/wiki/Call-with-current-continuation#Criticism,full-stack continuation}, is that you can set limits with precision, instead of having to capture the whole stack.

For researchers, there is a paper published on @uref{http://www.schemeworkshop.org/2016/,ICFP Scheme Workshop 2016 conference} to explain the principle and the design of GNU Artanis:

@uref{https://github.com/NalaGinrut/artanis/raw/gh-pages/research/scheme16/art2016.pdf,Multi-purpose web framework design based on websockets over HTTP Gateway}.

(to be continued @dots{})

@node Features
@section Features
@anchor{#features}
In Artanis, the request handling can be scheduled even when the socket buffer is full (depends on @code{server.bufsize}). And let other handlers deal with the requests. Just like the scheduling of an OS, but in userland.

If you have issues with the buffer when scheduling, there's no way to flush before it breaks, since we can't tell if the scheduling is caused by the buffering or the blocking.

@menu
* Multi-server support::
* Multi-workers support::
@end menu

@node Multi-server support
@subsection Multi-server support
@anchor{#multi-server-support}

@verbatim
server.multi = true
@end verbatim

Ragnarok takes advantage of @code{SO_REUSEPORT} introduced in GNU/Linux 3.9 to provide a feature named @code{server.multi} which can be enabled in the config. This feature allows users to start several Artanis instances listening to the same port, to take advantage of multi core CPUs, and the Linux Kernel managing the events.

@node Multi-workers support
@subsection Multi-workers support
@anchor{#multi-workers-support}

@verbatim
server.workers = positive_number
@end verbatim

The multi.server can let you start multiple GNU Artanis instances to handle the requests concurrently. This feature is essential for high-performance web applications. @emph{However, one may have to manage each instance by manually, it's a bit tricky since it's designed for distributed situation.}

@itemize
@item
If you set @code{server.workers} to a number larger than 1, then @code{server.multi} will be enabled automatically.
@end itemize

NOTE: Since v1.1.0.

@node Ragnarok APIs
@section Ragnarok APIs
@anchor{#ragnarok-apis}
You can use these APIs to customize your own server core.

(to be continued @dots{})

@node Key-Value Database
@chapter Key-Value Database
@anchor{#key-value-database}

@menu
* LPC::
@end menu

@node LPC
@section LPC
@anchor{#lpc}
LPC stands for Lightweight Persistent Cache. It's the easiest way to use key-value DB in Artanis.

For example:

@verbatim
(get "/certain_rule" #:lpc <backend> (lambda (rc) ...))
@end verbatim

The backend includes:

@itemize
@item
@code{#t} or @code{'redis}
@item
@code{'json}
@end itemize

After configured, it's easy to use it:

@verbatim
;; Setter
(:lpc rc 'set <key> <val>)

;; Getter
(:lpc rc 'get <key>)
;; or
(:lpc rc 'ref <key>)
@end verbatim

In default, the key will be prefixed automatically:

@verbatim
(string-append "__artanis_lpc_" (get-conf '(db name)) "_" key)
@end verbatim

@node I18n
@chapter I18n
@anchor{#i18n}
i18n stands for internationalization, which is a process of designing a software application so that it can be adapted to various languages and regions without engineering changes.

@menu
* Basic API to get language set from client::
* Translation mode::
* I18n best practice::
@end menu

@node Basic API to get language set from client
@section Basic API to get language set from client
@anchor{#basic-api-to-get-language-set-from-client}
There're three basic APIs for i18n in GNU Artanis:

@menu
* I18n from URL::
* I18n from cookie::
* I18n from HTTP header::
@end menu

@node I18n from URL
@subsection I18n from URL
@anchor{#i18n-from-url}

@verbatim
(get "/index/:lang"
  #:i18n "lang"
  (lamba (rc) ...))
@end verbatim

@itemize
@item
If the key in the URL is @code{":my_lang"}, then you should configure it as @code{"#:i18n my_lang"}.
@end itemize

@node I18n from cookie
@subsection I18n from cookie
@anchor{#i18n-from-cookie}

@verbatim
(get "/index"
  #:i18n '(cookie "lang")
  (lambda (rc) ...))
@end verbatim

@itemize
@item
If the key in the cookie is @code{"my_lang"}, then you should configure it as @code{#:i18n '(cookie "my_lang")}.
@end itemize

@node I18n from HTTP header
@subsection I18n from HTTP header
@anchor{#i18n-from-http-header}

@verbatim
(get "/index"
  #:i18n 'header
  (lambda (rc) ...))
@end verbatim

@itemize
@item
The language set in the HTTP header is @code{"Accept-Language"}.
@item
If there's multiple languages, GNU Artanis will choose the first one.
@item
Note that the language is in the format of RFC 4646 (e.g. ja-JP) GNU Artanis will convert it to the format of GNU Gettext (e.g. ja_JP).
@end itemize

@node Translation mode
@section Translation mode
@anchor{#translation-mode}

@menu
* With GNU Gettext::
* With JSON::
* With SXML::
@end menu

@node With GNU Gettext
@subsection With GNU Gettext
@anchor{#with-gnu-gettext}

@verbatim
session.i18n = locale
@end verbatim

You must put related translation into @strong{@strong{sys/i18n/locale}} directory. The directory structure should be like this: src_scheme[:exports code](lang_COUNTRY/LC_MESSAGES/domain.mo).

About PO and MO files, please read @uref{https://www.gnu.org/software/gettext/manual/html_node/index.html#SEC_Contents,GNU Gettext's manual}.

@node With JSON
@subsection With JSON
@anchor{#with-json}

@verbatim
session.i18n = json
@end verbatim

You must put related translation into @strong{@strong{sys/i18n/json}} directory. The directory structure should be like this: src_scheme[:exports code](lang_COUNTRY.json).

@node With SXML
@subsection With SXML
@anchor{#with-sxml}

@verbatim
session.i18n = sxml
@end verbatim

You must put related translation into @strong{@strong{sys/i18n/sxml}} directory. The directory structure should be like this: src_scheme[:exports code](lang_COUNTRY.scm).

@node I18n best practice
@section I18n best practice
@anchor{#i18n-best-practice}
Please read GNU Artanis blog post about @uref{https://artanis.dev/blog/how-to-make-i18n-properly.html,how to make i18n properly}.

@node Utils
@chapter Utils
@anchor{#utils}
@strong{The functions listed below require the @uref{https://gitlab.com/NalaGinrut/artanis/blob/master/artanis/utils.scm,(artanis utils)} module.}

@menu
* String Template::
* Random String Generator::
* Cryptographic hash functions::
* Stack & Queue::
* Useful string operation::
* Time operation tool::
@end menu

@node String Template
@section String Template
@anchor{#string-template-1}
GNU Artanis provides Python3-like template strings:

@verbatim
(make-string-template tpl . vals)
@end verbatim

@itemize
@item
@strong{tpl} stands for template string.
@item
@strong{vals} is varg-list specifying default value to certain key.
@end itemize

For example:

@verbatim
(define st (make-string-template "hello ${name}"))
(st #:name "nala")
;; ==> "hello nala"

;; or you may specify a default value for ${name}
(define st (make-string-template "hello ${name}" #:name "unknown"))
(st)
;; ==> "hello unknown"
(st #:name "john")
;; ==> "hello john"
@end verbatim

@node Random String Generator
@section Random String Generator
@anchor{#random-string-generator}
Get random string from @code{/dev/urandom}.

@verbatim
(get-random-from-dev #:length 8 #:uppercase #f)
@end verbatim

@node Cryptographic hash functions
@section Cryptographic hash functions
@anchor{#cryptographic-hash-functions}

@verbatim
;; hash a string with MD5
(string->md5 str)
;; hash a string with SHA-1
(string->sha-1 str)
@end verbatim

SHA-2 hash functions are also supported from Artanis-0.2.5.

@verbatim
(string->sha-224 str)
(string->sha-384 str)
(string->sha-512 str)
@end verbatim

@node Stack & Queue
@section Stack & Queue
@anchor{#stack-queue}
GNU Artanis provides simple interfaces for stack & queue:

@verbatim
;; stack operations
(new-stack)
(stack-pop! stk)
(stack-push! stk elem)
(stack-top stk)
(stack-remove! stk key)
(stack-empty? stk)

;; queue operations
(new-queue)
(queue-out! q)
(queue-in! q elem)
(queue-head q)
(queue-tail q)
(queue-remove! q key)
(queue-empty? q)
@end verbatim

@node Useful string operation
@section Useful string operation
@anchor{#useful-string-operation}
If you want to get all the contents from a file into a string, then don't use @code{get-string-all} imported from rnrs. Because it will not detect the correct charset from the locale, and this may cause the length differ from the actual value. Although GNU Artanis can handle this length issue properly, you should use @code{get-string-all-with-detected-charset} when you need to do something like this. If you don't care about the contents encoding but just want to get the them, it's better to use @code{get-bytevector-all} imported from rnrs.

@verbatim
(get-string-all-with-detected-charset filename)
@end verbatim

@node Time operation tool
@section Time operation tool
@anchor{#time-operation-tool}
TODO

@node Debug mode
@chapter Debug mode
@anchor{#debug-mode}
GNU Artanis provides a debug-mode for a more convenient way to debug. It's very easy to use.

For the simplest way, pass @code{#:debug #t} when calling @code{run} function:

@verbatim
(run #:debug #t)
@end verbatim

If you are using the MVC system, or created a project directory, just pass --debug or -g to art:

@verbatim
# In the project directory
art work --debug
# Or
art work -g
@end verbatim

When you enable debug-mode, the Model and Controller modules in the directory will be reloaded automatically every time they're called.

When @strong{@emph{not}} in debug mode, you have to press Ctrl+C to quit GNU Artanis server and start it again to test changed modules. Debug mode saves time when testing.

You can add paths to monitor certain files (for example, a JSON file as config file to be reloaded on the fly). If you want to be notified when they're changed. Just put the paths here:

@verbatim
debug.monitor = my/lib/json, my/lib/modules
@end verbatim

@node Appendix A GNU Free Documentation License
@chapter Appendix A GNU Free Documentation License
@anchor{#appendix-a-gnu-free-documentation-license}
Version 1.3, 3 November 2008 Copyright © 2000, 2001, 2002, 2007, 2008 Free Software Foundation, Inc. @url{http://fsf.org/}

Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. PREAMBLE The purpose of this License is to make a manual, textbook, or other functional and useful document free in the sense of freedom: to assure everyone the effective freedom to copy and redistribute it, with or without modifying it, either commercially or noncommercially. Secondarily, this License preserves for the author and publisher a way to get credit for their work, while not being considered responsible for modifications made by others.

This License is a kind of “copyleft”, which means that derivative works of the document must themselves be free in the same sense. It complements the GNU General Public License, which is a copyleft license designed for free software.

We have designed this License in order to use it for manuals for free software, because free software needs free documentation: a free program should come with manuals providing the same freedoms that the software does. But this License is not limited to software manuals; it can be used for any textual work, regardless of subject matter or whether it is published as a printed book. We recommend this License principally for works whose purpose is instruction or reference.

APPLICABILITY AND DEFINITIONS This License applies to any manual or other work, in any medium, that contains a notice placed by the copyright holder saying it can be distributed under the terms of this License. Such a notice grants a world-wide, royalty-free license, unlimited in duration, to use that work under the conditions stated herein. The “Document”, below, refers to any such manual or work. Any member of the public is a licensee, and is addressed as “you”. You accept the license if you copy, modify or distribute the work in a way requiring permission under copyright law.

A “Modified Version” of the Document means any work containing the Document or a portion of it, either copied verbatim, or with modifications and/or translated into another language.

A “Secondary Section” is a named appendix or a front-matter section of the Document that deals exclusively with the relationship of the publishers or authors of the Document to the Document's overall subject (or to related matters) and contains nothing that could fall directly within that overall subject. (Thus, if the Document is in part a textbook of mathematics, a Secondary Section may not explain any mathematics.) The relationship could be a matter of historical connection with the subject or with related matters, or of legal, commercial, philosophical, ethical or political position regarding them.

The “Invariant Sections” are certain Secondary Sections whose titles are designated, as being those of Invariant Sections, in the notice that says that the Document is released under this License. If a section does not fit the above definition of Secondary then it is not allowed to be designated as Invariant. The Document may contain zero Invariant Sections. If the Document does not identify any Invariant Sections then there are none.

The “Cover Texts” are certain short passages of text that are listed, as Front-Cover Texts or Back-Cover Texts, in the notice that says that the Document is released under this License. A Front-Cover Text may be at most 5 words, and a Back-Cover Text may be at most 25 words.

A “Transparent” copy of the Document means a machine-readable copy, represented in a format whose specification is available to the general public, that is suitable for revising the document straightforwardly with generic text editors or (for images composed of pixels) generic paint programs or (for drawings) some widely available drawing editor, and that is suitable for input to text formatters or for automatic translation to a variety of formats suitable for input to text formatters. A copy made in an otherwise Transparent file format whose markup, or absence of markup, has been arranged to thwart or discourage subsequent modification by readers is not Transparent. An image format is not Transparent if used for any substantial amount of text. A copy that is not “Transparent” is called “Opaque”.

Examples of suitable formats for Transparent copies include plain ASCII without markup, Texinfo input format, LaTeX input format, SGML or XML using a publicly available DTD, and standard-conforming simple HTML, PostScript or PDF designed for human modification. Examples of transparent image formats include PNG, XCF and JPG. Opaque formats include proprietary formats that can be read and edited only by proprietary word processors, SGML or XML for which the DTD and/or processing tools are not generally available, and the machine-generated HTML, PostScript or PDF produced by some word processors for output purposes only.

The “Title Page” means, for a printed book, the title page itself, plus such following pages as are needed to hold, legibly, the material this License requires to appear in the title page. For works in formats which do not have any title page as such, “Title Page” means the text near the most prominent appearance of the work's title, preceding the beginning of the body of the text.

The “publisher” means any person or entity that distributes copies of the Document to the public.

A section “Entitled XYZ” means a named subunit of the Document whose title either is precisely XYZ or contains XYZ in parentheses following text that translates XYZ in another language. (Here XYZ stands for a specific section name mentioned below, such as “Acknowledgements”, “Dedications”, “Endorsements”, or “History”.) To “Preserve the Title” of such a section when you modify the Document means that it remains a section “Entitled XYZ” according to this definition.

The Document may include Warranty Disclaimers next to the notice which states that this License applies to the Document. These Warranty Disclaimers are considered to be included by reference in this License, but only as regards disclaiming warranties: any other implication that these Warranty Disclaimers may have is void and has no effect on the meaning of this License.

VERBATIM COPYING You may copy and distribute the Document in any medium, either commercially or noncommercially, provided that this License, the copyright notices, and the license notice saying this License applies to the Document are reproduced in all copies, and that you add no other conditions whatsoever to those of this License. You may not use technical measures to obstruct or control the reading or further copying of the copies you make or distribute. However, you may accept compensation in exchange for copies. If you distribute a large enough number of copies you must also follow the conditions in section 3.

You may also lend copies, under the same conditions stated above, and you may publicly display copies.

COPYING IN QUANTITY If you publish printed copies (or copies in media that commonly have printed covers) of the Document, numbering more than 100, and the Document's license notice requires Cover Texts, you must enclose the copies in covers that carry, clearly and legibly, all these Cover Texts: Front-Cover Texts on the front cover, and Back-Cover Texts on the back cover. Both covers must also clearly and legibly identify you as the publisher of these copies. The front cover must present the full title with all words of the title equally prominent and visible. You may add other material on the covers in addition. Copying with changes limited to the covers, as long as they preserve the title of the Document and satisfy these conditions, can be treated as verbatim copying in other respects.

If the required texts for either cover are too voluminous to fit legibly, you should put the first ones listed (as many as fit reasonably) on the actual cover, and continue the rest onto adjacent pages.

If you publish or distribute Opaque copies of the Document numbering more than 100, you must either include a machine-readable Transparent copy along with each Opaque copy, or state in or with each Opaque copy a computer-network location from which the general network-using public has access to download using public-standard network protocols a complete Transparent copy of the Document, free of added material. If you use the latter option, you must take reasonably prudent steps, when you begin distribution of Opaque copies in quantity, to ensure that this Transparent copy will remain thus accessible at the stated location until at least one year after the last time you distribute an Opaque copy (directly or through your agents or retailers) of that edition to the public.

It is requested, but not required, that you contact the authors of the Document well before redistributing any large number of copies, to give them a chance to provide you with an updated version of the Document.

MODIFICATIONS You may copy and distribute a Modified Version of the Document under the conditions of sections 2 and 3 above, provided that you release the Modified Version under precisely this License, with the Modified Version filling the role of the Document, thus licensing distribution and modification of the Modified Version to whoever possesses a copy of it. In addition, you must do these things in the Modified Version:

Use in the Title Page (and on the covers, if any) a title distinct from that of the Document, and from those of previous versions (which should, if there were any, be listed in the History section of the Document). You may use the same title as a previous version if the original publisher of that version gives permission. List on the Title Page, as authors, one or more persons or entities responsible for authorship of the modifications in the Modified Version, together with at least five of the principal authors of the Document (all of its principal authors, if it has fewer than five), unless they release you from this requirement. State on the Title page the name of the publisher of the Modified Version, as the publisher. Preserve all the copyright notices of the Document. Add an appropriate copyright notice for your modifications adjacent to the other copyright notices. Include, immediately after the copyright notices, a license notice giving the public permission to use the Modified Version under the terms of this License, in the form shown in the Addendum below. Preserve in that license notice the full lists of Invariant Sections and required Cover Texts given in the Document's license notice. Include an unaltered copy of this License. Preserve the section Entitled “History”, Preserve its Title, and add to it an item stating at least the title, year, new authors, and publisher of the Modified Version as given on the Title Page. If there is no section Entitled “History” in the Document, create one stating the title, year, authors, and publisher of the Document as given on its Title Page, then add an item describing the Modified Version as stated in the previous sentence. Preserve the network location, if any, given in the Document for public access to a Transparent copy of the Document, and likewise the network locations given in the Document for previous versions it was based on. These may be placed in the “History” section. You may omit a network location for a work that was published at least four years before the Document itself, or if the original publisher of the version it refers to gives permission. For any section Entitled “Acknowledgements” or “Dedications”, Preserve the Title of the section, and preserve in the section all the substance and tone of each of the contributor acknowledgements and/or dedications given therein. Preserve all the Invariant Sections of the Document, unaltered in their text and in their titles. Section numbers or the equivalent are not considered part of the section titles. Delete any section Entitled “Endorsements”. Such a section may not be included in the Modified Version. Do not retitle any existing section to be Entitled “Endorsements” or to conflict in title with any Invariant Section. Preserve any Warranty Disclaimers. If the Modified Version includes new front-matter sections or appendices that qualify as Secondary Sections and contain no material copied from the Document, you may at your option designate some or all of these sections as invariant. To do this, add their titles to the list of Invariant Sections in the Modified Version's license notice. These titles must be distinct from any other section titles.

You may add a section Entitled “Endorsements”, provided it contains nothing but endorsements of your Modified Version by various parties---for example, statements of peer review or that the text has been approved by an organization as the authoritative definition of a standard.

You may add a passage of up to five words as a Front-Cover Text, and a passage of up to 25 words as a Back-Cover Text, to the end of the list of Cover Texts in the Modified Version. Only one passage of Front-Cover Text and one of Back-Cover Text may be added by (or through arrangements made by) any one entity. If the Document already includes a cover text for the same cover, previously added by you or by arrangement made by the same entity you are acting on behalf of, you may not add another; but you may replace the old one, on explicit permission from the previous publisher that added the old one.

The author(s) and publisher(s) of the Document do not by this License give permission to use their names for publicity for or to assert or imply endorsement of any Modified Version.

COMBINING DOCUMENTS You may combine the Document with other documents released under this License, under the terms defined in section 4 above for modified versions, provided that you include in the combination all of the Invariant Sections of all of the original documents, unmodified, and list them all as Invariant Sections of your combined work in its license notice, and that you preserve all their Warranty Disclaimers.

The combined work need only contain one copy of this License, and multiple identical Invariant Sections may be replaced with a single copy. If there are multiple Invariant Sections with the same name but different contents, make the title of each such section unique by adding at the end of it, in parentheses, the name of the original author or publisher of that section if known, or else a unique number. Make the same adjustment to the section titles in the list of Invariant Sections in the license notice of the combined work.

In the combination, you must combine any sections Entitled “History” in the various original documents, forming one section Entitled “History”; likewise combine any sections Entitled “Acknowledgements”, and any sections Entitled “Dedications”. You must delete all sections Entitled “Endorsements.”

COLLECTIONS OF DOCUMENTS You may make a collection consisting of the Document and other documents released under this License, and replace the individual copies of this License in the various documents with a single copy that is included in the collection, provided that you follow the rules of this License for verbatim copying of each of the documents in all other respects.

You may extract a single document from such a collection, and distribute it individually under this License, provided you insert a copy of this License into the extracted document, and follow this License in all other respects regarding verbatim copying of that document.

AGGREGATION WITH INDEPENDENT WORKS A compilation of the Document or its derivatives with other separate and independent documents or works, in or on a volume of a storage or distribution medium, is called an “aggregate” if the copyright resulting from the compilation is not used to limit the legal rights of the compilation's users beyond what the individual works permit. When the Document is included in an aggregate, this License does not apply to the other works in the aggregate which are not themselves derivative works of the Document.

If the Cover Text requirement of section 3 is applicable to these copies of the Document, then if the Document is less than one half of the entire aggregate, the Document's Cover Texts may be placed on covers that bracket the Document within the aggregate, or the electronic equivalent of covers if the Document is in electronic form. Otherwise they must appear on printed covers that bracket the whole aggregate.

TRANSLATION Translation is considered a kind of modification, so you may distribute translations of the Document under the terms of section 4. Replacing Invariant Sections with translations requires special permission from their copyright holders, but you may include translations of some or all Invariant Sections in addition to the original versions of these Invariant Sections. You may include a translation of this License, and all the license notices in the Document, and any Warranty Disclaimers, provided that you also include the original English version of this License and the original versions of those notices and disclaimers. In case of a disagreement between the translation and the original version of this License or a notice or disclaimer, the original version will prevail.

If a section in the Document is Entitled “Acknowledgements”, “Dedications”, or “History”, the requirement (section 4) to Preserve its Title (section 1) will typically require changing the actual title.

TERMINATION You may not copy, modify, sublicense, or distribute the Document except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense, or distribute it is void, and will automatically terminate your rights under this License.

However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation.

Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice.

Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, receipt of a copy of some or all of the same material does not give you any rights to use it.

FUTURE REVISIONS OF THIS LICENSE The Free Software Foundation may publish new, revised versions of the GNU Free Documentation License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. See @url{http://www.gnu.org/copyleft/}.

Each version of the License is given a distinguishing version number. If the Document specifies that a particular numbered version of this License “or any later version” applies to it, you have the option of following the terms and conditions either of that specified version or of any later version that has been published (not as a draft) by the Free Software Foundation. If the Document does not specify a version number of this License, you may choose any version ever published (not as a draft) by the Free Software Foundation. If the Document specifies that a proxy can decide which future versions of this License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Document.

RELICENSING “Massive Multiauthor Collaboration Site” (or “MMC Site”) means any World Wide Web server that publishes copyrightable works and also provides prominent facilities for anybody to edit those works. A public wiki that anybody can edit is an example of such a server. A “Massive Multiauthor Collaboration” (or “MMC”) contained in the site means any set of copyrightable works thus published on the MMC site.

“CC-BY-SA” means the Creative Commons Attribution-Share Alike 3.0 license published by Creative Commons Corporation, a not-for-profit corporation with a principal place of business in San Francisco, California, as well as future copyleft versions of that license published by that same organization.

“Incorporate” means to publish or republish a Document, in whole or in part, as part of another Document.

An MMC is “eligible for relicensing” if it is licensed under this License, and if all works that were first published under this License somewhere other than this MMC, and subsequently incorporated in whole or in part into the MMC, (1) had no cover texts or invariant sections, and (2) were thus incorporated prior to November 1, 2008.

The operator of an MMC Site may republish an MMC contained in the site under CC-BY-SA on the same site at any time before August 1, 2009, provided the MMC is eligible for relicensing.

ADDENDUM: How to use this License for your documents

To use this License in a document you have written, include a copy of the License in the document and put the following copyright and license notices just after the title page:

Copyright (C) year your name. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled ``GNU Free Documentation License''. If you have Invariant Sections, Front-Cover Texts and Back-Cover Texts, replace the “with@dots{}Texts.” line with this:

with the Invariant Sections being list their titles, with the Front-Cover Texts being list, and with the Back-Cover Texts being list. If you have Invariant Sections without Cover Texts, or some other combination of the three, merge those two alternatives to suit the situation.

If your document contains nontrivial examples of program code, we recommend releasing these examples in parallel under your choice of free software license, such as the GNU General Public License, to permit their use in free software.
@bye
