Skip to main content

gongish

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}"

Installation

pip install gongish

Usage

Quickstart

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

Configuration

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

Routing

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:

  1. Exact path

    @app.route('/')
    def get():
        return 'Index'
    
    @app.route('/about')
    def get():
        return 'About'
    
  2. 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}'
    
  3. Wildcard

    @app.json('/user/*')
    def get(*args):
        return args
    
    @app.json('/user/book/*')
    def get(*args):
        return args
    

Formatters

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
from gongish import Application

app = Application()

def format_yaml(request, response):
    response.type = 'text/x-yaml'
    response.body = yaml.dump(response.body)

@app.route('/user', formatter=format_yaml)
def get():
    return dict(name='John')

Exceptions

from gongish import Application, HTTPNotFound, HTTPNoContent, HTTPFound

app = Application()

@app.route('/user/:id')
def get(user_id):
    if user_id == '0':
        raise HTTPNotFound

    return dict(name='John')

@app.route('/user/:id')
def put(user_id):
    raise HTTPNoContent

@app.route('/redirectme')
def get():
    raise HTTPFound('https://github.com')

Complete list available in gongish/exceptions.py .

Streaming

You can use Python Generators as route handler:

@app.route('/')
def get():
    yield 'First'
    yield 'Second'

with HTTP chunked data transfer:

@app.binary('/')
@app.chunked
def get():
    yield b'First'
    yield b'Second'

Static Server

You can serve static files inside a directory like:

from gongish import Application

app = Application()
app.add_static('/public', '/var/www')
app.add_static('/another/public', '/var/www', default_document='index.html5')

Note: Static file handler designed for some limited use cases. for large projects use web servers like nginx instead.

Credits

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gongish-0.5.1.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gongish-0.5.1-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file gongish-0.5.1.tar.gz.

File metadata

  • Download URL: gongish-0.5.1.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.10

File hashes

Hashes for gongish-0.5.1.tar.gz
Algorithm Hash digest
SHA256 8d73cc64d5105d91d85c2ea8da284c56dcdf46b511f999a6978a1d1ea1bc3086
MD5 3dd4f67dba264f3d7dd05ddb5715d75c
BLAKE2b-256 6a16fb2bb1f62b6c49dcbb1765834a8d6e891d4d34ee9cf1808c320910653e41

See more details on using hashes here.

File details

Details for the file gongish-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: gongish-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.10

File hashes

Hashes for gongish-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8bda591a75dfaee7add215f6f9fab6f5b96c5bc0b23f186dcc4f1f917a28fbe8
MD5 1b96d2a6e65f46ae1fea0fa174dcc4cc
BLAKE2b-256 ba51d69368ad6cdb45d1e2c2837523a0ecf9e8bd5ae448c3e69558b74c192978

See more details on using hashes here.

Release history Release notifications | RSS feed

1.5.0

2 files

1.4.0

2 files

1.3.0

2 files

1.2.0

2 files

1.1.0

2 files

1.0.1

2 files

1.0.0

2 files

0.11.0

2 files

0.10.0

2 files

0.9.1

2 files

0.9.0

2 files

0.8.0

2 files

0.7.0

2 files

0.6.1

2 files

0.6.0

2 files

This release

0.5.1 This release

2 files

0.5.0

2 files

0.4.0

2 files

0.3.0

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page