A simple and fast HTTP framework for Python.
from gongish import Application
app = Application()
@app.route("/hello/:name")
def get(name: str):
return f"Hello {name}"
pip install gongish
Create a module (for example
main.py
) and define your first application.
from gongish import Application
app = Application()
@app.route('/')
def get():
return 'Index'
@app.route('/about')
def get():
return 'About'
now serve the app
with:
gongish serve main:app -b :8080
YAML is the configuration format we use as global app config which powered by
pymlconf
.
production.yaml
debug: False
redis:
host: localhost
port: port
main.py
from gongish import Application
app = Application()
@app.text('/')
def get():
return f'Redis Address: {app.config.redis.host}{app.config.redis.port}'
app.configure('production.yaml')
You can use OS environment variables inside config file:
sqlite:
path: %(HOME)/myappdb.sqlite
Python decorators are used to define routes and the wrapped function name must match with expected HTTP verb.
for example we want to call POST /user
, the route definition must be like:
@app.route('/user')
def post():
return 'User created!'
Methods for routing:
-
Exact path
@app.route('/') def get(): return 'Index' @app.route('/about') def get(): return 'About'
-
Positional arguments
@app.route('/user/:user_id') def get(user_id): return f'Hi {}' @app.route('/user/:id/book/:id') def get(user_id, book_id): return f'User #{user_id} and Book #{book_id}'
-
Wildcard
@app.json('/user/*') def get(*args): return args @app.json('/user/book/*') def get(*args): return args
When the response is ready, at final stage it will wrap by a formatter.
Available formatters: text
, json
, binary
from gongish import Application
app = Application()
@app.text('/')
def get():
return 'Index'
@app.json('/user')
def get():
return dict(name='John')
the text
formatter used as default, but you can change it:
from gongish import Application
class MyApp(Application):
default_formatter = Application.format_json
app = MyApp()
@app.route('/user')
def get():
return dict(name='John')
or in very special cases:
import yaml