commit 1432bab5e1f96950c5b87869abfd2c4e689437de Author: Aleksandra Jonas Date: Fri Jan 27 21:36:03 2023 +0100 pls work diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..69c3fec --- /dev/null +++ b/Dockerfile @@ -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 \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..269c1ad --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# SaaS 5.2/5.3 + diff --git a/api.py b/api.py new file mode 100644 index 0000000..3ed3c19 --- /dev/null +++ b/api.py @@ -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) \ No newline at end of file diff --git a/credentials_template.py b/credentials_template.py new file mode 100644 index 0000000..0641c11 --- /dev/null +++ b/credentials_template.py @@ -0,0 +1,5 @@ +aws_access_key_id='' +aws_secret_access_key='' +aws_session_token='' + +default_vpc = '' \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..64b20d8 --- /dev/null +++ b/main.py @@ -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], + ) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..b6fd337 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +fastapi +tika +uvicorn +python-multipart \ No newline at end of file diff --git a/user_data.py b/user_data.py new file mode 100644 index 0000000..54fbf9c --- /dev/null +++ b/user_data.py @@ -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 +''' \ No newline at end of file diff --git a/web/index.html b/web/index.html new file mode 100644 index 0000000..6c45109 --- /dev/null +++ b/web/index.html @@ -0,0 +1,22 @@ + + + + Makaron + + + +

+ + \ No newline at end of file diff --git a/web/website-bucket-policy.json b/web/website-bucket-policy.json new file mode 100644 index 0000000..58a0645 --- /dev/null +++ b/web/website-bucket-policy.json @@ -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/*" + } + ] + } \ No newline at end of file