httpd-conf intercept requests and answer with a function?

Started by gekkonier, January 30, 2018, 04:26:30 AM

Previous topic - Next topic

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 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:
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:


(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

After further investigation it seems it's the easiest way to do it with cgi on apache. and dont use the inbuilt webserver.

rickyboy

Dragonfly web framework is written in newlisp



http://www.taoeffect.com/dragonfly-newlisp/example-site/">http://www.taoeffect.com/dragonfly-newl ... mple-site/">http://www.taoeffect.com/dragonfly-newlisp/example-site/



and has the functionality you seek (concerning routes):



http://www.taoeffect.com/dragonfly-newlisp/example-site/dragonfly_routes">http://www.taoeffect.com/dragonfly-newl ... fly_routes">http://www.taoeffect.com/dragonfly-newlisp/example-site/dragonfly_routes



Check out / steal some code!  :) Happy hacking!
(λx. x x) (λx. x x)

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 ;)

rrq

For small scale http services I typically use https://acme.com/software/thttpd">thttpd as frontend, with newlisp for the "cgi" scripting. Quick and easy :)

varbanov

#5
Hi Gregor,

For similar purpose I start a server with something like:
newlisp.exe -c -d 4780 -w ..MyProjectsToWeb
(on Windows OS)



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



#!/usr/bin/newlisp

(print "Content-type: text/htmlrnrn")
(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">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">http://localhost:4780/my-hubert to the "inner" for my approach url, i.e. http://localhost:4780/?my-hubert">http://localhost:4780/?my-hubert

gekkonier

#6
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