Open a file called app.rb
and type the following
code:
Ouvrez un fichier appelé app.rb
et entrez le code
suivant :
require "syro" App = Syro.new do get do res.text "hello, world" end end
You can run the code with Ruby:
Vouz pouvez exécuter le code avec Ruby :
ruby ./app.rb
But as expected, you won't see any output. A Syro app can be called by passing a hash with CGI variables, so let's give it a try. Append the following code to the file:
Mais comme prévu, vous n'allez voir aucun résultat. Une app Syro peut être appelée en lui passant le hash avec les variables CGI, essayons donc ça. Ajoutez le code suivant au fichier :
env = { "REQUEST_METHOD" => "GET", "PATH_INFO" => "/", } p App.call(env)
If you run the code again, you will get this output:
Si vous exécutez le code encore une fois, vous allez obtenir le résultat suivant :
[200, {"Content-Length"=>"12", "Content-Type"=>"text/plain"}, ["hello, world"]]
If you are familiar with Rack, you will recognize the output. If not, reading about Rack will come handy later on.
Si vous êtes familiarisé avec Rack, vous allez reconnaître le résultat. Si ce n'est pas le cas, une lecture sur Rack pourra vous être utile plus tard.
You can see that the body of the response contained the string
"hello, world"
. That's because the block that was
passed to get
was executed. When get
is called, it checks two things: first, it makes sure the
REQUEST_METHOD
is "GET"
; then it checks
"PATH_INFO"
to see if we are at the end of the path.
The terminal "/"
is considered the end of the path,
and as REQUEST_METHOD
was set to "GET"
the block passed to get
was evaluated. Finally, the
response was generated and returned.
Vous pouvez constater que le corps de la réponse contenait la
chaîne "hello, world"
. C'est parce que le bloc qui
a été transmis à get
a été executé. Lorsque
get
est appelé, il vérifie deux choses : d'abord,
il s'assure que REQUEST_METHOD
est bien "GET"
;
après il vérifie "PATH_INFO"
pour voir si l'on est
bien à la fin du chemin. Le terminal "/"
est consideré
la fin du chemin, et comme REQUEST_METHOD
a été
définie sur "GET"
le bloc passé à get
a été evalué. Enfin, la réponse a été générée et retournée.
We will see later how to run this code with a webserver, but for now it's better to get familiar with the underlying mechanics.
Nous verrons plus tard comment exécuter ce code avec un serveur web, mais pour le moment c'est mieux de se familiariser avec les mécaniques sous-jacentes.
Change the value of REQUEST_METHOD
and run the program
again. Does it work as expected? Change the value of
PATH_INFO
, run it again. What happens?
Changez la valeur de REQUEST_METHOD
et exécutez le
programme à nouveau. Est-ce que ça marche comme attendu ? Changez
la valeur de PATH_INFO
, exécutez-le à nouveau. Que
s'est il passé ?