Ejemplo n.º 1
0
def main_http(
        http_webdir: str = config.http_webdir,
        socket_host: str = config.http_socket_host,
        socket_port: int = config.http_socket_port,
        ert_profile_ctl: EthearnalProfileController = None,
        # ert_profile_view: EthearnalProfileView = None,
        files_dir_name: str = config.static_files,
        interactive: bool = config.interactive,
        dht_=None,
        dht_facade_=None):

    site_conf = {
        '/': {
            'tools.sessions.on': True,
            'tools.staticdir.root': os.path.abspath(os.getcwd())
        },
        '/ui': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': http_webdir,
            'tools.staticdir.index': 'index.html',
        },
        '/ui/network': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': http_webdir,
            'tools.staticdir.index': 'network.html',
        },
        '/ui/profile': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': http_webdir,
            'tools.staticdir.index': 'profile.html',
        },
        '/ui/profiles': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': http_webdir,
            'tools.staticdir.index': 'profiles.html',
        },
        # '/ui/files': {
        #     'tools.staticdir.on': True,
        #     'tools.staticdir.root': ert_profile_ctl.data_dir,
        #     'tools.staticdir.dir': files_dir_name,
        # },
        '/api/ui': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': 'apidef/swagger',
            'tools.staticdir.index': 'index.html',
        }
    }

    cherrypy.server.socket_host = socket_host
    cherrypy.server.socket_port = socket_port
    # Cache-Control:public, max-age=5 # in seconds
    cherrypy.response.headers['Cache-Control'] = 'public, max-age=5'

    cherrypy.tree.mount(EthearnalSite(), '/', site_conf)
    cherrypy.config.update({
        'global': {
            'engine.autoreload.on': False,
            'server.thread_pool': 120,
        }
    })
    hfs_dir = '%s/%s' % (ert.data_dir, config.hfs_dir)
    mkdir(hfs_dir)
    print('HFS_DIR: %s' % hfs_dir)

    idx = Indexer(ert=ert, dhf=dht_facade_)

    # select and save CDN service
    hfs_guids = AutoDirHfs(hfs_dir, 'guids_hfs')
    dkv = DhtKv(d)
    dkv.set(WebDhtCdnSelector.K_SELECTED_CDN,
            "%s:%s" % (ert.cdn_host, ert.cdn_port))
    cdn_select = WebDhtCdnSelector(dkv)

    from webfacades.dht_kv import WebCdnClusterTrackerClient, WebCdnClusterTracker

    wtrack_cli = WebCdnClusterTrackerClient(
        dhf=d, http_host_port=config.ert_default_tracker)

    cdn_list = WebDhtCdnList(dkv=dkv, wtrack_cli=wtrack_cli)

    knownguids = WebDHTKnownGuids(cherry=cherrypy,
                                  dhtf=dht_facade_,
                                  dkv=dkv,
                                  hfs=hfs_guids,
                                  mount_point='/api/v1/dht/guids')

    dht_get_hk = DhtGetByHkeyWebAPI(
        cherry=cherrypy,
        dhf=dht_facade_,
    )

    dht_gigs_hk = DhtGigsHkeysWebAPI(cherry=cherrypy,
                                     dhf=dht_facade_,
                                     me_owner=OwnerGuidHashIO(
                                         ert_profile_ctl.rsa_guid_hex))
    # don't delete this
    # events = DhtEventsHkeysWebAPI(
    #     cherry=cherrypy,
    #     dhf=dht_facade_,
    #     me_owner=OwnerGuidHashIO(ert_profile_ctl.rsa_guid_hex),
    # )
    #
    # dht_portfolios_hk = DhtPortfoliosWebAPI(
    #     cherry=cherrypy,
    #     dhf=dht_facade_,
    #     me_owner=OwnerGuidHashIO(ert_profile_ctl.rsa_guid_hex)
    # )

    dht_ip4 = WebDHTKnownPeers(
        cherry=cherrypy,
        dhf=dht_facade_,
    )

    dht_profile = WebDHTProfileKeyVal(
        cherry=cherrypy,
        dhf=dht_facade_,
    )

    dht_node = WebDHTAboutNode(
        cherry=cherrypy,
        dhf=dht_facade_,
    )
    from webfacades.dht_peers import WebDhtPeers

    rel_urls = None
    if args.http_config_url:
        from toolkit.tools import get_http_peers_from_http_tracker
        from toolkit.tools import boot_peers_from_http_tracker
        # url = 'http://159.65.56.140:8080/cluster.json'
        relays = get_http_peers_from_http_tracker(args.http_config_url)
        rel_urls = [
            'http://%s/api/cdn/v1/resource' % k for k in relays if ip not in k
        ]
        # self.relays = set(rurl)
        boot_peers_from_http_tracker(d, args.http_config_url)

    peers = PeersInfo(dhf=d,
                      geo=FsCachedGeoIp(AutoDirHfs(hfs_dir, 'geo_hfs')),
                      hfs=AutoDirHfs(hfs_dir, 'peers_hfs'))

    web_peers = WebDhtPeers(peers=peers, cherry=cherrypy)

    from webdht.bundle import DocumentCollectionCRD, DHTEventHandler, DocModelIndexQuery
    # from webdht.test_bndle_gig import Gigs as test_gig_data

    # nes bundle
    c = DocumentCollectionCRD(
        'Event',
        dhf=dht_facade_,
        own_guid_hex=ert.rsa_guid_hex,
        key_composer=None,
    )
    # from apifacades.events import SelfEvent
    # se = SelfEvent(c)

    # evt = DHTEventHandler(dht_facade_.dht.storage, data_dir=ert_profile_ctl.personal_dir)
    # todo add support for instance id db files
    # qidx = DocModelIndexQuery(evt.doc_indexers.MODEL_INDEXERS['.Gig.model'])
    # eidx = DocModelIndexQuery(evt.doc_indexers.MODEL_INDEXERS['.Event.model'])
    # qpro = DocModelIndexQuery(evt.doc_indexers.MODEL_INDEXERS['.Profile.key'])

    cherrypy.engine.start()

    print('WEBUI DIR:', http_webdir)
    print('PROFILE DIR:', ert_profile_ctl.data_dir)

    cherrypy.engine.exit = on_hook(target=tear_down_udp,
                                   target_args=(dht_, ),
                                   target_kwargs={})(cherrypy.engine.exit)

    # import asyncio
    # import datetime
    # import random
    # import websockets
    # try:
    #     async def time(websocket, path):
    #         while True:
    #             now = datetime.datetime.utcnow().isoformat() + 'Z'
    #             await websocket.send(now)
    #             await asyncio.sleep(random.random() * 3)
    #
    #     start_server = websockets.serve(time, '127.0.0.1', 6789)
    #     asyncio.get_event_loop().run_until_complete(start_server)
    #     asyncio.get_event_loop().run_forever()
    # except Exception as e:
    #     print(str(e))
    #     sys.exit()

    if not interactive:
        cherrypy.engine.block()
    else:
        try:
            from IPython import embed
            embed()
        except:
            pass
