This commit is contained in:
mikolajpaterka 2023-01-28 20:18:57 +01:00
parent c4b749ce8a
commit bd24881ac2
5 changed files with 128 additions and 0 deletions

5
5/credencials_aws.py Normal file
View File

@ -0,0 +1,5 @@
aws_access_key_id=''
aws_secret_access_key=''
aws_session_token=''
DEFAULT_VPC = ''

71
5/main.py Normal file
View File

@ -0,0 +1,71 @@
from credencials_aws import aws_access_key_id, aws_secret_access_key, aws_session_token, DEFAULT_VPC
import boto3, time
INDEKS = "s444455"
key_name = f"{INDEKS}-key"
security_group_name = f"{INDEKS}-security-group"
user_data = f'''
#!/bin/bash
sudo yum update -y
sudo yum install git -y
git clone https://git.wmi.amu.edu.pl/s444455/DPZC_3.git
cd DPZC_3/5/web
sudo yum install docker -y
sudo service docker start
sudo usermod -a -G docker ec2-user
sudo docker build -t invoice .
sudo docker run -d -p 80:8080 -t invoice
'''
if __name__ == '__main__':
ec2 = boto3.resource(
'ec2',
region_name='us-east-1',
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
aws_session_token=aws_session_token,
)
key_pair = ec2.create_key_pair(
KeyName=key_name,
KeyType='ed25519',
KeyFormat='pem',
)
security_group = ec2.create_security_group(
Description=security_group_name,
GroupName=security_group_name,
VpcId=DEFAULT_VPC,
)
inbound_rules = security_group.authorize_ingress(
GroupId=security_group.group_id,
CidrIp='0.0.0.0/0',
IpProtocol='tcp',
FromPort=80,
ToPort=80,
)
instance = ec2.create_instances(
ImageId='ami-0b5eea76982371e91',
MinCount=1,
MaxCount=1,
InstanceType='t2.micro',
KeyName=key_pair.name,
UserData=user_data,
SecurityGroups=[security_group.group_name],
)
while True:
time.sleep(1)
instance = ec2.Instance(instance.id)
if instance.state['Code'] == 16:
break

7
5/web/Dockerfile Normal file
View File

@ -0,0 +1,7 @@
FROM python:3.8
COPY api.py requirements.txt ./
RUN apt-get update
RUN apt-get install -y default-jdk
RUN pip install -r requirements.txt
EXPOSE 80:8000/tcp
CMD python api.py

42
5/web/api.py Normal file
View File

@ -0,0 +1,42 @@
from fastapi import FastAPI, UploadFile
from tika import parser
import uvicorn, re
from fastapi.middleware.cors import CORSMiddleware
def daj_dane(plik):
faktura = parser.from_buffer(plik)
dane = faktura['content']
dane = dane.split('\n')
dane = [i for i in dane if i != '']
razem = list(filter(lambda x: 'Razem' in x, dane))
razem = float(re.findall(r'\d+[.]\d+', razem[0])[-1])
sprzedawca = dane[dane.index('Sprzedawca:') : dane.index('Nabywca:')]
sprzedawca_nazwa = sprzedawca[1]
nip = sprzedawca[-1].replace('NIP: ', '')
return {
'vat_id' : nip,
'seller' : sprzedawca_nazwa,
'total' : razem
}
app = FastAPI()
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post('/invoice')
async def root(file: UploadFile):
return daj_dane(file.file.read())
if __name__ == '__main__':
uvicorn.run(app, host="0.0.0.0", port=8000)

3
5/web/requirements.txt Normal file
View File

@ -0,0 +1,3 @@
tika
uvicorn
fastapi