Minimal web backend framework
Project description
blite
- minimal web backend framework
Usage
- first step is to import the library and instantiate a
Serverobject, 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)
- 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 AllowedHTTP response
- methods must be specified to be accepted by the framework, otherwise it will answer to the client with a
regex: boolwhich defaults toFalse- indicates that the route is specified as a regex pattern
allowOptions: boolwhich defaults toTrue- tells the framework if it should answer to
OPTIONSrequests for the given route
- tells the framework if it should answer to
giveMethod: boolwhich defaults toFalse- if set to True, the framework will provide the callback function with the method used by the client
giveUri: boolwhich defaults toFalse- if set to True, the framework will provide the callback function with the uri called by the client
giveRequest: boolwhich defaults toFalse- 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) arestring, which is intended as response bodybytes, which is indended as response bodytuple(headers: dict, responseBody: Any)dict, which is intended as json response bodyResponse- 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
import json
HOST = '0.0.0.0'
PORT = 8080
server = Server(HOST, PORT)
@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=400)
uname = jData.get('username')
passwd = jData.get('password')
if uname is None or passwd is None:
return Response(code=400)
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=400)
authenticated = checkAuthentication(jData) # function defined elsewhere
if not authenticated:
return Response(code=403)
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=404)
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.0.2.tar.gz
(7.0 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
blite-1.0.2-py3-none-any.whl
(6.4 kB
view details)
File details
Details for the file blite-1.0.2.tar.gz.
File metadata
- Download URL: blite-1.0.2.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
483f787d6de314e7f1a2b38d69deeec0a9ebfc759130f4ba911aedba15009f12
|
|
| MD5 |
6104ad2f84be1d484e19fcc37ec81943
|
|
| BLAKE2b-256 |
347711967fb8f66d409fea3e8cedb35252a01e4c07f4b5cc5d94fbe12a41caa4
|
File details
Details for the file blite-1.0.2-py3-none-any.whl.
File metadata
- Download URL: blite-1.0.2-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0949cda8ef007faaaebb6b7e06c34a2d7b856bf45390b1e0d136750c1d8cbb20
|
|
| MD5 |
6c671220021bf13eccae9d38c2afa96f
|
|
| BLAKE2b-256 |
56712eaccd149df5af9e26cc079e64e358ceb3a41d6e32c342cb0de15d267d1b
|