Ejemplo n.º 2
0
def main_http(
        http_webdir: str = config.http_webdir,
        socket_host: str = config.http_socket_host,
        socket_port: int = config.http_socket_port,
        ert_profile_ctl: EthearnalProfileController = None,
        # ert_profile_view: EthearnalProfileView = None,
        files_dir_name: str = config.static_files,
        interactive: bool = config.interactive,
        dht_=None,
        dht_facade_=None):

    site_conf = {
        '/': {
            'tools.sessions.on': True,
            'tools.staticdir.root': os.path.abspath(os.getcwd())
        },
        '/ui': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': http_webdir,
            'tools.staticdir.index': 'index.html',
        },
        '/ui/files': {
            'tools.staticdir.on': True,
            'tools.staticdir.root': ert_profile_ctl.data_dir,
            'tools.staticdir.dir': files_dir_name,
        },
        '/api/ui': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': 'apidef/swagger',
            'tools.staticdir.index': 'index.html',
        }
    }

    cherrypy.server.socket_host = socket_host
    cherrypy.server.socket_port = socket_port
    # Cache-Control:public, max-age=5 # in seconds
    cherrypy.response.headers['Cache-Control'] = 'public, max-age=5'

    cherrypy.tree.mount(EthearnalSite(), '/', site_conf)
    #
    # cherrypy.tree.mount(ert_profile_view,
    #                     '/api/v1/profile',
    #                     {'/': {'request.dispatch': cherrypy.dispatch.MethodDispatcher()}}
    #                     )
    # #
    # cherrypy.tree.mount(EthearnalJobView(EthearnalJobPostController(ert_profile_ctl)),
    #                     '/api/v1/job', {'/': {
    #                         'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
    #                         'tools.sessions.on': True,
    #                         'tools.response_headers.on': True,
    #                         'tools.response_headers.headers': [('Content-Type', 'text/plain')],
    #                         }
    #                      }
    #                     )
    #
    # cherrypy.tree.mount(EthearnalUploadFileView(ert_profile_ctl),
    #                     '/api/v1/upload', {'/': {
    #                         'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
    #                         'tools.sessions.on': True,
    #                         'tools.response_headers.on': True,
    #                         'tools.response_headers.headers': [('Content-Type', 'text/plain')],
    #                         }
    #                      }
    #                     )
    #
    # cherrypy.tree.mount(EthearnalUploadJsonView(ert_profile_ctl),
    #                     '/api/v1/uploadjson', {'/': {
    #                         'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
    #                         'tools.sessions.on': True,
    #                         'tools.response_headers.on': True,
    #                         'tools.response_headers.headers': [('Content-Type', 'text/plain')],
    #                         }
    #                      }
    #                     )

    cherrypy.config.update({'global': {'engine.autoreload.on': False}})
    #

    # webdht = WebDHTPulse(
    #     cherry=cherrypy,
    #     dht_pulse=DHTPulse(dht_facade_),
    #     mount_point='/api/v1/dhtpulse',
    #     mount_it=True,
    # )
    # websys = WebSysGuidApi(
    #     cherry=cherrypy,
    #     dht_pulse=DHTPulse(dht_facade_),
    #     owner=OwnerGuidHashIO(ert_profile_ctl.rsa_guid_hex)
    # )
    #
    # webself = WebSelfPredicateApi(
    #     cherry=cherrypy,
    #     dht_pulse=DHTPulse(dht_facade_),
    #     owner=OwnerGuidHashIO(ert_profile_ctl.rsa_guid_hex),
    # )
    # webguid = WebGuidPredicateApi(
    #     cherry=cherrypy,
    #     dht_pulse=DHTPulse(dht_facade_),
    # )
    #
    # knownguids = WebDHTKnownGuids(
    #     cherry=cherrypy,
    #     dhtf=dht_facade_,
    #     mount_point='/api/v1/dht/guids'
    # )

    # from webdht.wdht_ertapi import DhtGigsWebAPI

    # dht_gigs = DhtGigsWebAPI(
    #     cherry=cherrypy,
    #     dhf=dht_facade_,
    #     me_owner=OwnerGuidHashIO(ert_profile_ctl.rsa_guid_hex)
    # )

    idx = Indexer(ert=ert, dhf=dht_facade_)

    knownguids = WebDHTKnownGuids(cherry=cherrypy,
                                  dhtf=dht_facade_,
                                  mount_point='/api/v1/dht/guids')

    dht_get_hk = DhtGetByHkeyWebAPI(
        cherry=cherrypy,
        dhf=dht_facade_,
    )

    dht_gigs_hk = DhtGigsHkeysWebAPI(cherry=cherrypy,
                                     dhf=dht_facade_,
                                     me_owner=OwnerGuidHashIO(
                                         ert_profile_ctl.rsa_guid_hex))

    events = DhtEventsHkeysWebAPI(
        cherry=cherrypy,
        dhf=dht_facade_,
        me_owner=OwnerGuidHashIO(ert_profile_ctl.rsa_guid_hex),
    )

    dht_portfolios_hk = DhtPortfoliosWebAPI(cherry=cherrypy,
                                            dhf=dht_facade_,
                                            me_owner=OwnerGuidHashIO(
                                                ert_profile_ctl.rsa_guid_hex))

    # global_events = DhtGlobalEventsHkeysWebAPI(
    #     cherry=cherrypy,
    #     dhf=dht_facade_,
    #     me_owner=OwnerGuidHashIO(ert_profile_ctl.rsa_guid_hex)
    # )

    # dht_events_hk = DhtEventsHkeysWebAPI(
    #     cherry=cherrypy,
    #     dhf=dht_facade_,
    #     me_owner=OwnerGuidHashIO(ert_profile_ctl.rsa_guid_hex)
    # )

    dht_ip4 = WebDHTKnownPeers(
        cherry=cherrypy,
        dhf=dht_facade_,
    )

    dht_profile = WebDHTProfileKeyVal(
        cherry=cherrypy,
        dhf=dht_facade_,
    )

    dht_node = WebDHTAboutNode(
        cherry=cherrypy,
        dhf=dht_facade_,
    )

    # WebGuidPredicateApi
    # WebSelfPredicateApi
    cherrypy.engine.start()

    print('WEBUI DIR:', http_webdir)
    print('PROFILE DIR:', ert_profile_ctl.data_dir)

    cherrypy.engine.exit = on_hook(target=tear_down_udp,
                                   target_args=(dht_, ),
                                   target_kwargs={})(cherrypy.engine.exit)

    if not interactive:
        cherrypy.engine.block()
    else:
        try:
            from IPython import embed
            embed()
        except:
            pass
