janet-lang / circlet Goto Github PK
View Code? Open in Web Editor NEWHTTP server library for janet
License: Other
HTTP server library for janet
License: Other
I think, but am not certain, that some headers like 'Set Cookie' don't work so well comma separated. I think to work around this is may be nice to support arrays/tuples as header values indicating multiple values.
e.g.
:headers {"Set Cookie" [v1 v2 v3]}
I have implemented this in poolparty.
Currently circlet/server
takes only a port
parameter and binds to all available interfaces behind the scenes. The caller has no control over what interface(s) to bind to. I believe that this is not a good design and causes certain security risks. Any network server should bind to localhost
only as a default and make the interface explicitly controllable if desired otherwise.
I propose a modification with the following semantics:
(server 8000) ==> Circlet server listening on [localhost:8000] ...
(server 8000 "localhost") ==> Circlet server listening on [localhost:8000] ...
(server 8000 "192.168.1.130") ==> Circlet server listening on [192.168.1.130:8000] ...
(server 8000 "*") ==> Circlet server listening on [*:8000] ... (all interfaces)
Fortunately the necessary changes are minimal and can be done on high level in circlet_lib.janet
:
diff --git a/circlet_lib.janet b/circlet_lib.janet
index 52b3d0e..23fe61d 100644
--- a/circlet_lib.janet
+++ b/circlet_lib.janet
@@ -35,13 +35,18 @@
(defn server
"Creates a simple http server"
- [port handler]
+ [handler port &opt ip-address]
(def mgr (manager))
(def mw (middleware handler))
+ (default ip-address "localhost")
+ (var interface nil)
+ (if (peg/match "*" ip-address)
+ (set interface (string port))
+ (set interface (string/format "%s:%d" ip-address port)))
(defn evloop []
- (print "Circlet server listening on port " port "...")
+ (print (string/format "Circlet server listening on [%s:%d] ..." ip-address port))
(var req (yield nil))
(while true
(set req (yield (mw req)))))
- (bind-http mgr (string port) evloop)
+ (bind-http mgr interface evloop)
(while true (poll mgr 2000)))
Hey I'm studying janet/c and I was trying to upgrade to the latest mongoose here, but the changes in the API are huge.
I would like to know if there is any plans to use the new API?
Because there is no documentation other than a simple example, I don't know how to use it well.
Hi, I'm trying to write a little web app on circlet and I'm trying to spawn some processes like this one when I receive a request to certain endpoint.
(defn getls
"gets ls from bin dir" []
(def procc (os/spawn ["ls"] :p {:in :pipe :out :pipe :err :pipe}))
(pp :waiting_for_proc)
(pp (:wait procc))
[(:read (procc :out) :all)
(:read (procc :err) :all)])
the first time I get the request nothing happens, but when I try to request again I got an error that says:
current fiber is already waiting for event
could you explain me why this happens? is this a bug or maybe I'm not familiar enough with how things work in Janet or with Circlet
I think I should be able to just set the content-type to the appropriate value and serve a file by writing the slurped file to the body of the response, and this indeed works for ascii files. But not for binary files like pngs.
When I run below code and visit the page, the browser says the file is broken and cannot be displayed.
Tried different pngs and different image formats. The response's body seems to contain only the first 10 Bytes of the image I slurp. That slice coincides with a byte of 0x00 in the file. The content type and length are set to their correct values.
(import circlet)
(defn myserver [req]
(def data (slurp (string (os/cwd) "/test.png")))
{:status 200
:headers {"Content-Type" "image/png"}
:body data})
(circlet/server myserver 8000)
excerpt from test.png:
8950 4e47 0d0a 1a0a 0000 000d 4948 4452 0000 03c0 0000 03cd ...
sole content of received body:
8950 4e47 0d0a 1a0a 0d0a
If port is not free, app will segfault
Try to run a start command twice in a row
I wanted to try wrapping the websocket stuff, so I looked at a mongoose example: https://github.com/cesanta/mongoose/tree/master/examples/websocket-server
It needs latest mongoose though, so I tried jpm run update-mongoose
. However, now jpm build
is unable to build mongoose.
It appears as though some required files aren't included (e.g. time.h
and limit.h
). I could resolve this by hard coding includes for these, but I don't need to do that if I compile mongoose manually using gcc (without jpm build that is), so I'm unsure about what's going on.
I'm on Ubuntu, Janet 1.17.1-b799223e and jpm c2be9143201154c2b068f954474f1e0983f81189.
Here's the errors I get after running update-mongoose then jpm build. I understand that I get circlet-errors, it's the ones relating to mongoose.h I'm confused about: https://pastebin.com/6fzA15w6
Example console output
PS C:\devel\circlet> jpm test
running test\testserv.janet ...
error: could not bind to localhost:19876
in circlet/bind-http
in server [circlet_lib.janet] on line 69, column 3
in _thunk [test\testserv.janet] (tailcall) on line 4, column 1
I have a pull request incoming.
I'm using Janet 1.0.0-dev-232ea22 from janet-lang-git on the AUR.
Unfortunately build won't run because Janet doesn't know where cook is.
... Any idea where I can find this dependency?
Lets say that I want to write an API in Janet/C.
I have janet and jpm. I built circlet and run the test. I now have a build/circlet.so file - would that (and the appropriate janet files) be all I need to pull into my own codebase? Where I could then initialize a server similar to the sample in readme? just an (import circlet)
?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.