Recent

Author Topic: Example of a web application with lazarus  (Read 7486 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Example of a web application with lazarus
« on: October 24, 2018, 09:51:43 am »
Hello guys, if you try to download from this link my project you will see that I have created an example (with comments in Italian) in which I demonstrate how to create a web application bootstrap (with MVC pattern). It is based on Indy to implement the web server that takes the template from html file. It is not perfect, because in unit unit_controller.pas the GetHTML function uses synapse to recover the html as it seems that synapse has a bug (arbitrarily does not always read me the content correctly and this makes me skip the validity of the json content) .

www.lazaruspascal.it/esempi/ngit_bootstrap_application.zip

Let me know your impressions
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Re: Example of a web application with lazarus
« Reply #1 on: October 24, 2018, 10:44:55 am »
I forgot to log in. The credentials are

user: root
pwd: 1234
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Thaddy

  • Hero Member
  • *****
  • Posts: 18764
  • To Europe: simply sell USA bonds: dollar collapses
Re: Example of a web application with lazarus
« Reply #2 on: October 24, 2018, 11:16:05 am »
In unit_gethtml I see some issues:
- in the uses clause ssl_openssl is missing, which renders it a bit useless, since everybody uses https
- the uses clause otherwise contains a lot of futile unused units, which slows down compile times.
- Why do you write
Code: Pascal  [Select][+][-]
  1.         begin
  2.           page.LoadFromStream(http.Document);
  3.           for i:=0 to page.Count-1 do
  4.               risultato:=risultato + (page[i]);
  5.         end;
Instead of:
Code: Pascal  [Select][+][-]
  1.         begin
  2.           page.LoadFromStream(http.Document);
  3.           risultato:=Page.Text; // <-------------
  4.         end;
That is less error prone.

In general it would be best to replace that function with synapse' standard function:
I would write it like this:
Code: Pascal  [Select][+][-]
  1. unit unit_gethtml;
  2. {$mode objfpc}
  3. interface
  4. uses httpsend, ssl_openssl;
  5.  
  6.  function RecuperaHtml(const Url: string): string;
  7.  
  8. implementation
  9. uses classes;
  10.  
  11. function RecuperaHtml(const Url: string): string;
  12. var
  13.   L:TStrings;
  14. begin
  15.   Result :='';
  16.   try
  17.     L := TStringlist.Create;
  18.     if HttpGetText(Url,L) then Result := L.Text;
  19.   finally
  20.     L.Free;
  21.   end;
  22. end;
  23.  
  24. end.
Advantages:
- less error prone
- allows redirects!
- supports https



« Last Edit: October 24, 2018, 11:40:24 am by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

 

TinyPortal © 2005-2018