Writing fpWeb app without Lazarus

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Writing fpWeb app without Lazarus

Mr Bee
Hi all,

I'm now learning how to use fpWeb. But for some reasons, I don't want to use Lazarus. Instead, I want to write the program using plain text editor or console text editor (like vim, nano, etc), in case I need to do it remotely. It's now just a simple Hello World program, so it must be simple enough to write the program as a single file without Lazarus. However, I failed. There must be something stupid that I forgot to write but I don't know what that is. So here's my code:
 
program HelloWeb;

{$MODE OBJFPC}{$H+}{$J-}

uses
  Classes, HTTPDefs, fpHTTP, fpCGI, fpWeb;

type
  THelloModule = class(TFPWebModule)
    procedure HelloReq(Sender: TObject; ARequest: TRequest;
                       AResponse: TResponse; var Handled: Boolean);
  end;

var
  HelloModule: THelloModule;

procedure THelloModule.HelloReq(Sender: TObject; ARequest: TRequest;
                                AResponse: TResponse; var Handled: Boolean);
begin
  AResponse.ContentType := 'text/html;charset=utf-8';
  AResponse.Content := '<html><body>Hello World!</body></html>';
  Handled := true;
end;

begin
  HelloModule := THelloModule.Create(nil);
  HelloModule.ActionVar := 'm';
  HelloModule.OnRequest := @HelloModule.HelloReq;

  RegisterHTTPModule('THelloModule',THelloModule,true);

  Application.Title := 'Hello World';
  Application.Initialize;
  Application.Run;
end.

It was compiled fine without any errors using -XXs -CX -O3 parameters. But when the executable is deployed and then accessed from the browser, it returns 500 internal server error. I don't know what to do as I saw nothing is wrong.

That's the first problem. The second one is a question. I'd like to use fpWeb components in plain procedural way, without using the TApplication infrastructure, especially for simple CGI apps/services. Is it possible? Any advices?

I'm using a Linux Ubuntu 14.04 machine with Apache web server. I believe the problem has nothing to do with the OS nor the web server because my plain Pascal CGI (without using fpWeb) is running just fine.

Any hints to solve the problem is really appreciated.

Thank you.

Regards,

–Mr Bee


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Writing fpWeb app without Lazarus

Michael Van Canneyt


On Sun, 5 Jun 2016, Mr Bee wrote:

> Hi all,
> I'm now learning how to use fpWeb. But for some reasons, I don't want to use Lazarus. Instead, I want to write the program using plain text editor or console text editor (like vim, nano, etc), in case I need to do it remotely. It's now just a simple Hello World program, so it must be simple enough to write the program as a single file without Lazarus. However, I failed. There must be something stupid that I forgot to write but I don't know what that is. So here's my code: program HelloWeb;
> {$MODE OBJFPC}{$H+}{$J-}
> uses  Classes, HTTPDefs, fpHTTP, fpCGI, fpWeb;
> type  THelloModule = class(TFPWebModule)    procedure HelloReq(Sender: TObject; ARequest: TRequest;                       AResponse: TResponse; var Handled: Boolean);  end;
> var  HelloModule: THelloModule;
> procedure THelloModule.HelloReq(Sender: TObject; ARequest: TRequest;                                AResponse: TResponse; var Handled: Boolean);begin  AResponse.ContentType := 'text/html;charset=utf-8';  AResponse.Content := '<html><body>Hello World!</body></html>';  Handled := true;end;
> begin  HelloModule := THelloModule.Create(nil);  HelloModule.ActionVar := 'm';  HelloModule.OnRequest := @HelloModule.HelloReq;
>   RegisterHTTPModule('THelloModule',THelloModule,true);
>   Application.Title := 'Hello World';  Application.Initialize;  Application.Run;end.
> It was compiled fine without any errors using -XXs -CX -O3 parameters. But when the executable is deployed and then accessed from the browser, it returns 500 internal server error.
> I don't know what to do as I saw nothing is wrong.
What is the URL you used to access your app ?
What is the output in the browser ?

Then, never create an instance of a webmodule manually, always let the
framework create instances.

Also, a statement as

HelloModule := THelloModule.Create(nil);

is wrong, since it will attempt to stream the module. If you do want to
create modules manually, do
HelloModule := THelloModule.CreateNew(nil,0);


> That's the first problem. The second one is a question. I'd like to use fpWeb components in plain procedural way, without using the TApplication infrastructure, especially for simple CGI apps/services. Is it possible? Any advices?

No, this is not possible.
It was explicitly designed to use classes in a RAD environment.
It is perfectly possible to drop the RAD part and use the classes as-is, but
it is not possible not to use the classes.

> I'm using a Linux Ubuntu 14.04 machine with Apache web server. I believe the problem has nothing to do with the OS nor the web server because my plain Pascal CGI (without using fpWeb) is running just fine.

Without more information, it is hard to say.

There are multiple examples in the FPC source tree;
Why don't you use those as a starting point ?

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Bls: Writing fpWeb app without Lazarus

Mr Bee
Pada Selasa, 7 Juni 2016 15:23, Michael Van Canneyt <[hidden email]> menulis:



> What is the URL you used to access your app ?

http://beeography.koding.io/hello.cgi (/mod) (/act [/act1 | act/act2])

And here's the source code: http://pastebin.com/N7EQz4Lk 
Feel free to correct any mistakes in my code. Thank you.

> What is the output in the browser ?

Just a "hello world" message.

> Then, never create an instance of a webmodule manually, always let the
framework create instances.

Yes, the problem has been solved with a help from Mario's ebook (https://bitbucket.org/leledumbo/books/downloads/fpwebtutorial-chromeprint.pdf). Mario is also known as @leledumbo at Lazarus' forum.

