Recent

Author Topic: ** Server  (Read 704 times)

michoux

  • Jr. Member
  • **
  • Posts: 79
** Server
« on: December 05, 2022, 11:54:38 am »
Hello

I created a new ** Server Application in Lazarus on port 8080
When ever I try to reach it on localhost:8080  from browser I get following erro

************************************++
The application encountered the following error:

    Error: Not found
    Stack trace:
    $00438D3C
    $0043873C
    $00438D9E
    $0043234F
    $00432A47
    $0042BCA5
    $00436047
    $004359FF
    $00435B84
    $004219F1
    $00402B7F
    $7730FEF9
    $778C7BBE
    $778C7B8E
*******************************
« Last Edit: December 05, 2022, 12:27:02 pm by michoux »

michoux

  • Jr. Member
  • **
  • Posts: 79
Re: ** Server
« Reply #1 on: December 05, 2022, 12:31:14 pm »
Somehow now server is working :)

I have following code, it is from tutorial of fpweb

Code: Pascal  [Select][+][-]
  1. procedure JSON(req: TRequest; res: TResponse);
  2. var
  3.   n: Integer;
  4.   f: TUploadedFile;
  5.   i: Integer;
  6. begin
  7.   n := req.Files.Count;
  8.   if n = 0 then
  9.     with res.Contents do
  10.     begin
  11.       Add('<form id="form" action="' + req.URI + '" method="POST" enctype="multipart/form-data">');
  12.       Add('<label for="name">Drag n drop or click to add file:</label>');
  13.       Add('<input type="file" name="input" />');
  14.       Add('<input type="submit" value="Send" />');
  15.       Add('</form>');
  16.     end
  17.   else
  18.   begin
  19.     f := req.Files[0];
  20.     res.Contents.LoadFromStream(f.Stream);
  21.   end;
  22.  
  23. end;                                      
  24.  
  25.  

I want to change this so that I receive file directly  with post
For example with curl.
c:\curl\bin\curl.exe -X GET --data-binary @c:\test.xml **://localhost:9080

Regards Mich

michoux

  • Jr. Member
  • **
  • Posts: 79
Re: ** Server
« Reply #2 on: December 05, 2022, 12:37:50 pm »
Hi,

Ok I found out all is written in request.

just a simple code will output the file content posted with curl.
c:\curl\bin\curl.exe -X GET --data-binary @c:\live_timing\race.xml **://localhost:9080/


Code: Pascal  [Select][+][-]
  1. procedure JSON(req: TRequest; res: TResponse);
  2. var
  3.   n: Integer;
  4.   f: TUploadedFile;
  5.   i: Integer;
  6.   s:string;
  7. begin
  8.   s := req.Content;
  9.  
  10. writeln(s);
  11.   end;        
  12.  

michoux

  • Jr. Member
  • **
  • Posts: 79
Re: ** Server
« Reply #3 on: December 05, 2022, 01:15:46 pm »
Now I have next question.

I make a form and I start a web server with a button.
form1.FP**Server2.Active:=true

This blocks my app totally.

PierceNg

  • Sr. Member
  • ****
  • Posts: 287
    • SamadhiWeb
Re: ** Server
« Reply #4 on: December 05, 2022, 01:36:33 pm »
Now I have next question.

I make a form and I start a web server with a button.
form1.FP**Server2.Active:=true

This blocks my app totally.

Run the ** server on a thread. Something like this:

Code: Pascal  [Select][+][-]
  1. type
  2.   TWebServerThread = class(TThread)
  3.     protected
  4.       procedure Execute; override;
  5.     public
  6.       constructor Create(CreateSuspended: boolean);
  7.   end;
  8.  
  9. constructor TWebServerThread.Create(CreateSuspended: boolean);
  10. begin
  11.   inherited Create(CreateSuspended);
  12.   FreeOnTerminate := true;
  13. end;
  14.  
  15. procedure TWebServerThread.Execute;
  16. begin
  17.   fp**app.Application.Run;
  18. end;
Blog: **s://samadhiweb.com/
Github: https://github.com/PierceNg

Pascal:
- **s://samadhiweb.com/tags/Pascal.html
- https://github.com/PierceNg/fpwebview
- https://github.com/PierceNg/wasm-demo

michoux

  • Jr. Member
  • **
  • Posts: 79
Re: ** Server
« Reply #5 on: December 05, 2022, 01:59:15 pm »
Thanks.

How do I call TWebServerThread.Execute from my app?

PierceNg

  • Sr. Member
  • ****
  • Posts: 287
    • SamadhiWeb
Re: ** Server
« Reply #6 on: December 05, 2022, 02:11:01 pm »
How do I call TWebServerThread.Execute from my app?

Code: Pascal  [Select][+][-]
  1. begin
  2.   // Set up web server.
  3.   fp**app.Application.Port := 8000;
  4.   fp**app.Application.Threaded := true; // web server itself is multi-threaded
  5.   fp**app.Application.Initialize;
  6.   // Run the server in a thread.
  7.   // If you want to start the server on button press, then move next line to the button press handler.
  8.   TWebServerThread.Create(false); // false means the server thread runs immediately
  9.  
  10.   // Now run LCL application.
  11.   RequireDerivedFormResource:=True;
  12.   Forms.Application.Scaled:=True;
  13.   Forms.Application.Initialize;
  14.   Forms.Application.CreateForm(TForm1, Form1);
  15.   Forms.Application.Run;  
  16. end.

See https://wiki.freepascal.org/Multithreaded_Application_Tutorial for more info.
Blog: **s://samadhiweb.com/
Github: https://github.com/PierceNg

Pascal:
- **s://samadhiweb.com/tags/Pascal.html
- https://github.com/PierceNg/fpwebview
- https://github.com/PierceNg/wasm-demo

michoux

  • Jr. Member
  • **
  • Posts: 79
Re: ** Server
« Reply #7 on: December 05, 2022, 03:42:02 pm »
Thank you very much.

I inserted form on which I have a report with lazReport.

After I get response from a webserver I want to call to open report, which is on form1 which I added.
I use
form1.frReport1.ShowReport;

This kills my app with SIGSERV ...
Is this related to thred?

PierceNg

  • Sr. Member
  • ****
  • Posts: 287
    • SamadhiWeb
Re: ** Server
« Reply #8 on: December 05, 2022, 04:01:16 pm »
Thank you very much.

I inserted form on which I have a report with lazReport.

After I get response from a webserver I want to call to open report, which is on form1 which I added.
I use
form1.frReport1.ShowReport;

This kills my app with SIGSERV ...
Is this related to thred?

Dunno. If you post a simplified but complete project that compiles and demonstrates the crash, then others will be better placed to help.
Blog: **s://samadhiweb.com/
Github: https://github.com/PierceNg

Pascal:
- **s://samadhiweb.com/tags/Pascal.html
- https://github.com/PierceNg/fpwebview
- https://github.com/PierceNg/wasm-demo

michoux

  • Jr. Member
  • **
  • Posts: 79
Re: ** Server
« Reply #9 on: December 05, 2022, 04:33:44 pm »
Here is simple code.
I also have a form with lazreport on it this is all.
I just want to open report with

 form1.frReport1.LoadFromFile('test.lrf');
    form1.frReport1.ShowReport;

Code: Pascal  [Select][+][-]
  1.  
  2. program Project1;
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6. uses
  7.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  8.   cthreads,
  9.   {$ENDIF}{$ENDIF}
  10.   Classes,SysUtils, fp**app, **defs, **route, fpjson, jsonparser, interfaces,forms,Unit1;
  11.  
  12. type
  13.   TWebServerThread = class(TThread)
  14.     protected
  15.       procedure Execute; override;
  16.     public
  17.       constructor Create(CreateSuspended: boolean);
  18.   end;
  19.  
  20.   constructor TWebServerThread.Create(CreateSuspended: boolean);
  21.   begin
  22.     inherited Create(CreateSuspended);
  23.     FreeOnTerminate := true;
  24.   end;
  25.  
  26.   procedure TWebServerThread.Execute;
  27.   begin
  28.     fp**app.Application.Run;
  29.   end;
  30.  
  31. procedure test(req: TRequest; res: TResponse);
  32. var
  33.   s:string;
  34.   rawJson: AnsiString;
  35.   people: TJSONArray;
  36.   person: TJSONObject;
  37.   personEnum: TJSONEnum;
  38. begin
  39.   // Get the JSON data
  40.     rawJson:=req.Content;
  41.      writeln(rawjson);
  42.  
  43.     form1.frReport1.LoadFromFile('test.lrf');
  44.     form1.frReport1.ShowReport;
  45. end;
  46.  
  47.  
  48.  
  49.  
  50. begin
  51.  
  52.  
  53.   fp**app.Application.Port := 8000;
  54.   **Router.RegisterRoute('/test', @test, true);
  55.   fp**app.Application.Threaded := true;
  56.   fp**app.Application.Initialize;
  57.   TWebServerThread.Create(false);
  58.  
  59.  
  60.   RequireDerivedFormResource:=True;
  61.   Forms.Application.Scaled:=True;
  62.   Forms.Application.Initialize;
  63.   Forms.Application.CreateForm(TForm1, Form1);
  64.   Forms.Application.Run;
  65.  
  66. end.
  67.  
  68.  
  69.  

