A toy web framework demonstrating a boilerplate-free Sinatra-esque API.
Sinatra works by extending the main
object with get
, post
, etc. methods and then starting
the server using the at_exit
method.
This allows for writing an application like
require 'sinatra'
get '/howdy' do
'Howdy!'
end
and starting a server that will respond to GET
requests to http://localhost:4567/howdy with Howdy!
using
ruby example.rb
I find this equally blasphemous and beautiful, so now there’s a "simplified" Python port.
While Sinatra extends the main
object with methods that correspond to HTTP verbs, SinPy inspects the
(Python-equivalent) main
object and sets up routing for every function defined therein (that doesn’t
start with an underscore) based on the function name, e.g.
def myhandler():
return "This text will be the response requests to /myhandler"
python3 example.py
corresponds to
import sinpy
def hello():
return "Hello!"
def howdy():
return _i_am_a_howdy_helper()
def _i_am_a_howdy_helper():
return "Howdy!"
Once running, we can see
➜ python example.py Serving on port 8000...
If we open another shell and issue a few commands
➜ curl localhost:8000/hello Hello! ➜ curl localhost:8000/howdy Howdy! ➜ curl localhost:8000/_i_am_a_howdy_helper Not Found
we can see that the server has set up routing for the hello
and howdy
functions,
and ignored the "private" _i_am_a_howdy_helper
function.