Genie

The web server technology I am exploring in Julia is called Genie. Let's start with some code to run in the REPL:


            using Genie, Genie.Router
            route("/hello") do
                "Hello world"
            end
        

Still in the REPL we start the server as follows:

up()

(To close the server we will type 'down()').

There is no conflict with Apache because Genie starts the server here: http://127.0.0.1:8000 and apache2 starts it here: http://127.0.0.1:80

We start up a browser and go to http://127.0.0.1:8000. I have saved this under the browser folder 'me' as the bookmark 'Genie'. At present we will get the 404 page not found message. This is because we have only defined the one route which is /hello, so if we add this to the url then we get the text "Hello world" displayed on the page.

As an experiment I changed the code to output some html. The file cannot be shown on this page because it contains html tags. Look at the source code for this page to see the code used. It is in a comment just below this text.

This code works because of the additional using statements that can render html.

I continued my study of the tutorial at this site: https://www.freecodecamp.org/news/how-to-build-web-apps-in-julia/ The next step begins by creating a web app. Step one is choosing where to put the website-like folders that will be created. These are very like the ones I have in my own website. We have compleate freedom where to put them so I have chosen /home/steve/JuliaWebSite. To create these files we will use this command: Genie.Generator.newapp_webservice("JuliaWebsite") Here is the correct sequence of steps:

  1. In the REPL...
  2. type a ";" to enter shell mode
  3. Type 'cd /home/steve'. The folders will be created below here.
  4. backspace to exit the shell mode, returning to the REPL
  5. Type the following two commands:
  6. using Genie
  7. Genie.Generator.newapp_webservice("JuliaWebsite")

A whole set of useful folders and files are created.

The tutorial was slightly wrong about the last command because the instructions didn't include the '.Generator.' part. I found this by searching for Julia packages. However, having put it right, I now have an embryonic website at Home/JuliaWebsite. This means that if I now visit http://127.0.0.1/ I get a welcome page provided by the Genie package.

I have modified the file 'routes.jl' so that the url http://127.0.0.1/genie gets this page!

Restarting the server

This took a bit of searching around. To restart the server from scratch do this (starting from the home directory):

  1. cd JuliaWebsite
  2. ./bin/repl

When the Julia environmnt is ready, type up(). The web server will start up and is now be available in the browser.

The code is being edited using VS, opening the 'JuliaWebsite' folder.

At present I have a form which sends two items of data to the server. The server opens a fresh page and reflects back the data. The NotesController module was created by using this command: Genie.Generator.newcontroller("Notes") This creates an empty module and registers it as part of the application.

The sequence in modules is to name the module first and then have using statements. Genie.Requests was needed for the postpayload function, which is the one which extracts data from the POST that was sent from the browser.