https://github.com/linuxwhatelse/mjs
A JSON-Server component which uses the mapper project
https://github.com/linuxwhatelse/mjs
Last synced: 2 months ago
JSON representation
A JSON-Server component which uses the mapper project
- Host: GitHub
- URL: https://github.com/linuxwhatelse/mjs
- Owner: linuxwhatelse
- License: gpl-3.0
- Created: 2016-03-25T15:27:44.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2018-04-19T09:08:31.000Z (about 7 years ago)
- Last Synced: 2025-02-23T01:48:53.249Z (3 months ago)
- Language: Python
- Homepage:
- Size: 25.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
mjs - Mapper JSON Server
========================
[](https://pypi.python.org/pypi/lwe-mjs)**mjs** is a simple server component which uses the [mapper project](https://github.com/linuxwhatelse/mapper) to run functions mapped to url patterns.
## Requirements
1. python 3.4 and up
2. [mapper.py](https://github.com/linuxwhatelse/mapper)## Installation
Just run:
```bash
pip install lwe-mjs
```## Usage
It's pretty straight forward...
```python
import mjs
import mappermpr = mapper.Mapper.get()
def validator(path, headers):
# Would verify if the request is allowed or not
return True# Map "/index" to the index function.
#
# To see how the mapper works and what it can do, head over to
# https://github.com/linuxwhatelse/mapper
@mpr.url('^/index/$', 'GET')
def index():
# When using mjs, your functions response HAS to look like this!
return {
# (Required) Status-Code to be sent to the client
'status_code' : 200,# (Optional) Alternative message to be sent to the client
'message' : 'Success',# (Optional) A cookie to be sent to the client.
# e.g. http.cookies.SimpleCookie()
'cookie' : None,# (Optional) Payload/Body to be sent to the client
# Has to be of type dict or list
'payload' : None
}if __name__ == '__main__':
# Create a default configuration
conf = mjs.Config()# Who is allowed to access the server
conf.address = '0.0.0.0'# The port to be used with the server
conf.port = 8088# A callback to be called for EVERY request (GET, POST, PUT, DELETE)
# BEFORE the resolved function will be run.
#
# This callback HAS to return either True (if the request is allowed), or
# False (if the request should be denied)
#
# Usefully to implement e.g. authentication
conf.validate_callback = validator# A list of url-paths (without host:port) to be excluded from validation.
conf.validate_exclude_paths = ['/login', '/register']# Create a new server instance
server = mjs.Server(conf)
# or
server = mjs.ThreadedServer(conf)# Start the server
print('Server running: %s:%s' % (conf.address, conf.port))
print('use ctrl+c to exit')
try:
server.serve_forever()except KeyboardInterrupt:
print('Server closed...')
```