Skip to content
/ sinpy Public

A toy web framework demonstrating a boilerplate-free Sinatra-esque API

Notifications You must be signed in to change notification settings

bradshjg/sinpy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

SinPy

Table of Contents

A toy web framework demonstrating a boilerplate-free Sinatra-esque API.

Motivation

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

example.rb
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.

API

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"

Demo

python3 example.py

corresponds to

example.py
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.

About

A toy web framework demonstrating a boilerplate-free Sinatra-esque API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages