httpd-conf intercept requests and answer with a function?

Q&A's, tips, howto's

httpd-conf intercept requests and answer with a function?

Postby gekkonier » Tue Jan 30, 2018 12:26 pm

Hi,
I'd like to tinker a bit with the inbuilt webserver.
I managed to fire one up that serves form a directory, and logs ervery request into a file.
For this i used (command-event (fn (s)) in a httpd-conf.lsp file.
This I use with
Code: Select all
newlisp httpd-conf.lsp -http -d 80 -w ./www


In this command-event i can append-file the requests into a txt file to look what a "user" is looking for.
That all works good.

Now I would like to implement something more.

Lets say: if my request looks like this:
Code: Select all
GET /hubert HTTP/1.1

I know i can parse the request for lets say "hubert".

But how can I "route" to a custom definition which ansers to hubert?
Lets say i have this here:

Code: Select all
(define (my-hubert)
   (print "hello world"))


How can I "answer" with (my-hubert) if someone does a GET /hubert. Instead of a file sitting in www?

Thank you so much for your input in advance,
Gregor
gekkonier
 
Posts: 14
Joined: Fri Jan 26, 2018 7:44 pm

Re: httpd-conf intercept requests and answer with a function

Postby gekkonier » Tue Jan 30, 2018 6:51 pm

After further investigation it seems it's the easiest way to do it with cgi on apache. and dont use the inbuilt webserver.
gekkonier
 
Posts: 14
Joined: Fri Jan 26, 2018 7:44 pm

Re: httpd-conf intercept requests and answer with a function

Postby rickyboy » Tue Jan 30, 2018 9:10 pm

Dragonfly web framework is written in newlisp

http://www.taoeffect.com/dragonfly-newl ... mple-site/

and has the functionality you seek (concerning routes):

http://www.taoeffect.com/dragonfly-newl ... fly_routes

Check out / steal some code! :) Happy hacking!
(λx. x x) (λx. x x)
rickyboy
 
Posts: 551
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: httpd-conf intercept requests and answer with a function

Postby gekkonier » Wed Jan 31, 2018 10:29 am

Thank you rickyboy,

I need something much more simpler.
I'd like to program inhouse tools for 2-3 people in company, and for my family at home.
I like lisp like syntax much more (than my previous vehicle ruby) so I tried Common Lisp, Clojure, Racket for this tasks, but:
Common Lisp is overkill and I don't like 20mb images just for serving simple stuff. Racket ditto, the whole stack is bloated (at least it really works!), and Clojure is on Raspberries a pain.
Now newLISP - i LIKE it very much, so far.

I took a look at both newlisp on rockets and dragonfly - that seem to me the more sophisticated options for webdev with newLISP, but both are to much for me.

Nothing fancy ;)
gekkonier
 
Posts: 14
Joined: Fri Jan 26, 2018 7:44 pm

Re: httpd-conf intercept requests and answer with a function

Postby ralph.ronnquist » Thu Feb 01, 2018 3:38 am

For small scale http services I typically use thttpd as frontend, with newlisp for the "cgi" scripting. Quick and easy :)
ralph.ronnquist
 
Posts: 176
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: httpd-conf intercept requests and answer with a function

Postby varbanov » Mon May 21, 2018 3:56 pm

Hi Gregor,
For similar purpose I start a server with something like:
Code: Select all
newlisp.exe -c -d 4780 -w ..\MyProjects\ToWeb

(on Windows OS)

In ..\MyProjects\ToWeb I've put index.cgi which might look like (minimalistic):

Code: Select all
#!/usr/bin/newlisp

(print "Content-type: text/html\r\n\r\n")
(module "cgi.lsp")

(define (my-hubert) (println "<h3>Hello World!</h3>"))

(apply (read-expr  (CGI:params 0 0)))

(exit)


Then I open in a browser http://localhost:4780/?my-hubert

:) Not the absolute elegance, but it works...

PS notes:
- I don't remember why I left -c parameter when starting the server. May be it's not important and -http will be fine too
- The first line of index.cgi is important for Linux. I use the same code on both Windows and Linux machines at home.
- In index.cgi you can use (load "path-to-some-newlisp-file") to load more functions and/or data, of course.
- I'm using this for a very simplistic implementation of Wiki combined with Markdown-like coding ... it's great fun to see it working with so small code :)

Good Luck and please, share how Your experiments go.

PPS. I'm sorry, I didn't pay enough attention to httpd-conf.lsp. It's obvious, that the command-event function could transform a nicer "outside" url like http://localhost:4780/my-hubert to the "inner" for my approach url, i.e. http://localhost:4780/?my-hubert
varbanov
 
Posts: 6
Joined: Mon Jul 01, 2013 1:33 pm
Location: Sofia, Bulgaria

Re: httpd-conf intercept requests and answer with a function

Postby gekkonier » Fri May 25, 2018 7:00 pm

Hi varbanov,
thank you very much for your example. I try to setup your example and tinker with it.
Sorry for that late response, i got so much work to do.

Gregor
gekkonier
 
Posts: 14
Joined: Fri Jan 26, 2018 7:44 pm


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 1 guest

cron