Skip to main content

Minimal web backend framework

Project description

blite

  • minimal web backend framework

Usage

  • first step is to import the library and instantiate a Server object, passing the host and port to bind to the server
from blite import Server, renderFile

HOST = '0.0.0.0'
PORT = 8080

server = Server(HOST, PORT)
  • logging level can be set to:

    • silent executing server.setLogLevel(LogLevel.SILENT)
    • concise executing server.setLogLevel(LogLevel.CONCISE)
    • verbose executing server.setLogLevel(LogLevel.VERBOSE)
  • then define the routes, using the @server.setRoute() decorator

    • the decorator requires a route parameter, which is the URI relative to the callback function
@server.setRoute('/')
def root():
    return renderFile('index.html')
  • additional parameters can be given to the decorator, such as

    • methods: [str] which defaults to ['GET']
      • methods must be specified to be accepted by the framework, otherwise it will answer to the client with a 405 Method Now Allowed HTTP response
    • regex: bool which defaults to False
      • indicates that the route is specified as a regex pattern
    • allowOptions: bool which defaults to True
      • tells the framework if it should answer to OPTIONS requests for the given route
    • giveMethod: bool which defaults to False
      • if set to True, the framework will provide the callback function with the method used by the client
    • giveUri: bool which defaults to False
      • if set to True, the framework will provide the callback function with the uri called by the client
    • giveRequest: bool which defaults to False
      • if set to True, the framework will provide the callback with the request bytes
    • kwargs: tuple(str) which defaults to ()
      • if the tuple is populated, the framework will expect parameters to be given into the URI
      • e.g. http://HOST:PORT/URI?key=value&other_key=other_value
  • callback functions can either return someting or None

  • allowed return values (different from None) are

    • string, which is intended as response body
    • bytes, which is indended as response body
    • tuple(headers: dict, responseBody: Any)
    • dict, which is intended as json response body
    • Response
      • custom class which allows to set HTTP code, response headers and response body
  • for full usage example, take a look at the following code

Usage Example

from blite import Server, Response, renderFile, HttpCode, LogLevel
import json

HOST = '0.0.0.0'
PORT = 8080

server = Server(HOST, PORT)

# set the headers to be used in every HTTP response   
server.setStandardHeaders({'Access-Control-Allow-Origin' : '*'})

# sets concise logging level
server.setLogLevel(LogLevel.CONCISE)

@server.setRoute('/')
def root():
    renderFile('index.html')


@server.setRoute('/auth', kwargs=('wants_token'), methods=['POST'], allowOptions=False)
def authUser(requestBody, wants_token):
    try:
        jData = json.loads(requestBody.decode())

    except:
        return Response(code=HttpCode.NOT_FOUND)

    uname = jData.get('username')
    passwd = jData.get('password')

    if uname is None or passwd is None:
        return Response(code=HttpCode.NOT_FOUND)

    authenticated = checkCredentials(uname, passwd)  # function defined elsewhere

    if not authenticated:
        return 'failed'

    if wants_token == 'true':
        token = makeToken(uname, passwd)  # function defined elsewhere
        return {'token': token}

    return 'ok'


@server.setRoute('/get_data', methods=['POST'], giveMethod=True)
def getData(requestBody, method):
    try:
        jData = json.loads(requestBody.decode())
    except:
        return Response(code=HttpCode.NOT_FOUND)

    authenticated = checkAuthentication(jData)  # function defined elsewhere
    if not authenticated:
        return Response(code=HttpCode.FORBIDDEN)

    jsonData = fetchData()  # function defined elsewhere, which returns a dict
    return jsonData


@server.setRoute('^/get_image/[0-9a-zA-Z]*\.(jpg|png|jpeg)\/?$', regex=True, giveUri=True)
def getImage(uri):
    imagePath = findImagePath(uri)  # function defined elsewhere
    imageBytes = getImageBytes(imagePath)  # function defined elsewhere

    if not imageBytes:
        return Response(code=HttpCode.NOT_FOUND)

    imageExtension = imagePath.split('.')[-1]
    return {'Content-Type': f'image/{imageExtension}'}, imageBytes

Project details


Download files

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

Source Distribution

blite-1.1.0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

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

blite-1.1.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file blite-1.1.0.tar.gz.

File metadata

  • Download URL: blite-1.1.0.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for blite-1.1.0.tar.gz
Algorithm Hash digest
SHA256 276b47e12480e02896c5d1032a3272beb64d58a4bffde202529d94f49c239430
MD5 9679ad6ac81b20b60656110efe5b7f1b
BLAKE2b-256 26ba05875b13eb404f299abd853ddec3ed504cd0ed589e13e949ed20127aa3a6

See more details on using hashes here.

File details

Details for the file blite-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: blite-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for blite-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cb563cba27b9a5e97aeae52366c0484f2993c0550b6cc247cfae03ba232e73a0
MD5 53909c2946502dc898257b0ed10c1fd3
BLAKE2b-256 d5ab0aa974163aeb15c1e005f17a96742a1cf569779e4ab8d4543e835d48388d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page