> Also, a statement as
> HelloModule := THelloModule.Create(nil);
> is wrong, since it will attempt to stream the module. If you do want to
> create modules manually, do
> HelloModule := THelloModule.CreateNew(nil,0);

Yes, I should have figured it out earlier, but it's been a long time since the last time I used TDataModule, so there are many things that I forgot about VCL. In fact, that's the main source of my problem. Once I used the CreateNew constructor, everything works as expected. Though I still have many things to learn to understand how the fpWeb works. :)

> No, this is not possible.
> It was explicitly designed to use classes in a RAD environment.
> It is perfectly possible to drop the RAD part and use the classes as-is, but
> it is not possible not to use the classes.

I don't mind the clasess, but I do mind the RAD part. A web app development should be able to be done without the RAD part because in some cases we don't always have the access to use the RAD.

> There are multiple examples in the FPC source tree; 
> Why don't you use those as a starting point ?

I did. But all the examples I found are using Lazarus aka RAD. Hence this email.

fpWeb —and all the gems of FCL— do really need proper and complete documentations. It's hard —especially for Pascal newbies— to learn FCL from the source code alone. Documentation is the main and first obstacle for everyone who like to use FCL to its potential. I wish I could help.

Thank you for the reply, Michael. 

Regards,

–Mr Bee


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Bls: Writing fpWeb app without Lazarus

Michael Van Canneyt


On Wed, 8 Jun 2016, Mr Bee wrote:

> Pada Selasa, 7 Juni 2016 15:23, Michael Van Canneyt <[hidden email]> menulis:
>
>
>
>> What is the URL you used to access your app ?
> http://beeography.koding.io/hello.cgi (/mod) (/act [/act1 | act/act2])
>
> And here's the source code: http://pastebin.com/N7EQz4Lk Feel free to correct any mistakes in my code. Thank you.
>> What is the output in the browser ?
> Just a "hello world" message.
>
>> Then, never create an instance of a webmodule manually, always let the
> framework create instances.
> Yes, the problem has been solved with a help from Mario's ebook (https://bitbucket.org/leledumbo/books/downloads/fpwebtutorial-chromeprint.pdf). Mario is also known as @leledumbo at Lazarus' forum.
>> Also, a statement as
>> HelloModule := THelloModule.Create(nil);
>> is wrong, since it will attempt to stream the module. If you do want to
>> create modules manually, do
>> HelloModule := THelloModule.CreateNew(nil,0);
>
> Yes, I should have figured it out earlier, but it's been a long time since the last time I used TDataModule, so there are many things that I forgot about VCL. In fact, that's the main source of my problem. Once I used the CreateNew constructor, everything works as expected. Though I still have many things to learn to understand how the fpWeb works. :)
>
>> No, this is not possible.
>> It was explicitly designed to use classes in a RAD environment.
>> It is perfectly possible to drop the RAD part and use the classes as-is, but
>> it is not possible not to use the classes.
> I don't mind the clasess, but I do mind the RAD part. A web app development should be able to be done without the RAD part because in some cases we don't always have the access to use the RAD.
I develop using fpweb all the time, without RAD, so I know this works :-)

>
>> There are multiple examples in the FPC source tree; 
>> Why don't you use those as a starting point ?
> I did. But all the examples I found are using Lazarus aka RAD. Hence this email.
> fpWeb —and all the gems of FCL— do really need proper and complete documentations.
> It's hard —especially for Pascal newbies— to learn FCL from the source code alone.
> Documentation is the main and first obstacle for everyone who like to use FCL to its potential. I wish I could help.

There are over 1000 units. There is only 1 documenter...

On
http://www.freepascal.org/~michael/articles/
you can find some articles that explain the workings of fcl-web.
See web1, web2.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Bls: Writing fpWeb app without Lazarus

Graeme Geldenhuys-6
In reply to this post by Mr Bee
On 2016-06-08 07:37, Mr Bee wrote:
> I did. But all the examples I found are using Lazarus aka RAD.

If you look at the wiki, then yes, that is predominantly a Lazarus
support area. The FCL packages normally included example projects too,
or unit tests. Both are handy for leaning usage of packages.

> fpWeb —and all the gems of FCL— do really need proper and
> complete documentations.

Granted, not all FCL packages are document, but those that are, and the
rest of FPC's documentation is excellent. As Michael mentioned, there is
a library of technical articles too - those are very useful. And if all
else fails, don't hesitate to ask here. Excellent advise can be had here.


Regards,
  Graeme

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Bls: Bls: Writing fpWeb app without Lazarus

Mr Bee
In reply to this post by Michael Van Canneyt
Pada Rabu, 8 Juni 2016 15:59, Michael Van Canneyt <[hidden email]> menulis:



> I develop using fpweb all the time, without RAD, so I know this works :-)

Of course you do. You are the main author of fpWeb, after all. :) It's me who didn't know how to do it due the lack of documentation.

> There are over 1000 units. There is only 1 documenter…

I know. You're our hero, Michael. Your work on documentations are really helpfull. Thank you very much.

> On http://www.freepascal.org/~michael/articles/
> you can find some articles that explain the workings of fcl-web.
> See web1, web2.

I'll save the link. This is a treasure.

Thank you.

Regards,

-Mr Bee


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Bls: Writing fpWeb app without Lazarus

leledumbo
Administrator
In reply to this post by Michael Van Canneyt
> On
> http://www.freepascal.org/~michael/articles/
> you can find some articles that explain the workings of fcl-web.
> See web1, web2.

WOW! You never said you did document for many things we don't know how to use properly yet. Must be put somewhere everyone else can see, Lazarus wiki would be good. Are you OK if I do that?