Ejemplo n.º 3
0
        cherrypy.response.headers['Access-Control-Allow-Methods'] = 'POST GET'
        cherrypy.response.headers[
            'Access-Control-Allow-Headers'] = 'content-type'
        cherrypy.response.headers['Access-Control-Allow-Origin'] = '*'
        # tell CherryPy no avoid normal handler
        return True
    else:
        cherrypy.response.headers['Access-Control-Allow-Origin'] = '*'


cherrypy.tools.cors = cherrypy._cptools.HandlerTool(cors)

# cherrypy.tools.CORS = cherrypy.Tool('before_handler', cors)

cherrypy.engine.exit = on_hook(target=tear_down_udp,
                               target_args=(dht, ),
                               target_kwargs={})(cherrypy.engine.exit)
ip = None
if ert.my_lan_ip:
    ip = ert.my_lan_ip
if ert.my_wan_ip:
    ip = ert.my_wan_ip
if not ip:
    raise Exception('NO LAN OR WAN IP')
ert.cdn_service_http_url = 'http://%s:%s' % (ip, port)

if dht.server_thread.is_alive():
    print('UDP server thread is alive')
else:
    print('UDP server thread id dead')
Ejemplo n.º 4
0
def main_http(
        http_webdir: str = config.http_webdir,
        socket_host: str = config.http_socket_host,
        socket_port: int = config.http_socket_port,
        ert_profile_ctl: EthearnalProfileController = None,
        # ert_profile_view: EthearnalProfileView = None,
        files_dir_name: str = config.static_files,
        interactive: bool = config.interactive,
        dht_=None,
        dht_facade_=None):

    site_conf = {
        '/': {
            'tools.sessions.on': True,
            'tools.staticdir.root': os.path.abspath(os.getcwd())
        },
        '/ui': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': http_webdir,
            'tools.staticdir.index': 'index.html',
        },
        '/ui/profile': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': http_webdir,
            'tools.staticdir.index': 'profile.html',
        },
        '/ui/profiles': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': http_webdir,
            'tools.staticdir.index': 'profiles.html',
        },
        '/ui/files': {
            'tools.staticdir.on': True,
            'tools.staticdir.root': ert_profile_ctl.data_dir,
            'tools.staticdir.dir': files_dir_name,
        },
        '/api/ui': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': 'apidef/swagger',
            'tools.staticdir.index': 'index.html',
        }
    }

    cherrypy.server.socket_host = socket_host
    cherrypy.server.socket_port = socket_port
    # Cache-Control:public, max-age=5 # in seconds
    cherrypy.response.headers['Cache-Control'] = 'public, max-age=5'

    cherrypy.tree.mount(EthearnalSite(), '/', site_conf)
    cherrypy.config.update({
        'global': {
            'engine.autoreload.on': False,
            'server.thread_pool': 120,
        }
    })

    idx = Indexer(ert=ert, dhf=dht_facade_)

    knownguids = WebDHTKnownGuids(cherry=cherrypy,
                                  dhtf=dht_facade_,
                                  mount_point='/api/v1/dht/guids')

    dht_get_hk = DhtGetByHkeyWebAPI(
        cherry=cherrypy,
        dhf=dht_facade_,
    )

    dht_gigs_hk = DhtGigsHkeysWebAPI(cherry=cherrypy,
                                     dhf=dht_facade_,
                                     me_owner=OwnerGuidHashIO(
                                         ert_profile_ctl.rsa_guid_hex))
    # don't delete this
    # events = DhtEventsHkeysWebAPI(
    #     cherry=cherrypy,
    #     dhf=dht_facade_,
    #     me_owner=OwnerGuidHashIO(ert_profile_ctl.rsa_guid_hex),
    # )
    #
    # dht_portfolios_hk = DhtPortfoliosWebAPI(
    #     cherry=cherrypy,
    #     dhf=dht_facade_,
    #     me_owner=OwnerGuidHashIO(ert_profile_ctl.rsa_guid_hex)
    # )

    dht_ip4 = WebDHTKnownPeers(
        cherry=cherrypy,
        dhf=dht_facade_,
    )

    dht_profile = WebDHTProfileKeyVal(
        cherry=cherrypy,
        dhf=dht_facade_,
    )

    dht_node = WebDHTAboutNode(
        cherry=cherrypy,
        dhf=dht_facade_,
    )

    from webdht.bundle import DocumentCollectionCRD, DHTEventHandler, DocModelIndexQuery
    from webdht.test_bndle_gig import Gigs as test_gig_data

    # nes bundle
    c = DocumentCollectionCRD(
        'Gig',
        dhf=dht_facade_,
        own_guid_hex=ert.rsa_guid_hex,
        key_composer=None,
    )

    evt = DHTEventHandler(dht_facade_.dht.storage,
                          data_dir=ert_profile_ctl.personal_dir)
    qidx = DocModelIndexQuery(evt.doc_indexers.MODEL_INDEXERS['.Gig.model'])

    #

    web_profile_static = WebProfileStatic(
        cherry=cherrypy,
        web_root_dir=http_webdir,
        web_route_name='profilexxx',
        file_name='profile.html',
    )

    # WebGuidPredicateApi
    # WebSelfPredicateApi

    cherrypy.engine.start()

    print('WEBUI DIR:', http_webdir)
    print('PROFILE DIR:', ert_profile_ctl.data_dir)

    cherrypy.engine.exit = on_hook(target=tear_down_udp,
                                   target_args=(dht_, ),
                                   target_kwargs={})(cherrypy.engine.exit)

    # import asyncio
    # import datetime
    # import random
    # import websockets
    # try:
    #     async def time(websocket, path):
    #         while True:
    #             now = datetime.datetime.utcnow().isoformat() + 'Z'
    #             await websocket.send(now)
    #             await asyncio.sleep(random.random() * 3)
    #
    #     start_server = websockets.serve(time, '127.0.0.1', 6789)
    #     asyncio.get_event_loop().run_until_complete(start_server)
    #     asyncio.get_event_loop().run_forever()
    # except Exception as e:
    #     print(str(e))
    #     sys.exit()

    if not interactive:
        cherrypy.engine.block()
    else:
        try:
            from IPython import embed
            embed()
        except:
            pass