Recent

Author Topic: [Tutorial] Basic fpWeb Tutorial Article  (Read 40304 times)

bee

  • Sr. Member
  • ****
  • Posts: 393
Re: [Tutorial] Basic fpWeb Tutorial Article
« Reply #15 on: February 02, 2016, 05:31:26 am »
If you know the basic mechanisms of how a web app works, actually you don't need fpWeb to write a web app using Pascal. I've been making many web apps using Pascal since about 10 years ago, long before fpWeb existed. HTTP is a pretty old technology, you know. :)

IMHO, it's not about the future of Pascal for web because it has been able to do that since the beginning. I remember my first Pascal web app was written using Turbo Pascal 7.0. Basically it's just about how to read from stdin and to write to stdout using HTTP communication protocol. It's long before PHP even existed.

It's more about how we use Pascal to compete with currently over-hype web tools/frameworks/languages. Object Pascal is an advance language since its inception. It's just born too early so most people don't understand its capability.
-Bee-

A long time pascal lover.

edwinyzh

  • New Member
  • *
  • Posts: 43
Re: [Tutorial] Basic fpWeb Tutorial Article
« Reply #16 on: March 16, 2016, 08:09:53 am »
Well done and Thanks! Downloaded :)

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: [Tutorial] Basic fpWeb Tutorial Article
« Reply #17 on: March 24, 2016, 10:22:17 am »
If you know the basic mechanisms of how a web app works, actually you don't need fpWeb to write a web app using Pascal. I've been making many web apps using Pascal since about 10 years ago, long before fpWeb existed. HTTP is a pretty old technology, you know. :)

IMHO, it's not about the future of Pascal for web because it has been able to do that since the beginning. I remember my first Pascal web app was written using Turbo Pascal 7.0. Basically it's just about how to read from stdin and to write to stdout using HTTP communication protocol. It's long before PHP even existed.

It's more about how we use Pascal to compete with currently over-hype web tools/frameworks/languages. Object Pascal is an advance language since its inception. It's just born too early so most people don't understand its capability.

Well said & every encouraging. Thanks to Leledumbo for his excellent work. I never understood how fpWeb worked beforehand but I'll now give it a try.

JD
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: [Tutorial] Basic fpWeb Tutorial Article
« Reply #18 on: March 24, 2016, 07:02:24 pm »
Well said & every encouraging. Thanks to Leledumbo for his excellent work. I never understood how fpWeb worked beforehand but I'll now give it a try.
Don't worry, I can guarantee 1 out of 10 web developers don't actually understand how browsers and servers communicate, HTTP protocol or even basic networking works ;)
So if you give it a try, at least you're already one step ahead.

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: [Tutorial] Basic fpWeb Tutorial Article
« Reply #19 on: March 24, 2016, 07:43:28 pm »
@Leledumbo, it seems you are active in web development and I saw some your activities about this like your posts in your blog (I like to read more but you didnt update it lately) so I want to ask is it good doing web stuff with fpc? what libraries do you used? did you tried Brook,didi you liked it?
What fields in web you think fpc is more usefull?
Beside this post what sources should I read for this subject?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: [Tutorial] Basic fpWeb Tutorial Article
« Reply #20 on: March 25, 2016, 09:33:21 am »
so I want to ask is it good doing web stuff with fpc?
You should've figured it out yourself. Why the hell do I keep posting it if it's not good? ;)
what libraries do you used? did you tried Brook,didi you liked it?
Brook, exactly. fpWeb is secondary choice.
What fields in web you think fpc is more usefull?
Everything, no exception.
Beside this post what sources should I read for this subject?
General knowledge about networking, HTTP protocol, specific web server configuration, AJAX, JSON, etc. Too many to list if you have no clear goal.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: [Tutorial] Basic fpWeb Tutorial Article
« Reply #21 on: March 25, 2016, 10:09:23 am »
I actually ported FPC to FreeBSD in 1999-2000 because I wanted to run cgis on my FreeBSD account. It was my first major job as FPC devel.

The CGIs were a kind of webcounters btw.

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: [Tutorial] Basic fpWeb Tutorial Article
« Reply #22 on: March 25, 2016, 12:10:12 pm »
Thanks @Leledumbo.
Mostly I like to make some API for my programs to work with so avoid writing PHP for my API parts. I never saw a easy to learn demo for making an API system with authentication.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: [Tutorial] Basic fpWeb Tutorial Article
« Reply #23 on: March 25, 2016, 04:31:25 pm »
Thanks @Leledumbo.
Mostly I like to make some API for my programs to work with so avoid writing PHP for my API parts. I never saw a easy to learn demo for making an API system with authentication.
Because it's no different from normal HTTP request/response flow, just specially dictated (what you have to send, what you will receive) in some parsable text (json, xml, whatever). It's a matter of knowledge combining, although in PHP world it's often overrated by being something special and thus require special libraries as well.

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: [Tutorial] Basic fpWeb Tutorial Article
« Reply #24 on: March 25, 2016, 04:39:55 pm »
Quote
Because it's no different from normal HTTP request/response flow, just specially dictated (what you have to send, what you will receive) in some parsable text (json, xml, whatever). It's a matter of knowledge combining, although in PHP world it's often overrated by being something special and thus require special libraries as well.
But it good to have one example for beginners anyway.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: [Tutorial] Basic fpWeb Tutorial Article
« Reply #25 on: March 25, 2016, 05:37:28 pm »
But it good to have one example for beginners anyway.
I don't consider web service API with authentication as a beginner task, which is why I don't write one for it. I always write basic tutorial and only advanced ones if they're not merely a knowledge combining, for instance: compiler construction. I assume knowledge combining to be basic requirement for intermediate programmers, in fact that's what makes them different from beginners.

Below is a simple echo API with basic access authentication using Brook:
Code: Pascal  [Select][+][-]
  1. program SimpleAPIWithBasicAuthentication;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$define debug}
  5.  
  6. uses
  7.   base64,
  8.   fpjson,
  9.   httpprotocol,
  10.   BrookUtils,
  11.   BrookLogger,
  12.   BrookAction,
  13.   BrookHttpDefs,
  14.   BrookFCLHTTPAppBroker,
  15.   BrookApplication;
  16.  
  17. const
  18.   ValidUsername = 'root';
  19.   ValidPassword = 'admin';
  20.  
  21. type
  22.  
  23.   { TAPIAction }
  24.  
  25.   TAPIAction = class(TBrookAction)
  26.   protected
  27.     FJSONResponse: TJSONObject;
  28.   public
  29.     procedure DoRequest(ARequest: TBrookRequest; AResponse: TBrookResponse); override;
  30.   end;
  31.  
  32.   { TAuthenticatedAction }
  33.  
  34.   TAuthenticatedAction = class(TAPIAction)
  35.   private
  36.     function IsValidToken(const AToken: String): Boolean;
  37.   public
  38.     procedure Request(ARequest: TBrookRequest; AResponse: TBrookResponse); override;
  39.   end;
  40.  
  41.   { TEchoAction }
  42.  
  43.   TEchoAction = class(TAuthenticatedAction)
  44.     procedure Get; override;
  45.   end;
  46.  
  47. { TAPIAction }
  48.  
  49. procedure TAPIAction.DoRequest(ARequest: TBrookRequest;
  50.   AResponse: TBrookResponse);
  51. var
  52.   Log: String;
  53. begin
  54.   FJSONResponse := TJSONObject.Create;
  55.   try
  56.     inherited DoRequest(ARequest, AResponse);
  57.     // Every output is considered a log
  58.     Log := AResponse.Content;
  59.     if (Length(Log) > 0) and BrookSettings.LogActive then begin
  60.       TBrookLogger.Service.Info(Log);
  61.     end;
  62.     // Override content by JSON
  63.     AResponse.Content := FJSONResponse.{$ifndef debug}AsJSON{$else}FormatJSON{$endif};
  64.   finally
  65.     FJSONResponse.Free;
  66.   end;
  67. end;
  68.  
  69. { TAuthenticatedAction }
  70.  
  71. function TAuthenticatedAction.IsValidToken(const AToken: String): Boolean;
  72. begin
  73.   Result := EncodeStringBase64(ValidUsername + ':' + ValidPassword) = AToken;
  74. end;
  75.  
  76. procedure TAuthenticatedAction.Request(ARequest: TBrookRequest; AResponse: TBrookResponse);
  77. var
  78.   AuthHeader, TokenPrefix, Token: String;
  79.   Authenticated: Boolean;
  80. begin
  81.   AuthHeader := ARequest.GetHeader(hhAuthorization);
  82.   // I love System.Copy's "no error" behavior :)
  83.   TokenPrefix := Copy(AuthHeader,1,6);
  84.   Token := Copy(AuthHeader,7,Length(AuthHeader));
  85.   Authenticated := (TokenPrefix = 'Basic ') and IsValidToken(Token);
  86.   if Authenticated then
  87.     inherited Request(ARequest, AResponse)
  88.   else begin
  89.     AResponse.Code := 401;
  90.     AResponse.CodeText := 'Unauthorized';
  91.     AResponse.SetHeader(hhWWWAuthenticate,'Basic realm="API"');
  92.     FJSONResponse.Add('status',AResponse.Code);
  93.     FJSONResponse.Add('message',AResponse.CodeText);
  94.   end;
  95. end;
  96.  
  97. { TEchoAction }
  98.  
  99. procedure TEchoAction.Get;
  100. var
  101.   Msg: String;
  102. begin
  103.   Msg := HttpRequest.QueryFields.Values['message'];
  104.   if Length(Msg) > 0 then begin
  105.     FJSONResponse.Add('status',200);
  106.     FJSONResponse.Add('message',Msg);
  107.   end else begin
  108.     FJSONResponse.Add('status',400);
  109.     FJSONResponse.Add('message','Please provide "message" field');
  110.   end;
  111. end;
  112.  
  113. begin
  114.   TEchoAction.Register('/echo');
  115.   BrookSettings.Port := 4444;
  116.   BrookApp.Run;
  117. end.
  118.  
