wtorek, 28 marca 2017

lazarus + Brook framework: reactivation :-)

It was long time, when I decided to create website application using Lazarus and Brook framework. In the mean time a lot of happen in my life, so this idea wasnt realized.
Now, 3 years later, we have a new versions of Lazarus IDE and Brook framework. And maybe will be enough time to do it again :-)

I founded very nice book: "CMS design using PHP and JQuery" by Kae Verens so I decided to try make similar thing using Lazarus and Brook to learn how CMS works and how Brook works. It will be big challenge, because Im not professional developer, but hobbyist :-)

It's the big difference between developing PHP application and Pascal application. In first case, the application is run through php module of Apache, and it's interpreted by server before pages are showed by client request.
Pascal application is compiled, so Im not sure if all techniques useful for PHP could be transformed to use in Pascal web application. We will see.

Configuration of local development environment 

I have installed Linux Mint 18 on my laptop, Apache 2.4 and Mysql rdbms.
Before we start developing our application some preparation steps are necessary.

Step 1. Apache configuration 

We have few ways for using our application: we can make is as standalone application with embedded web server, we can build it as an Apache module or use CGI or FastCGI standard to serve our web application to the world.
Maybe I will write about all this cases later, but for this blog, I decided to make it as FastCGI application. So, first I need to  install suitable modules for Apache.
They are two modules for fastCGI applications:
  •  libapache2-mod-fastcgi
  • libapache2-mod-fcgid 
 I choose fcgid apache module, discussions about both you can find in the Internet. Yet another module will be usefull: mod-rewrite. In my system it is provided with apache2 installation. Next step will be enabling this modules. In the console window we write:

a2enmodule rewrite
a2enmodule fcgid

after that restarting of apache2 service will be necessary. For example:

apachectl restart

And last step remain: configure virtual site to test my application. I created config file mylazcms.conf in the /etc/apache2/sites-avaiable directory with this content:

<VirtualHost mylazcms.localhost>
   ServerName mylazcms.localhost
    DocumentRoot  /var/www/mylazcms/public

    <IfModule mod_fcgid.c>

    <Directory /var/www/mylazcms/appbf/>
           AddHandler fcgid-script .fcgi
           Options +ExecCGI -Indexes
           AllowOverride all
           Order allow,deny
           Allow from all

   ScriptAlias /myapp/ "/var/www/mylazcms/appbf/"
   ErrorLog ${APACHE_LOG_DIR}/mylazcms.error.log
   LogLevel warn rewrite:trace3 fcgid:info
   CustomLog ${APACHE_LOG_DIR}/mylazcms.access.log combined

ServerSignature Off


You should look for documentation of virtual host in Apache, below few explanations.
Because I have some other www application on my laptop, I named this one as mylazcms.localhost. This need to make some changes in the host file, located in my system in the /etc directory. In this file I added line:

for server name <-> IP number translation.

In the /var/www/ - location of all www sites for Apache server in Linux Mint OS -  (this will be specific for your system configuration, you should check documentation) I created directory mylazcms and subdirectories: public and appbf. So directory structure  looks like this:

  - /var/www
         - /mylazcms 

In the appbf directory we will put our application, this location should be available only for Apache, not for users for security reasons.
Directory public will be available for everyone.

This lines can serve our application with Apache:

AddHandler fcgid-script .fcgi
Options +ExecCGI -Indexes

First line define all applications with extension *.fcgi  to be handled through fcgid module, the second defines directory /var/www/mycms/appbf as directory of executable files.


ScriptAlias /myapp/ "/var/www/mylazcms/appbf/"

hides real path to our application under alias /myapp so we can call it under address like this : www.ourserver.com/myapp

Next lines define separated log file and access log file for our application in apache2 log directory.

LogLevel warn rewrite:trace3 fcgid:info

help us catch more information from fcgid module and rewrite module. It's for debug purpose only .

Step 2: Lazarus and Brook installation and configuration

On the page http://www.lazarus-ide.org/index.php?page=downloads you can find links for download all necessary packages specific for your operation system.
You have to install three packages: lazarus, fpc, fpc-src.
When Lazaus IDE start first time, you can choose location of FPC (FreePascal compiler)  and FP packages source directories. IDE needs to know where they are. It should be founded by Lazarus, if everything gone well.

Now we have to download and install Brook framework. When Im writing this, is available 3.0 stable release of Brook. You can find it here: https://silvioprog.github.io/brookframework/. You can download packed sources or use git for this. After downloading and unpacking it's time to run Lazarus IDE. In the main menu choose: Package->Open package file (.lpk) and find brookex.lpk in /your-directory-with brook/packages directory.
Open and install in the IDE. Lazarus will be recompile yourself and when appear again, you should find new items in the main menu File->New... In the tree should be visible Brook framework items.

