Skip to content

Commit

Permalink
add: init docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
Cycrypto committed Jun 3, 2024
1 parent 8e933c4 commit 45953e4
Show file tree
Hide file tree
Showing 20 changed files with 262 additions and 49 deletions.
12 changes: 12 additions & 0 deletions .aws-sam/build.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is auto generated by SAM CLI build command

[function_build_definitions.447f7c9f-164f-4a26-bf6a-38da6b6910b6]
codeuri = "/Users/cycrpto/Desktop/tuk_sandol_team/sandol"
runtime = "python3.11"
architecture = "x86_64"
handler = "app.handler"
manifest_hash = ""
packagetype = "Zip"
functions = ["FastApiFunction"]

[layer_build_definitions]
Empty file added __init__.py
Empty file.
20 changes: 20 additions & 0 deletions events/event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"resource": "/{proxy+}",
"path": "/",
"httpMethod": "GET",
"headers": {
"Content-Type": "application/json"
},
"queryStringParameters": {},
"pathParameters": {
"proxy": "/"
},
"stageVariables": null,
"requestContext": {
"resourcePath": "/{proxy+}",
"httpMethod": "GET",
"path": "/prod/"
},
"body": null,
"isBase64Encoded": false
}
20 changes: 20 additions & 0 deletions events/meal/meal_event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"resource": "/{proxy+}",
"path": "/meal/view",
"httpMethod": "POST",
"headers": {
"Content-Type": "application/json"
},
"queryStringParameters": {},
"pathParameters": {
"proxy": "meal/view"
},
"stageVariables": null,
"requestContext": {
"resourcePath": "/{proxy+}",
"httpMethod": "POST",
"path": "/prod/meal/view"
},
"body": "{\"intent\":{\"id\":\"viqkzodj54puyo8ai5e6m49w\",\"name\":\"블록 이름\"},\"userRequest\":{\"timezone\":\"Asia/Seoul\",\"params\":{\"ignoreMe\":\"true\"},\"block\":{\"id\":\"viqkzodj54puyo8ai5e6m49w\",\"name\":\"블록 이름\"},\"utterance\":\"발화 내용\",\"lang\":null,\"user\":{\"id\":\"702912\",\"type\":\"accountId\",\"properties\":{}}},\"bot\":{\"id\":\"5e0f180affa74800014bd33d\",\"name\":\"봇 이름\"},\"action\":{\"name\":\"u0o68ejcea\",\"clientExtra\":null,\"params\":{\"meal\":\"view\"},\"id\":\"lwnhtra9gidojk68b6xr41o4\",\"detailParams\":{\"meal\":{\"origin\":\"view\",\"value\":\"view\",\"groupName\":\"\"}}}}",
"isBase64Encoded": false
}
5 changes: 0 additions & 5 deletions main.py

This file was deleted.

24 changes: 24 additions & 0 deletions packaged.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
FastApiFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.11
CodeUri: s3://sandol-deploy-bucket/773a22c99daca4bf37a9bcfe7c23d54f
MemorySize: 128
Timeout: 300
Policies:
- AWSLambdaBasicExecutionRole
Environment:
Variables:
ENV: prod
Events:
ApiEvent:
Type: Api
Properties:
Path: /{proxy+}
Method: ANY
Metadata:
SamResourceId: FastApiFunction
31 changes: 31 additions & 0 deletions samconfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# More information about the configuration file can be found here:
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html
version = 0.1

[default]
[default.global.parameters]
stack_name = "untitled"

[default.build.parameters]
parallel = true

[default.validate.parameters]
lint = true

[default.deploy.parameters]
capabilities = "CAPABILITY_IAM"
confirm_changeset = true
#resolve_s3 = true
resolve_image_repos = true

[default.package.parameters]
#resolve_s3 = true

[default.sync.parameters]
watch = true

[default.local_start_api.parameters]
warm_containers = "EAGER"

[default.local_start_lambda.parameters]
warm_containers = "EAGER"
18 changes: 18 additions & 0 deletions sandol/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Use the official Python image from the Docker Hub
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.11

# Set the working directory
WORKDIR /app

