Skip to content

Tutorial

LiuTao edited this page Jan 25, 2017 · 14 revisions

Overview

This tutorial aims to develop a smallest web application in Eloquent-MVC.

Create a project

The function ELOQUENT.MVC.PROJECT:MAKE-PROJECT can be used to create a project's directory. If the target project's name is "foobar" and its parent directory is "/tmp/", the following code can create the project for you:

(eloquent.mvc.project:make-project #P"/tmp/foobar")

Define an action

In Eloquent-MVC, a function called by dispatcher when handling a request is an action. The most simple action is a function receives a HTTP request, and sends a string response.

In the foobar project, write the following code in file foobar.lisp and press C-c C-e to compile it:

(defun foobar (request)
  (declare (ignorable request))
  (eloquent.mvc.response:respond
   "Hello, world!"))

This function can be tested as followed:

(foobar nil)

The return value is an instance of class ELOQUENT.MVC.RESPONSE:<RESPONSE>, and it would be send back to client in proper form.

Define a routing rule

In Eloquent-MVC, a routing rule would be looked up when a HTTP request came, and if the action of the first rule matched a request would be invoked, to generate a response for the client. All the routing rules are defined in file router.lisp under directory config/. Here's an example:

((:get "/foobar" foobar::foobar))

The rule aboved explained as belowed: 1 . :get is the HTTP method GET; 1 . "/foobar" is the pattern a request must be matched; 1 . foobar::foobar is the action

Therefor, an HTTP GET request with path /foobar would be handled by action foobar::foobar.

Configuration

The Eloquent-MVC reads configuration from file config/eloquent-mvc.yaml. This file is in YAML format. Here's an example:

application:
  root: /tmp/foobar/
log:
  directory: /tmp/foobar/log/
server:
  port: 8087
  server: hunchentoot
static-file:
  prefix: /assets/

Start the server

Enter the following code to start the web server

(eloquent.mvc.loader:load #P"/tmp/foobar/")

The server hunchentoot started and listened on port 8087, waiting for HTTP requests. Before requesting, run the following command to create the log directory:

mkdir /tmp/foobar/log/

Now the server can handle HTTP request, try the following code:

curl 'http://localhost:8087/foobar'
Clone this wiki locally