@@ -27,9 +27,21 @@ | |||||
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging> | <EnableUnmanagedDebugging>false</EnableUnmanagedDebugging> | ||||
</PropertyGroup> | </PropertyGroup> | ||||
<ItemGroup> | <ItemGroup> | ||||
<Compile Include="api.py"> | |||||
<SubType>Code</SubType> | |||||
</Compile> | |||||
<Compile Include="constants.py"> | |||||
<SubType>Code</SubType> | |||||
</Compile> | |||||
<Compile Include="main.py"> | <Compile Include="main.py"> | ||||
<SubType>Code</SubType> | <SubType>Code</SubType> | ||||
</Compile> | </Compile> | ||||
<Compile Include="middlewares.py"> | |||||
<SubType>Code</SubType> | |||||
</Compile> | |||||
<Compile Include="resources.py"> | |||||
<SubType>Code</SubType> | |||||
</Compile> | |||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | <ItemGroup> | ||||
<InterpreterReference Include="Global|PythonCore|3.6" /> | <InterpreterReference Include="Global|PythonCore|3.6" /> | ||||
@@ -0,0 +1,16 @@ | |||||
import falcon | |||||
import middlewares | |||||
import resources | |||||
import constants | |||||
#from constants import * | |||||
def create(): | |||||
api = falcon.API(media_type=constants.MEDIA_CSV, middleware=[ | |||||
middlewares.AuthMiddleware(constants.USER_AGENT), | |||||
middlewares.RequireCSV() | |||||
]) | |||||
api.add_route(constants.ROUTES_POSITIONS, resources.PositionsResource()) | |||||
return api |
@@ -0,0 +1,4 @@ | |||||
USER_AGENT = 'SIMCOM_MODULE' | |||||
MEDIA_CSV = 'text/csv' | |||||
ROUTES_POSITIONS = '/positions' |
@@ -2,34 +2,12 @@ | |||||
Basic HTTP server to receive and save gps positions sent by Arduino GPS Tracker | Basic HTTP server to receive and save gps positions sent by Arduino GPS Tracker | ||||
""" | """ | ||||
import falcon | |||||
class RequireCSV: | |||||
def process_request(self, req, resp): | |||||
if req.method in ('POST', 'PUT'): | |||||
if 'text/csv' not in req.content_type: | |||||
raise falcon.HTTPUnsupportedMediaType('Only CSV is supported') | |||||
class PositionsResource: | |||||
def on_post(self, req, resp): | |||||
data = req.bounded_stream.read() | |||||
resp.body = data | |||||
resp.status = falcon.HTTP_201 | |||||
positions = PositionsResource() | |||||
api = falcon.API(middleware=[ | |||||
RequireCSV() | |||||
]) | |||||
api.add_route('/positions', positions) | |||||
import api | |||||
if __name__ == "__main__": | if __name__ == "__main__": | ||||
# Use python built-in WSGI reference implemention to run a web server | # Use python built-in WSGI reference implemention to run a web server | ||||
from wsgiref.simple_server import make_server | from wsgiref.simple_server import make_server | ||||
print('Starting web server...') | print('Starting web server...') | ||||
srv = make_server('localhost', 8080, api) | |||||
srv.serve_forever() | |||||
srv = make_server('localhost', 8080, api.create()) | |||||
srv.serve_forever() |
@@ -0,0 +1,24 @@ | |||||
import falcon | |||||
import constants | |||||
class RequireCSV(object): | |||||
def process_request(self, req, resp): | |||||
if req.method in ('POST', 'PUT'): | |||||
if constants.MEDIA_CSV not in req.content_type: | |||||
raise falcon.HTTPUnsupportedMediaType | |||||
class AuthMiddleware(object): | |||||
def __init__(self, userAgentRequired): | |||||
self._userAgentRequired = userAgentRequired | |||||
def process_request(self, req, resp): | |||||
authorization = req.get_header('Authorization') | |||||
userAgent = req.get_header('User-Agent') | |||||
if userAgent != self._userAgentRequired: | |||||
if authorization is None: #make clients believe that Authorization header is the issue | |||||
raise falcon.HTTPForbidden | |||||
else: | |||||
raise falcon.HTTPUnauthorized |
@@ -0,0 +1,11 @@ | |||||
import falcon | |||||
class PositionsResource: | |||||
def on_post(self, req, resp): | |||||
data = req.bounded_stream.read() | |||||
resp.body = data | |||||
resp.status = falcon.HTTP_201 | |||||
def on_get(self, req, resp): | |||||
raise falcon.HTTPNotImplemented |