PierceNg

  • Sr. Member
  • ****
  • Posts: 287
    • SamadhiWeb
Re: ** Server
« Reply #10 on: December 05, 2022, 04:46:46 pm »
Here is simple code.
I also have a form with lazreport on it this is all.
I just want to open report with

 form1.frReport1.LoadFromFile('test.lrf');
    form1.frReport1.ShowReport;

Code: Pascal  [Select][+][-]
  1.  
  2. program Project1;
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6. uses
  7.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  8.   cthreads,
  9.   {$ENDIF}{$ENDIF}
  10.   Classes,SysUtils, fp**app, **defs, **route, fpjson, jsonparser, interfaces,forms,Unit1;
  11.  
  12. type
  13.   TWebServerThread = class(TThread)
  14.     protected
  15.       procedure Execute; override;
  16.     public
  17.       constructor Create(CreateSuspended: boolean);
  18.   end;
  19.  
  20.   constructor TWebServerThread.Create(CreateSuspended: boolean);
  21.   begin
  22.     inherited Create(CreateSuspended);
  23.     FreeOnTerminate := true;
  24.   end;
  25.  
  26.   procedure TWebServerThread.Execute;
  27.   begin
  28.     fp**app.Application.Run;
  29.   end;
  30.  
  31. procedure test(req: TRequest; res: TResponse);
  32. var
  33.   s:string;
  34.   rawJson: AnsiString;
  35.   people: TJSONArray;
  36.   person: TJSONObject;
  37.   personEnum: TJSONEnum;
  38. begin
  39.   // Get the JSON data
  40.     rawJson:=req.Content;
  41.      writeln(rawjson);
  42.  
  43.     form1.frReport1.LoadFromFile('test.lrf');
  44.     form1.frReport1.ShowReport;
  45. end;
  46.  
  47.  
  48.  
  49.  
  50. begin
  51.  
  52.  
  53.   fp**app.Application.Port := 8000;
  54.   **Router.RegisterRoute('/test', @test, true);
  55.   fp**app.Application.Threaded := true;
  56.   fp**app.Application.Initialize;
  57.   TWebServerThread.Create(false);
  58.  
  59.  
  60.   RequireDerivedFormResource:=True;
  61.   Forms.Application.Scaled:=True;
  62.   Forms.Application.Initialize;
  63.   Forms.Application.CreateForm(TForm1, Form1);
  64.   Forms.Application.Run;
  65.  
  66. end.
  67.  
  68.  
  69.  

According to the wiki page on multi-threaded programming I linked to earlier, section 'Lazarus Widgetset Interfaces':

Quote
Only one thread in an application should call LCL APIs, usually the main thread. Other threads can make use of the LCL through a number of indirect methods, ...


So your '/test' web handler shouldn't do LCL GUI things directly. Instead, it should save the data received, and somehow inform the main thread to process the saved data. See the wiki page for ideas.
Blog: **s://samadhiweb.com/
Github: https://github.com/PierceNg

Pascal:
- **s://samadhiweb.com/tags/Pascal.html
- https://github.com/PierceNg/fpwebview
- https://github.com/PierceNg/wasm-demo

egsuh

  • Hero Member
  • *****
  • Posts: 1066
Re: ** Server
« Reply #11 on: December 06, 2022, 05:20:26 am »
Hi michoux,

