Skip to content

Commit dc1c3ca

Browse files
AsakerMohdsrprash
andauthored
Add a microservices based sample app to test enablement (#18)
Adding a microservices based sample app to help us test enablement for the python integration. By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice. --------- Co-authored-by: Prashant Srivastava <[email protected]>
1 parent 6afac54 commit dc1c3ca

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1722
-1
lines changed

eachdist.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
[lintroots]
66
extraroots=scripts/, sample-applications/
7-
subglob=*.py,tests/,test/,src/*, simple-client-server
7+
subglob=*.py,tests/,test/,src/*, simple-client-server, vehicle-dealership-sample-app
88

99
[testroots]
1010
extraroots=tests/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.env
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
static
2+
.idea
3+
.env
4+
db.sqlite3
5+
*.iml
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# Use an official Python runtime as a parent image
5+
FROM python:3.10
6+
7+
# Set environment variables
8+
ENV PYTHONDONTWRITEBYTECODE 1
9+
ENV PYTHONUNBUFFERED 1
10+
11+
# Set the working directory
12+
WORKDIR /image-service-app
13+
14+
# Install dependencies
15+
COPY ImageServiceApp/requirements.txt /image-service-app/
16+
RUN pip install -r requirements.txt
17+
18+
# Copy the project code into the container
19+
COPY ImageServiceApp/. /image-service-app/
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""
4+
ASGI config for ImageServiceApp project.
5+
6+
It exposes the ASGI callable as a module-level variable named ``application``.
7+
8+
For more information on this file, see
9+
https://docs.djangoproject.com/en/5.0/howto/deployment/asgi/
10+
"""
11+
12+
import os
13+
14+
from django.core.asgi import get_asgi_application
15+
16+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ImageServiceApp.settings")
17+
18+
application = get_asgi_application()
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""
4+
Django settings for ImageServiceApp project.
5+
6+
Generated by 'django-admin startproject' using Django 5.0.
7+
8+
For more information on this file, see
9+
https://docs.djangoproject.com/en/5.0/topics/settings/
10+
11+
For the full list of settings and their values, see
12+
https://docs.djangoproject.com/en/5.0/ref/settings/
13+
"""
14+
15+
import os
16+
from pathlib import Path
17+
18+
# Build paths inside the project like this: BASE_DIR / 'subdir'.
19+
BASE_DIR = Path(__file__).resolve().parent.parent
20+
21+
22+
# Quick-start development settings - unsuitable for production
23+
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/
24+
25+
# SECURITY WARNING: don't run with debug turned on in production!
26+
DEBUG = False
27+
28+
ALLOWED_HOSTS = ["*"]
29+
30+
# SECURITY WARNING: keep the secret key used in production secret!
31+
SECRET_KEY = "django-insecure-kixr5rw-%ik9i@395&jyjc$p_2%r(^vu0pv=)$$0)z8sjh84=#"
32+
33+
# Application definition
34+
35+
INSTALLED_APPS = [
36+
"django.contrib.admin",
37+
"django.contrib.auth",
38+
"django.contrib.contenttypes",
39+
"django.contrib.sessions",
40+
"django.contrib.messages",
41+
"django.contrib.staticfiles",
42+
"MainService.apps.MainServiceConfig",
43+
]
44+
45+
MIDDLEWARE = [
46+
"django.middleware.security.SecurityMiddleware",
47+
"django.contrib.sessions.middleware.SessionMiddleware",
48+
"django.middleware.common.CommonMiddleware",
49+
"django.middleware.csrf.CsrfViewMiddleware",
50+
"django.contrib.auth.middleware.AuthenticationMiddleware",
51+
"django.contrib.messages.middleware.MessageMiddleware",
52+
"django.middleware.clickjacking.XFrameOptionsMiddleware",
53+
]
54+
55+
ROOT_URLCONF = "ImageServiceApp.urls"
56+
57+
TEMPLATES = [
58+
{
59+
"BACKEND": "django.template.backends.django.DjangoTemplates",
60+
"DIRS": [],
61+
"APP_DIRS": True,
62+
"OPTIONS": {
63+
"context_processors": [
64+
"django.template.context_processors.debug",
65+
"django.template.context_processors.request",
66+
"django.contrib.auth.context_processors.auth",
67+
"django.contrib.messages.context_processors.messages",
68+
],
69+
},
70+
},
71+
]
72+
73+
WSGI_APPLICATION = "ImageServiceApp.wsgi.application"
74+
75+
76+
# Database
77+
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases
78+
79+
DATABASES = {
80+
"default": {
81+
"ENGINE": "django.db.backends.sqlite3",
82+
"NAME": BASE_DIR / "db.sqlite3",
83+
}
84+
}
85+
86+
87+
# Password validation
88+
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators
89+
90+
AUTH_PASSWORD_VALIDATORS = [
91+
{
92+
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
93+
},
94+
{
95+
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
96+
},
97+
{
98+
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
99+
},
100+
{
101+
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
102+
},
103+
]
104+
105+
106+
# Internationalization
107+
# https://docs.djangoproject.com/en/5.0/topics/i18n/
108+
109+
LANGUAGE_CODE = "en-us"
110+
111+
TIME_ZONE = "UTC"
112+
113+
USE_I18N = True
114+
115+
USE_TZ = True
116+
117+
118+
# Static files (CSS, JavaScript, Images)
119+
# https://docs.djangoproject.com/en/5.0/howto/static-files/
120+
121+
STATIC_URL = "static/"
122+
STATIC_ROOT = os.path.join(BASE_DIR, "static")
123+
124+
# Default primary key field type
125+
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
126+
127+
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""
4+
URL configuration for ImageServiceApp project.
5+
6+
The `urlpatterns` list routes URLs to views. For more information please see:
7+
https://docs.djangoproject.com/en/5.0/topics/http/urls/
8+
Examples:
9+
Function views
10+
1. Add an import: from my_app import views
11+
2. Add a URL to urlpatterns: path('', views.home, name='home')
12+
Class-based views
13+
1. Add an import: from other_app.views import Home
14+
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
15+
Including another URLconf
16+
1. Import the include() function: from django.urls import include, path
17+
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
18+
"""
19+
from django.urls import include, path
20+
21+
urlpatterns = [path("images/", include("MainService.urls"))]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""
4+
WSGI config for ImageServiceApp project.
5+
6+
It exposes the WSGI callable as a module-level variable named ``application``.
7+
8+
For more information on this file, see
9+
https://docs.djangoproject.com/en/5.0/howto/deployment/wsgi/
10+
"""
11+
12+
import os
13+
14+
from django.core.wsgi import get_wsgi_application
15+
16+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ImageServiceApp.settings")
17+
18+
application = get_wsgi_application()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
# from django.contrib import admin
4+
5+
# Register your models here.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
from django.apps import AppConfig
4+
5+
6+
class MainServiceConfig(AppConfig):
7+
default_auto_field = "django.db.models.BigAutoField"
8+
name = "MainService"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
# from django.db import models
4+
5+
# Create your models here.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
# from django.test import TestCase
4+
5+
# Create your tests here.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
from django.urls import path
4+
5+
from . import views
6+
7+
urlpatterns = [
8+
path("", views.index, name="index"),
9+
path("name/<str:image_name>", views.handle_image, name="image"),
10+
path("remote-image", views.get_remote_image, name="remote-image"),
11+
]
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
import os
4+
from threading import Thread
5+
from time import sleep
6+
7+
import boto3
8+
import requests
9+
from django.http import HttpResponse, HttpResponseNotAllowed
10+
from django.views.decorators.csrf import csrf_exempt
11+
from dotenv import load_dotenv
12+
13+
load_dotenv()
14+
15+
s3_client = boto3.client("s3")
16+
s3_resource = boto3.resource("s3")
17+
sqs = boto3.resource("sqs", region_name="us-west-2")
18+
19+
20+
# create resources if they don't exist
21+
# TODO: auto gen bucket name
22+
bucket_name = os.environ.get("S3_BUCKET")
23+
bucket = s3_resource.create_bucket(Bucket=bucket_name)
24+
queue = sqs.create_queue(
25+
QueueName="imageQueue.fifo", Attributes={"FifoQueue": "true", "ContentBasedDeduplication": "true"}
26+
)
27+
28+
29+
def read_from_queue():
30+
while True:
31+
for message in queue.receive_messages():
32+
image_name = message.body
33+
print("receiving " + image_name + " from the queue")
34+
s3_client.put_object(Bucket=bucket_name, Key=image_name)
35+
message.delete()
36+
sleep(10)
37+
38+
39+
thread = Thread(target=read_from_queue)
40+
41+
42+
def index(request):
43+
# return all images in s3?
44+
return HttpResponse("Hello, world LOL!")
45+
46+
47+
@csrf_exempt
48+
def handle_image(request, image_name):
49+
print(image_name)
50+
if request.method == "POST":
51+
put_image(image_name)
52+
return HttpResponse("Putting to queue")
53+
if request.method == "GET":
54+
return HttpResponse(get_image(image_name))
55+
return HttpResponseNotAllowed("Only GET requests are allowed!")
56+
57+
58+
def get_image(image_name):
59+
s3_object = s3_client.get_object(Bucket=bucket_name, Key=image_name)
60+
return str(s3_object)
61+
62+
63+
def get_remote_image(request):
64+
api_url = "https://google.com"
65+
return HttpResponse(requests.get(api_url, timeout=10))
66+
67+
68+
def put_image(image_name):
69+
queue.send_message(MessageBody=image_name, MessageGroupId="1")
70+
print("adding " + image_name + " to the queue")
71+
if not thread.is_alive():
72+
thread.start()
73+
return HttpResponse("Image added to the queue")
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python
2+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
# pylint: skip-file
5+
"""Django's command-line utility for administrative tasks."""
6+
import os
7+
import sys
8+
9+
10+
def main():
11+
"""Run administrative tasks."""
12+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ImageServiceApp.settings")
13+
try:
14+
from django.core.management import execute_from_command_line
15+
except ImportError as exc:
16+
raise ImportError(
17+
"Couldn't import Django. Are you sure it's installed and "
18+
"available on your PYTHONPATH environment variable? Did you "
19+
"forget to activate a virtual environment?"
20+
) from exc
21+
execute_from_command_line(sys.argv)
22+
23+
24+
if __name__ == "__main__":
25+
main()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Django~=5.0
2+
requests~=2.31.0
3+
boto3~=1.34.14
4+
python-dotenv~=1.0.0

0 commit comments

Comments
 (0)