A beginner is likely to ask:
  • How to compile
  • How to run
  • How to test
  • How to have username & password combo stored in files/database/wherever
And many other non-essential things that may clutter a clean tutorial focusing on the web service itself. It's a self experience from teaching, and experiences don't lie ;)
« Last Edit: March 25, 2016, 10:59:14 pm by Leledumbo »

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: [Tutorial] Basic fpWeb Tutorial Article
« Reply #26 on: March 25, 2016, 09:02:43 pm »
Thanks @Leledumbo. It is more clear for me know how to make a simple API.

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: [Tutorial] Basic fpWeb Tutorial Article
« Reply #27 on: December 16, 2016, 04:51:26 pm »
Thanks Leledumbo!
It's a good tutorial indeed!!

But I've got a doubt:
When building a module, is there a way to specify the for which Apache version (e.g. Apache 2.2 Apache 2.4) generate the module?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: [Tutorial] Basic fpWeb Tutorial Article
« Reply #28 on: December 16, 2016, 06:36:08 pm »
Thanks Leledumbo!
It's a good tutorial indeed!!

But I've got a doubt:
When building a module, is there a way to specify the for which Apache version (e.g. Apache 2.2 Apache 2.4) generate the module?
The unit names differ for both versions, look in your units folder.

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: [Tutorial] Basic fpWeb Tutorial Article
« Reply #29 on: December 19, 2016, 02:33:34 pm »
Thank you!!
The unit names differ for both versions, look in your units folder.
I've changed the units "httpd" to "httpd24" and "fpapache" to "fpapache24" now the apache server starts well. But when the request reaches the server it doesn't responds. Checking the apache "error_log" file I read that the error that occurred is

Quote
    An unhandled exception occurred at $00007FF8F0760C66:
    EHTTP: No REQUEST_METHOD passed from server.
      $00007FF8F0760C66
Googling the message "No REQUEST_METHOD passed from server" points to FPC repositories only. For that reason I added temporary logs everywhere to pin point if there is something wrong with my code (which works fine on Apache 2.2) and the only logs I have are the ones in the "begin ... end." of the ".lpr" file. It makes me think that the exception is raised before my code has a chance to read the request.

Is there any other thing I need to change? Maybe the ".fpc.cfg" file or any other unit?
« Last Edit: December 19, 2016, 07:25:56 pm by garlar27 »

 

TinyPortal © 2005-2018