import time
from fastapi import FastAPI
import sys
import os
import datetime
import ast
from fastapi.responses import JSONResponse, FileResponse
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import asyncio
import time


app = FastAPI()

origins = [
    "http://www.googo.org",
    "http://www.googo.org:8080",
    "http://0.0.0.0:8080",
    "http://googo.org:8080",
    "http://googo.org",
    "http://139.162.121.30"
]
#uvicorn main:app --host 0.0.0.0 --port 8001
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

class q_req(BaseModel):
    domain_name: str

class kw_req(BaseModel):
    fullurl: str
    shorturl: str


def check_exists(shorturl):
    foldername='/etc/nginx/url'
    lst=os.listdir(foldername)
    for l in lst:
        txt=l.replace('.conf','')
        if txt==shorturl:
            return True
    return False

#    os.path.exists(foldername)
#    print(lst)
#/etc/nginx/url

@app.post("/urldata")
async def get_domain_by_keyword(req:kw_req):
    print(req)
    print(req.fullurl)
    print(req.shorturl)
#    check_exists(req.shorturl)
    val=check_exists('seo')
    print(val)
    return "OK"
    
