Page 1 of 1
httpd-conf intercept requests and answer with a function?
Posted: Tue Jan 30, 2018 12:26 pm
by gekkonier
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:
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
Re: httpd-conf intercept requests and answer with a function
Posted: Tue Jan 30, 2018 6:51 pm
by gekkonier
After further investigation it seems it's the easiest way to do it with cgi on apache. and dont use the inbuilt webserver.
Re: httpd-conf intercept requests and answer with a function
Posted: Tue Jan 30, 2018 9:10 pm
by rickyboy
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!
Re: httpd-conf intercept requests and answer with a function
Posted: Wed Jan 31, 2018 10:29 am
by gekkonier
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 ;)
Re: httpd-conf intercept requests and answer with a function
Posted: Thu Feb 01, 2018 3:38 am
by ralph.ronnquist
For small scale http services I typically use
thttpd as frontend, with newlisp for the "cgi" scripting. Quick and easy :)
Re: httpd-conf intercept requests and answer with a function
Posted: Mon May 21, 2018 3:56 pm
by varbanov
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
Re: httpd-conf intercept requests and answer with a function
Posted: Fri May 25, 2018 7:00 pm
by gekkonier
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