Syro

5. Composing applications

5. Composer des applications

The pillars for scalability are simplicity and modularity. With Syro you can achieve both, and in this chapter we will explore how applications can be mounted on top of other applications.

Les piliers de la scalabilité sont la simplicité et la modularité. Avec Syro vous pouvez accomplir les deux, et dans ce chapitre nous allons explorer comment des applications peuvent être montées par dessus d'autres.

In the following example, we will use a different application if the visitor tries to access "/admin":

Dans l'exemple suivant, nous allons utiliser une application différente si le visiteur essaie d'acceder à "/admin":

  require "syro"

  Admin = Syro.new do
    get do
      res.text "Got /admin"
    end
  end

  App = Syro.new do
    on "admin" do
      run(Admin)
    end
  end

  env = {
    "REQUEST_METHOD" => "GET",
    "PATH_INFO"      => "/admin",
    "SCRIPT_NAME"    => "",
  }

  p App.call(env)

When we use the run method, the SCRIPT_NAME environment variable needs to be present in our env hash.

Lorsque nous utilisons la méthode run, la variable d'environnement SCRIPT_NAME doit être présente dans le hash env.

Output:

Résultat :

  [200,
   {"Content-Length"=>"10",
    "Content-Type"=>"text/plain"},
   ["Got /admin"]]

Sharing information between apps

Partager des informations entre apps

When mounting an application, you can provide a hash that will become the inbox of the mounted app:

Quand vous montez une application, vous pouvez fournir un hash qui deviendra l'inbox de l'app montée :

  require "syro"

  Admin = Syro.new do
    get do
      res.text "Got #{inbox[:path]}"
    end
  end

  App = Syro.new do
    on "admin" do
      run(Admin, path: path.prev)
    end
  end

  env = {
    "REQUEST_METHOD" => "GET",
    "PATH_INFO"      => "/admin",
    "SCRIPT_NAME" => "",
  }

  p App.call(env)

Output:

Résultat :

  [200,
   {"Content-Length"=>"10",
    "Content-Type"=>"text/plain"},
   ["Got /admin"]]

Exercise

Exercice

Can you mount the same app at two different places? Can you mount an app on a mounted app? Remember to modify PATH_INFO to access different matchers.

Pouvez-vous monter la même app à deux endroits différents ? Pouvez-vous monter une app sur une app montée ? N'oubliez pas de modifier PATH_INFO pour acceder à différents matchers.


Prev | Index | Next

Précédent | Sommaire | Suivant