def start_server( workspace: "Workspace path (full or chrooted) to load", port: "port like URI to start the server at", CHROOT: "Path to Image Data", ): """ Start a SuRVoS API Server for requests. """ from hug.store import InMemoryStore from hug.middleware import SessionMiddleware, CORSMiddleware from survos2.model import DataModel full_ws_path = os.path.join(CHROOT, workspace) if not os.path.isdir(full_ws_path): logger.error(f"No workspace can be found at {full_ws_path}, aborting.") sys.exit(1) logger.info(f"Full workspace path is {full_ws_path}") DataModel.g.CHROOT = CHROOT DataModel.g.current_workspace = workspace api, __plugins = init_api(return_plugins=True) session_store = InMemoryStore() middleware = SessionMiddleware( session_store, cookie_name="survos_sid", cookie_secure=False, cookie_path="/" ) api.http.add_middleware(middleware) api.http.add_middleware(CORSMiddleware(api, max_age=10)) logger.debug(f"Starting server on port {port} with workspace {workspace}") api.http.serve(port=int(port), display_intro=False)
def test_cors_middleware(hug_api): hug_api.http.add_middleware(CORSMiddleware(hug_api, max_age=10)) @hug.get('/demo', api=hug_api) def get_demo(): return {'result': 'Hello World'} @hug.get('/demo/{param}', api=hug_api) def get_demo(param): return {'result': 'Hello {0}'.format(param)} @hug.post('/demo', api=hug_api) def post_demo(name: 'your name'): return {'result': 'Hello {0}'.format(name)} @hug.put('/demo/{param}', api=hug_api) def get_demo(param, name): old_name = param new_name = name return {'result': 'Goodbye {0} ... Hello {1}'.format(old_name, new_name)} @hug.delete('/demo/{param}', api=hug_api) def get_demo(param): return {'result': 'Goodbye {0}'.format(param)} assert hug.test.get(hug_api, '/demo').data == {'result': 'Hello World'} assert hug.test.get(hug_api, '/demo/Mir').data == {'result': 'Hello Mir'} assert hug.test.post(hug_api, '/demo', name='Mundo') assert hug.test.put(hug_api, '/demo/Carl', name='Junior').data == {'result': 'Goodbye Carl ... Hello Junior'} assert hug.test.delete(hug_api, '/demo/Cruel_World').data == {'result': 'Goodbye Cruel_World'} response = hug.test.options(hug_api, '/demo') methods = response.headers_dict['access-control-allow-methods'].replace(' ', '') allow = response.headers_dict['allow'].replace(' ', '') assert set(methods.split(',')) == set(['OPTIONS', 'GET', 'POST']) assert set(allow.split(',')) == set(['OPTIONS', 'GET', 'POST']) response = hug.test.options(hug_api, '/demo/1') methods = response.headers_dict['access-control-allow-methods'].replace(' ', '') allow = response.headers_dict['allow'].replace(' ', '') assert set(methods.split(',')) == set(['OPTIONS', 'GET', 'DELETE', 'PUT']) assert set(allow.split(',')) == set(['OPTIONS', 'GET', 'DELETE', 'PUT']) assert response.headers_dict['access-control-max-age'] == '10' response = hug.test.options(hug_api, '/v1/demo/1') methods = response.headers_dict['access-control-allow-methods'].replace(' ', '') allow = response.headers_dict['allow'].replace(' ', '') assert set(methods.split(',')) == set(['OPTIONS', 'GET', 'DELETE', 'PUT']) assert set(allow.split(',')) == set(['OPTIONS', 'GET', 'DELETE', 'PUT']) assert response.headers_dict['access-control-max-age'] == '10' response = hug.test.options(hug_api, '/v1/demo/123e4567-midlee89b-12d3-a456-426655440000') methods = response.headers_dict['access-control-allow-methods'].replace(' ', '') allow = response.headers_dict['allow'].replace(' ', '') assert set(methods.split(',')) == set(['OPTIONS', 'GET', 'DELETE', 'PUT']) assert set(allow.split(',')) == set(['OPTIONS', 'GET', 'DELETE', 'PUT']) assert response.headers_dict['access-control-max-age'] == '10'
import hug import json import base64 from random import random from falcon import HTTP_400, HTTP_200 from hug.middleware import CORSMiddleware import io api = hug.API(__name__) api.http.add_middleware(CORSMiddleware(api)) def writeFile(data): f = open('files/' + str(random()) + '.png', 'w+b') binary_format = bytearray(data) f.write(binary_format) f.close() @hug.startup() def add_data(api): """Adds initial data to the api on startup""" print("It's working") @hug.post('/upload') def main(request, body, response, debug=True): print("Bwah", response) print(type(body)) print(len(body))
def test_cors_middleware(hug_api): hug_api.http.add_middleware(CORSMiddleware(hug_api, max_age=10)) @hug.get("/demo", api=hug_api) def get_demo(): return {"result": "Hello World"} @hug.get("/demo/{param}", api=hug_api) def get_demo(param): return {"result": "Hello {0}".format(param)} @hug.post("/demo", api=hug_api) def post_demo(name: "your name"): return {"result": "Hello {0}".format(name)} @hug.put("/demo/{param}", api=hug_api) def get_demo(param, name): old_name = param new_name = name return { "result": "Goodbye {0} ... Hello {1}".format(old_name, new_name) } @hug.delete("/demo/{param}", api=hug_api) def get_demo(param): return {"result": "Goodbye {0}".format(param)} assert hug.test.get(hug_api, "/demo").data == {"result": "Hello World"} assert hug.test.get(hug_api, "/demo/Mir").data == {"result": "Hello Mir"} assert hug.test.post(hug_api, "/demo", name="Mundo") assert hug.test.put(hug_api, "/demo/Carl", name="Junior").data == { "result": "Goodbye Carl ... Hello Junior" } assert hug.test.delete(hug_api, "/demo/Cruel_World").data == { "result": "Goodbye Cruel_World" } response = hug.test.options(hug_api, "/demo") methods = response.headers_dict["access-control-allow-methods"].replace( " ", "") allow = response.headers_dict["allow"].replace(" ", "") assert set(methods.split(",")) == set(["OPTIONS", "GET", "POST"]) assert set(allow.split(",")) == set(["OPTIONS", "GET", "POST"]) response = hug.test.options(hug_api, "/demo/1") methods = response.headers_dict["access-control-allow-methods"].replace( " ", "") allow = response.headers_dict["allow"].replace(" ", "") assert set(methods.split(",")) == set(["OPTIONS", "GET", "DELETE", "PUT"]) assert set(allow.split(",")) == set(["OPTIONS", "GET", "DELETE", "PUT"]) assert response.headers_dict["access-control-max-age"] == "10" response = hug.test.options(hug_api, "/v1/demo/1") methods = response.headers_dict["access-control-allow-methods"].replace( " ", "") allow = response.headers_dict["allow"].replace(" ", "") assert set(methods.split(",")) == set(["OPTIONS", "GET", "DELETE", "PUT"]) assert set(allow.split(",")) == set(["OPTIONS", "GET", "DELETE", "PUT"]) assert response.headers_dict["access-control-max-age"] == "10" response = hug.test.options( hug_api, "/v1/demo/123e4567-midlee89b-12d3-a456-426655440000") methods = response.headers_dict["access-control-allow-methods"].replace( " ", "") allow = response.headers_dict["allow"].replace(" ", "") assert set(methods.split(",")) == set(["OPTIONS", "GET", "DELETE", "PUT"]) assert set(allow.split(",")) == set(["OPTIONS", "GET", "DELETE", "PUT"]) assert response.headers_dict["access-control-max-age"] == "10"
import hug from hug.middleware import CORSMiddleware # middleware to accept localhost api = hug.API(__name__) api.http.add_middleware( CORSMiddleware(api, allow_origins=["http://*****:*****@hug.post('/test') def test(body): scoreText = 0 scoreNumeric = 0 scoreBinary = 0 points = 20 if body['usPresident'] == 'DONALD TRUMP': scoreText += points if body['longestRiver'] == 'NILE' or body['longestRiver'] == 'NILE RIVER': scoreText += points if body['largestCountry'] == 'RUSSIA': scoreText += points if body['southafrica'] == 'ZAR': scoreText += points if int(body['multiply']) == 12: scoreNumeric += points if int(body['multiply2']) == 10: scoreNumeric += points if int(body['largestNumber']) == 19: scoreNumeric += points if int(body['primenumber']) == 97:
import threading import time import hug from hug.middleware import CORSMiddleware from logzero import logger from Rimoto_plex_companion.Model.selections import list_unscanned from Rimoto_plex_companion.Model.selections import list_recently_scanned from Rimoto_plex_companion.Model.selections import add_to_queue from Rimoto_plex_companion.Model.selections import manual_import from Rimoto_plex_companion.Model.selections import scan_all from Rimoto_plex_companion.Model.selections import delete_from_queue API = hug.API(__name__) API.http.add_middleware(CORSMiddleware(API)) ROUTER = hug.route.API(__name__) logger.info('Starting rimoto backend server') def start_tasks(): """""" next_call = time.time() while True: scan_all() try: next_call += 10 time.sleep(next_call - time.time()) except ValueError: time.sleep(10) next_call = time.time()
from similarity_search import get_similar_faces detection_total = 0 embed_total = 0 if os.getenv("HUG_DEBUG", "0") == "1": class CustomLogMiddleware(LogMiddleware): def process_request(self, request, response): pass api = hug.API(__name__) api.http.add_middleware( CORSMiddleware( api, allow_origins=[ "http://*****:*****@hug.get("/info") def info(): return {