The best practice to port Scheme application to Artanis

In this tutorial, we will show you the best practice to port Scheme application to Artanis. It is assumed that you have installed Artanis and have your own Scheme application. For the installation, please refer to the Artanis Manual.

Step-1 Create a new Artanis web application

artanis new my-app

The important directories of Artanis web application

Here's a typical directory structure of an Artanis web application:

.
├── app
│   ├── controllers
│   ├── models
│   ├── protocols
│   └── views
├── conf
│   ├── artanis.conf
│   └── README
├── db
│   ├── migration
│   └── sm
├── ENTRY
├── lib
├── log
├── prv
├── pub
│   ├── css
│   ├── img
│   │   └── upload
│   └── js
├── README
├── sys
│   ├── i18n
│   └── pages
├── test
│   ├── benchmark
│   │   └── README
│   ├── functional
│   └── unit
└── tmp
    └── cache
        ├── migration
        ├── README
        └── route.cache

For your porting work, here are the directories you need to pay attention to:

  • app/controllers
  • pub/{css,img,js}
  • lib

Most of the cases, that's enough.

Step-2 Put your existing code into lib/ directory

It is recommended to put your existing code into the lib/ directory. You can create a new directory in lib/ to organize your code. For example, we create a new directory called lib/my-app to put your own code.

It's also recommended to use the define-library form to define your module. which is R7RS compatible. Here's an example:

;; lib/my-app/hello.scm
(define-library (my-app hello)
 (import (guile))
 (export hello)
 (begin

   (define (hello)
     '((name . nala) (age . 15)))

   ))

In this example, we define a function hello which returns a list of name and age. We define this association list because we can easily convert it to JSON format to response to the client.

Step-3 Create a controller to handle the request

Now, we create a controller to handle the request. Here's an example:

art draw controller hello

Then, we modify the controller to call the function in lib/my-app/hello.scm.

;; app/controllers/hello.scm

;; Controller hello definition of tt
;; Please add your license header here.
;; This file is generated automatically by GNU Artanis.
(define-artanis-controller hello) ; DO NOT REMOVE THIS LINE!!!

(import (my-app hello))

(get "/hello"
 #:mime 'json
 (lambda (rc)
   (:mime rc (hello))))

The modules in lib/my-app will be detected automatically by Artanis. You just need to import it and use it.

Step-4 Test your application

Now, you can test your application by running the following command:

art work

Then, you can visit http://localhost:8080/hello to see the result.

curl http://localhost:8080/hello
# {"name":"nala","age":15}

Author: Artanis Dev Team

Created: 2024-12-04 Wed 03:11

Validate