I think here's an example you are looking for.

**s://www.freepascal.org/~michael/articles/webserver1/webserver1.pdf

You can also find the sources in the following list. Scroll down the list until you find webserver.

**s://www.freepascal.org/~michael/articles/
« Last Edit: December 06, 2022, 05:23:05 am by egsuh »

michoux

  • Jr. Member
  • **
  • Posts: 79
Re: ** Server
« Reply #12 on: December 06, 2022, 11:01:32 am »
Hi

Thank you for your help. I managed to get things going.
Now next step is to read some data and parse it.

I got following script which sends some data to serve.

Code: Pascal  [Select][+][-]
  1. <html>
  2.  
  3. <head>
  4.         <script src="**s://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
  5. </head>
  6.  
  7. <body>
  8.         <h1>Localhost service call test</h1>
  9.         <label for="url-call">URL to call:</label>
  10.         <input style="width: 400px;" type="text" id="url-call" name="url-call" value="**s://127.0.0.1:8000/sandbox"><br><br>
  11.         <a id="test-call" href="#">Call</a>
  12.  
  13.         <p id="odgovor"></p>
  14. </body>
  15.  
  16. <footer>
  17.         <script type="text/javascript">
  18.                 $("#test-call").click(function(){
  19.                         $.ajax({
  20.                                 type: 'POST',
  21.                                 url: $("#url-call").val(),
  22.                                 dataType: 'json',
  23.                                 data: {
  24.                                         "id": 10,
  25.                                         "title": "HP Pavilion 15-DK1056WM",
  26.                                         "description": "HP Pavilion 15-DK1056WM Gaming...",
  27.                                         "price": 1099,
  28.                                         "discountPercentage": 6.18,
  29.                                         "rating": 4.43,
  30.                                         "stock": 89,
  31.                                         "brand": "HP Pavilion",
  32.                                         "category": "laptops",
  33.                                         "thumbnail": "**s://i.dummyjson.com/data/products/10/thumbnail.jpeg",
  34.                                         "images": [
  35.                                                 "**s://i.dummyjson.com/data/products/10/1.jpg",
  36.                                                 "**s://i.dummyjson.com/data/products/10/2.jpg",
  37.                                                 "**s://i.dummyjson.com/data/products/10/3.jpg",
  38.                                                 "**s://i.dummyjson.com/data/products/10/thumbnail.jpeg"
  39.                                         ]
  40.                                 },
  41.                                 success: function (r) {
  42.                                         $("#odgovor").html("Odgovor: <br> Success: <br> " + JSON.stringify(r, null, 2));
  43.                                 },
  44.                                 error: function (r) {
  45.                                         $("#odgovor").html("Odgovor: <br> Error: <br> " + JSON.stringify(r, null, 2));
  46.                                 }
  47.                         }, $(this));
  48.                 });
  49.         </script>
  50. </footer>
  51.  
  52. </html>
  53.  
  54.  


I get the data but it is is not in real json format it is some strange outpout.

Here is the received data.
"id=10&title=HP+Pavilion+15-DK1056WM&description=HP+Pavilion+15-DK1056WM+Gaming...&price=1099&discountPercentage=6.18&rating=4.43&stock=89&brand=HP+Pavilion&category=laptops&thumbnail=**s%3A%2F%2Fi.dummyjson.com%2Fdata%2Fproducts%2F10%2Fthumbnail.jpeg&images%5B%5D=**s%3A%2F%2Fi.dummyjson.com%2Fdata%2Fproducts%2F10%2F1.jpg&images%5B%5D=**s%3A%2F%2Fi.dummyjson.com%2Fdata%2Fproducts%2F10%2F2.jpg&images%5B%5D=**s%3A%2F%2Fi.dummyjson.com%2Fdata%2Fproducts%2F10%2F3.jpg&images%5B%5D=**s%3A%2F%2Fi.dummyjson.com%2Fdata%2Fproducts%2F10%2Fthumbnail.jpeg"

Regards Matija

michoux

  • Jr. Member
  • **
  • Posts: 79
Re: ** Server
« Reply #13 on: December 06, 2022, 11:19:04 am »
I think above is problem with ajax code posting Json, I will try to fix this

 

TinyPortal © 2005-2018