Comments:"hackerschool/webstack.jl · GitHub"
URL:https://github.com/hackerschool/webstack.jl
WIP: Implementing the webstack in Julia.
Dependencies
- Julia, since it's that's what it's all written in.
- HttpParser.jl, see HttpParser/README.md. This julia module requires a C shared library.
- Calendar.jl, installable via
Pkg.add("Calendar")
in the Julia REPL. Repo here.
Simple HTTP hello world:
If you want to handle things at the level of just HTTP requests and responses, this is a simple example of returning "Hello !" only on "/hello/" paths, and a 404 on all others.
Put it in a file called http.jl
and then use the command julia http.jl
to run it.
If you open localhost:8000/hello/user
, you'll get to see the server say hello.
usingHttphttp=HttpHandler()doreq::Request,res::ResponseResponse(ismatch(r"^/hello/",req.resource)?string("Hello ",split(req.resource,'/')[3],"!"):404)endhttp.events["error"]=(client,err)->println(err)http.events["listen"]=(port)->println("Listening on $port...")server=Server(http)run(server,8000)
Middleware using Meddle
:
This is an example of using our middleware framework, Meddle. It puts each request through a "stack" of handlers, each of which can modify the request and/or response objects.
This particular example is a file server.
It will serve files from the directory you run it in,
and respond with 404
s if you give an invalid path.
Put it in a file called meddle.jl
and then use the command julia meddle.jl
to run it.
If you run it from inside your webstack.jl
folder,
you could then open localhost:8000/examples/meddle.jl
to have it serve its own code to you.
usingHttpusingMeddlestack=[DefaultHeaders(),CookieDecoder(),FileServer(pwd()),NotFound()]http=HttpHandler((req,res)->handle(stack,req,res))foreventinsplit("connect read write close error")http.events[event]=((event)->(client,args...)->println(client.id,": $event"))(event)endhttp.events["error"]=(client,err)->println(err)server=Server(http)run(server,8000)
Web Application with Micro
:
Micro is a Sinatra-like micro framework for declaring routes and handling requests. It is built on top of Http
and Meddle
.
Here is a brief example that will return a few different messages for different routes, if you run this and open localhost:8000
you will see "This is the root" for GET, POST or PUT requests. The line get(app, "/about") do ...
is shorthand for only serving GET requests through that route.
usingMicroapp=Micro.app()route(app,GET|POST|PUT,"/")doreq,res"This is the root"endget(app,"/about")doreq,res"This app is running on Micro"endstart(app,8000)