Now it's time for:

First fastcgi application

This is first step on the way to create web application with Lazarus/Brook :-)
So, in the main menu of IDE, choose  File->New... and from Brook framework branch choose Full CGI/FastCGI application. Small wizard will open, that help you to make base configuration of your application.

I chose FastCGI, UTF-8 options. next name of our application: testbrook for example, and path for the public directory for our application: /var/www/mylazcms/public - the same as in the virtual host configuration as DocumentRoot. 

In the next window we can configure actions of our application for requests.
I added default get action only for test. Next and OK create default test project.
Now build application, in main menu: Run->Build. Save project as testbrook.lpi.
This steps make testbrook application in the directory of testbrook.lpi file.
Change name of this file, save it as: testbrook.fcgi.
Copy this file to the /appbf subdirectory of your webapp, in my case: /var/www/mylazcms/appbf.
Restart the apache service as before, and open your browser.
In the address bar type: mylazcms.localhost/myapp/testbrook. This should show you the default page:

This is old name of my testing project on this screenshot, I named it mybrtest before I started write this :-)

So, it is all folks! Its working! :-)

wtorek, 11 lutego 2014

Hello everybody


Few years ago I developed many applications using Borland Delphi (now Embarcadero). I very liked this tool, had a lot of useful components and libraries and excellent community support. Provided very easy way to visually develop application, Object Pascal as a developing language take care about a lot of things, that C++ don't :-)
When Borland announced Kylix - Delphi like IDE for Pascal and C++ for Linux I was very excited: it was a very nice way for developing applications for Windows and Linux in the same way. Unfortunately Borland take a deal with Microsoft for .Net support in Delphi and Kylix project was abandoned.

Later I founded Lazarus Project  It use Freepascal compiler and working similar to the Delphi, but Lazarus follow your own path, trying to maintain compatibility on the language layer with Delphi ObjectPascal.
As we can read on Lazarus website:
Lazarus is a Delphi compatible cross-platform IDE for Free Pascal. It includes LCL which is more or less compatible with Delphi's VCL. Free Pascal is a GPL'ed compiler that runs on Linux, Win32, OS/2, 68K and more. Free Pascal is designed to be able to understand and compile Delphi syntax, which is OOP. Lazarus is the part of the missing puzzle that will allow you to develop Delphi like programs in all of the above platforms. Unlike Java which strives to be a write once run anywhere, Lazarus and Free Pascal strives for write once compile anywhere. Since the exact same compiler is available on all of the above platforms it means you don't need to do any recoding to produce identical products for different platforms.
I try Lazarus for few months and find the same simplicity in developing as Delphi had. Of course many things are different, debugging isn't so nice like in Delphi and numbers of components much smaller, but I think, it will be growing and will be better when more people start using and develop this excellent IDE.

Few months ago we can read on Lazarus forum
Brook 1.0 - A new framework was born
It's developed by Silvio Clecio (Brook website) As we can read:
Brook framework is the perfect Free Pascal framework for your web applications. It's pure Pascal. You don't need to leave your preferred programming language.
It's complete: simple actions or configurable actions for database access, advanced routing features, wizard for Lazarus, support for internationalization, high compatibility with JSON structures, easy and elegant REST implementation, plugins for varied needs, extensible and decoupled brokers... In fact, you have several reasons to adopt Brook as your web development framework.
As other web frameworks with Brook we can use databases and ORM (Greyhound project), template engines, CAPTCHA, mail, RSS and other toold provided as the plugins for Brook framework.
This works as a CGI or FastCGI web application, we can build application using this framework with embedded web server too.
I didn't use FastCGI or CGI before to build web applications, I developed them using PHP. But now I decided to try build web application using Lazarus and Brook framework. Im not professional developer, it's rather hobby but allow me to make some money from time to time :-)

So first, I installed Linux Mint 16 distribution to test it. In distribution repositories are older versions of Lazarus and Freepascal, so I downloaded them directly form the Lazarus and  Freepascal site. Next I downloaded and installed Brook framework. Installation package installs nice wizard for the Brook-based applications. Of course we need web server (I choose Apache2) and database server (my choice is MySQL). If we want to use embedded webserver we don't need Apache, but I decided build application in similar way as I did with PHP and to compare building application process and using application :-)
So now we are almost ready to begin developing, but I will write about it next time :-)