This commit is contained in:
Aleksandra Jonas 2023-01-27 21:36:03 +01:00
commit 1432bab5e1
9 changed files with 155 additions and 0 deletions

9
Dockerfile Normal file
View File

@ -0,0 +1,9 @@
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

2
README.md Normal file
View File

@ -0,0 +1,2 @@
# SaaS 5.2/5.3

39
api.py Normal file
View File

@ -0,0 +1,39 @@
from fastapi import FastAPI, UploadFile
from tika import parser
import uvicorn, re
from fastapi.middleware.cors import CORSMiddleware
def parse(pdf):
content = parser.from_buffer(pdf)
content = content['content'].split('\n')
content = [c for c in content if c != '']
total = float(re.findall(r'\d+[.]\d+', list(filter(lambda x: 'Razem' in x, content))[0])[-1])
content = content[content.index('Sprzedawca:') : content.index('Nabywca:')]
seller = content[1]
vat_id = content[-1].replace('NIP: ', '')
return {
'vat_id' : vat_id,
'seller' : seller,
'total' : total
}
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 parse(file.file.read())
if __name__ == '__main__':
uvicorn.run(app, host="0.0.0.0", port=8000)

5
credentials_template.py Normal file
View File

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

49
main.py Normal file
View File

@ -0,0 +1,49 @@
import boto3
from user_data import user_data
from credentials import (
aws_access_key_id,
aws_secret_access_key,
aws_session_token,
default_vpc
)
PREFIX = 'sassy'
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=PREFIX + '-key',
KeyType='ed25519',
KeyFormat='pem',
)
security_group = ec2.create_security_group(
Description=PREFIX + '-group',
GroupName=PREFIX + '-group',
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,
)
instances = 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],
)

4
requirements.txt Normal file
View File

@ -0,0 +1,4 @@
fastapi
tika
uvicorn
python-multipart

12
user_data.py Normal file
View File

@ -0,0 +1,12 @@
user_data = '''
#!/bin/bash
sudo yum update -y
sudo yum install git -y
git clone https://git.wmi.amu.edu.pl/s444391/saas-chmurki.git
cd saas-chmurki
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:8000 -t invoice
'''

22
web/index.html Normal file
View File

@ -0,0 +1,22 @@
<html>
<head>
<script>
async function parse(){
document.getElementById("result").innerHTML = "Submitting..."
var data = new FormData()
data.append('file', document.getElementById("docpicker").files[0])
document.getElementById("result").innerHTML = "Fetching..."
resp = await fetch('http://54.205.36.47:80/invoice', {method: "POST", body: data})
document.getElementById("result").innerHTML = "Fetched..."
data = await resp.json()
var result = JSON.stringify(data)
document.getElementById("result").innerHTML = result
}
</script>
<title>Makaron</title>
</head>
<body>
<input type="file" id="docpicker" accept=".pdf" onchange="parse()" />
<p id="result"></p>
</body>
</html>

View File

@ -0,0 +1,13 @@
{
"Id": "MyPolicy",
"Version": "2023-01-22",
"Statement": [
{
"Sid": "PublicReadForGetBucketObjects",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::443930-bucket/*"
}
]
}