# Copy the requirements.txt file and install dependencies
COPY requirements.txt /app/
RUN pip install --no-cache-dir -r /app/requirements.txt

# Copy the entire 'sandol' directory to the working directory
COPY . /app

# Expose port 80
EXPOSE 80

# Command to run the FastAPI application
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "80"]
Empty file.
10 changes: 5 additions & 5 deletions sandol/api_server/meal.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
from fastapi import APIRouter
from fastapi.responses import JSONResponse

from sandol.api_server.kakao import Payload, ValidationPayload
from sandol.api_server.kakao.response import (
from api_server.kakao import Payload, ValidationPayload
from api_server.kakao.response import (
KakaoResponse, QuickReply, ActionEnum, ValidationResponse
)
from sandol.api_server.kakao.response.components import SimpleTextComponent
from sandol.api_server.utils import (
from api_server.kakao.response.components import SimpleTextComponent
from api_server.utils import (
meal_error_response_maker, split_string,
meal_response_maker, make_meal_cards,
check_tip_and_e
)
from sandol.crawler import (
from crawler import (
get_registration, Restaurant, get_meals
)

Expand Down
6 changes: 3 additions & 3 deletions sandol/api_server/settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""응답에 사용되는 상수들을 정의합니다."""
from sandol.api_server.kakao.response import QuickReply
from sandol.api_server.kakao.response.components import TextCardComponent
from sandol.api_server.kakao.response.interactiron import ActionEnum
from api_server.kakao.response import QuickReply
from api_server.kakao.response.components import TextCardComponent
from api_server.kakao.response.interactiron import ActionEnum

# 도움말 QuickReply
HELP = QuickReply(
Expand Down
14 changes: 7 additions & 7 deletions sandol/api_server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
from functools import wraps
import traceback

from sandol.api_server.settings import CAFETRIA_REGISTER_QUICK_REPLY_LIST
from sandol.api_server.kakao.response.components.card import ItemCardComponent
from sandol.api_server.kakao.response import KakaoResponse
from sandol.api_server.kakao.response.components import (
from api_server.settings import CAFETRIA_REGISTER_QUICK_REPLY_LIST
from api_server.kakao.response.components.card import ItemCardComponent
from api_server.kakao.response import KakaoResponse
from api_server.kakao.response.components import (
CarouselComponent, TextCardComponent, SimpleTextComponent)
from sandol.crawler import Restaurant
from sandol.crawler.ibookcrawler import BookTranslator
from sandol.crawler.ibookdownloader import BookDownloader
from crawler import Restaurant
from crawler.ibookcrawler import BookTranslator
from crawler.ibookdownloader import BookDownloader


def split_string(s: str) -> list[str]:
Expand Down
23 changes: 10 additions & 13 deletions sandol/app.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
"""Sandol의 메인 애플리케이션 파일입니다."""
from fastapi import FastAPI, HTTPException, Request, status # noqa: F401 # pylint: disable=W0611
from fastapi.responses import JSONResponse # noqa: F401

from sandol.api_server.meal import meal_api
from sandol.api_server.utils import error_message
from sandol.api_server.kakao.response import KakaoResponse

from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from mangum import Mangum
from api_server.meal import meal_api
from api_server.utils import error_message
from api_server.kakao.response import KakaoResponse

app = FastAPI()
app.include_router(meal_api)


@app.exception_handler(Exception)
async def http_exception_handler(request: Request, exc: Exception): # pylint: disable=W0613
async def http_exception_handler(request: Request, exc: Exception):
return JSONResponse(
KakaoResponse().add_component(error_message(exc)).get_dict()
)


@app.get("/")
async def root():
"""루트 페이지입니다."""
return "Hello Sandol"
return {"test": "Hello Sandol"}

handler = Mangum(app)
Binary file removed sandol/crawler/__pycache__/settings.cpython-311.pyc
Binary file not shown.
22 changes: 18 additions & 4 deletions requirements.txt → sandol/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
annotated-types==0.7.0
anyio==4.4.0
beautifulsoup4==4.12.3
blinker==1.7.0
bs4==0.0.2
Expand All @@ -7,30 +9,42 @@ click==8.1.7
colorama==0.4.6
et-xmlfile==1.1.0
exceptiongroup==1.2.0
Flask==3.0.2
fastapi==0.109.2
h11==0.14.0
httptools==0.6.1
idna==3.6
importlib_metadata==7.1.0
iniconfig==2.0.0
itsdangerous==2.1.2
Jinja2==3.1.3
mangum==0.17.0
MarkupSafe==2.1.5
numpy==1.26.4
openpyxl==3.1.2
packaging==24.0
pandas==2.2.2
platformdirs==4.2.0
pluggy==1.4.0
pydantic==2.7.2
pydantic_core==2.18.3
pytest==8.1.1
python-dateutil==2.9.0.post0
python-dotenv==1.0.1
pytz==2024.1
six==1.16.0
PyYAML==6.0.1
requests==2.31.0
six==1.16.0
sniffio==1.3.1
soupsieve==2.5
starlette==0.36.3
tomli==2.0.1
types-requests==2.32.0.20240523
typing_extensions==4.12.0
tzdata==2024.1
urllib3==2.2.1
uvicorn==0.30.0
uvloop==0.19.0
watchfiles==0.22.0
websockets==12.0
Werkzeug==3.0.1
yapf==0.40.2
zipp==3.18.1

22 changes: 22 additions & 0 deletions template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
FastApiFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.11
CodeUri: ./sandol
MemorySize: 128
Timeout: 300
Policies:
- AWSLambdaBasicExecutionRole
Environment:
Variables:
ENV: "prod"
Events:
ApiEvent:
Type: Api
Properties:
Path: /{proxy+}
Method: ANY
Empty file added tests/__init__.py
Empty file.
Empty file added tests/unit/__init__.py
Empty file.
72 changes: 72 additions & 0 deletions tests/unit/test_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import json

import pytest

from hello_world import app


@pytest.fixture()
def apigw_event():
""" Generates API GW Event"""

return {
"body": '{ "test": "body"}',
"resource": "/{proxy+}",
"requestContext": {
"resourceId": "123456",
"apiId": "1234567890",
"resourcePath": "/{proxy+}",
"httpMethod": "POST",
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
"accountId": "123456789012",
"identity": {
"apiKey": "",
"userArn": "",
"cognitoAuthenticationType": "",
"caller": "",
"userAgent": "Custom User Agent String",
"user": "",
"cognitoIdentityPoolId": "",
"cognitoIdentityId": "",
"cognitoAuthenticationProvider": "",
"sourceIp": "127.0.0.1",
"accountId": "",
},
"stage": "prod",
},
"queryStringParameters": {"foo": "bar"},
"headers": {
"Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)",
"Accept-Language": "en-US,en;q=0.8",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Mobile-Viewer": "false",
"X-Forwarded-For": "127.0.0.1, 127.0.0.2",
"CloudFront-Viewer-Country": "US",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Upgrade-Insecure-Requests": "1",
"X-Forwarded-Port": "443",
"Host": "1234567890.execute-api.us-east-1.amazonaws.com",
"X-Forwarded-Proto": "https",
"X-Amz-Cf-Id": "aaaaaaaaaae3VYQb9jd-nvCd-de396Uhbp027Y2JvkCPNLmGJHqlaA==",
"CloudFront-Is-Tablet-Viewer": "false",
"Cache-Control": "max-age=0",
"User-Agent": "Custom User Agent String",
"CloudFront-Forwarded-Proto": "https",
"Accept-Encoding": "gzip, deflate, sdch",
},
"pathParameters": {"proxy": "/examplepath"},
"httpMethod": "POST",
"stageVariables": {"baz": "qux"},
"path": "/examplepath",
}


def test_lambda_handler(apigw_event, mocker):

ret = app.lambda_handler(apigw_event, "")
data = json.loads(ret["body"])

assert ret["statusCode"] == 200
assert "message" in ret["body"]
assert data["message"] == "hello world"
12 changes: 0 additions & 12 deletions zappa_settings.json

This file was deleted.

0 comments on commit 45953e4

Please sign in to comment.