Project

General

Profile

1
import os
2
from fastapi import FastAPI, Form
3
import logging
4
from starlette.staticfiles import StaticFiles
5
from starlette.responses import FileResponse
6
from eu.dnetlib.es_connector import DLIESConnector
7
import sys
8
from eu.dnetlib.metrics_utils import metrics
9
from eu.dnetlib.metricsMiddleware import PrometheusMiddleware
10

    
11
_CURDIR = os.path.dirname(os.path.abspath(__file__))
12

    
13
app = FastAPI()
14

    
15
app.mount('/static', StaticFiles(directory=os.path.join(_CURDIR, 'static' )))
16
base_dnet_url = "http://aggregator-dli.openaire.eu/dli/"
17
pid_resolver = {
18
    "pdb": "http://www.rcsb.org/pdb/explore/explore.do?structureId=%s",
19
    "ncbi-n": "http://www.ncbi.nlm.nih.gov/gquery/?term=%s",
20
    "pmid": "http://www.ncbi.nlm.nih.gov/pubmed/%s",
21
    "pmcid": "http://www.ncbi.nlm.nih.gov/pmc/articles/%s",
22
    "pubmedid": "http://www.ncbi.nlm.nih.gov/pubmed/%s",
23
    "doi": "http://dx.doi.org/%s",
24
    "genbank": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
25
    "nuccore": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
26
    "swiss-prot": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
27
    "arrayexpress": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
28
    "biomodels": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
29
    "bmrb": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
30
    "ena": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
31
    "geo": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
32
    "ensembl": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
33
    "mgi": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
34
    "bind": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
35
    "pride": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
36
    "ddbj": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
37
    "bioproject": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
38
    "embl": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
39
    "sra": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
40
}
41

    
42
log = logging.getLogger("scholexplorer-portal")
43
log.setLevel(logging.INFO)
44
fh = logging.StreamHandler(sys.stdout)
45
fh.setLevel(logging.INFO)
46
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
47
fh.setFormatter(formatter)
48
log.addHandler(fh)
49
log.info("Scholexplorer Portal Restarted")
50

    
51

    
52
app.add_middleware(PrometheusMiddleware)
53
app.add_route("/metrics/", metrics)
54

    
55
@app.get('/')
56
def root():
57
    return FileResponse(os.path.join(os.path.join(_CURDIR, 'static' ),'index.html'))
58

    
59
@app.get("/favicon.ico")
60
def favicon():
61
    return FileResponse(os.path.join(os.path.join(_CURDIR, 'static' ),'favicon.ico'))
62

    
63
@app.get("/api/main_page_stats")
64
def main_page_stats():
65
    connector= DLIESConnector()
66
    return dict(result=connector.get_main_page_stats())
67

    
68
@app.post('/api/queryPid/')
69
def query_pid(*, pid:str= Form(...)):
70
    connector = DLIESConnector()
71
    return dict(result=connector.query_by_id(pid))
72

    
73

    
74

    
75
@app.post('/api/post/')
76
def query_post(*, start:int= Form(default=0), action:str=Form(default="query"), query:str=Form(...),filter:str=Form(default=None)):
77
    print("SONO QUIIII")
78

    
79
    filter_key = None
80
    result = {}
81
    if filter:
82
        filter_key = filter
83
    if action == 'query' and query is not None:
84
        print(f"query is {query}")
85
        connector = DLIESConnector()
86
        try:
87
            result = connector.simple_query(query, start=start, user_filter=filter_key)
88
        except Exception as e:
89
            log.error(e)
90
        return dict(result=result)
91
    else:
92
        return dict(error={'error_code': 'an error occur during query on server'})
93

    
94

    
95
@app.get('/api/item/{identifier}')
96
@app.post('/api/item/{identifier}')
97
def get_item(identifier:str, object_type:str=Form(default=None), start:int=Form(default=0)):
98
    if identifier:
99
        connector = DLIESConnector()
100
        return dict(result=connector.item_by_id(identifier, object_type, start))
101
    else:
102
        return None
(3-3/4)