Browse Source

개발 자료 업로드

heewon 3 years ago
commit
65bce790c8

+ 16 - 0
README.md

@@ -0,0 +1,16 @@
+# 월패드 데이터 수집 모듈
+	월패드 데이터 수집 모듈 저장소이다
+	docker 이미지를 생성하기 위한 소스코드와 yml 파일을 저장한다
+
+# 내용
+	월패드 데이터 수집 모듈은 flask를 사용하여 rest api 기반으로 작성되었다
+	flask는 python 기반 경량 rest api 서버로 기본적인 웹서버 기능을 제공하지만
+	프로덕션 환경에서 운영하기에는 적합하지 않다
+	따라서 일반적으로 사용되는 gunicorn + flask + nginx 조합으로 구성하였다
+	또한 배포시 환경구성의 어려운점을 개선하기 위해 도커 기반으로 작성하여 배포한다
+	해당 저장소는 도커 이미지를 만들기 위한 저장소로 rest api의 수정이 필요하면
+	저장소의 내용을 업데이트 해야한다.
+	
+ 	
+		
+		

+ 25 - 0
docker-compose.yml

@@ -0,0 +1,25 @@
+version: "3"
+
+services:
+        flask_app:
+                container_name: flask_app
+                restart: always
+                build: ./flask_app
+                image: ive2go/app:1.0.0
+                ports:
+                        - "9001:9001"                          
+                command: gunicorn -w 1 -b 0.0.0.0:9001 wsgi:server
+                volumes:
+                        - ~:/usr/src/flask_app/logs
+                environment:
+                        - SITE=sujigangyosan
+
+        nginx:
+                container_name: nginx
+                restart: always
+                build: ./nginx
+                image: ive2go/nginx:1.15.8
+                ports:
+                        - "9999:9999"
+                depends_on:
+                        - flask_app

+ 6 - 0
flask_app/Dockerfile

@@ -0,0 +1,6 @@
+FROM python:3.6.7
+
+WORKDIR usr/src/flask_app
+COPY requirements.txt .
+RUN pip install --no-cache-dir -r requirements.txt
+COPY . .

BIN
flask_app/__pycache__/app.cpython-38.pyc


+ 34 - 0
flask_app/app.py

@@ -0,0 +1,34 @@
+from flask import Flask, jsonify, request
+from flask_restful import reqparse, abort, Api, Resource
+from kinesis.producer import KinesisProducer
+import json
+import boto3
+import logging
+import logging.handlers
+import os
+
+server = Flask(__name__)
+api = Api(server)
+
+session = boto3.Session( 
+        aws_access_key_id='AKIAUSTWEOGGEJICFGIP',
+        aws_secret_access_key='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',
+        region_name='ap-northeast-2')
+
+logger = logging.getLogger(__name__)
+formatter = logging.Formatter('%(message)s')
+fileHandler = logging.FileHandler('./logs/log')
+fileHandler.setFormatter(formatter)
+logger.addHandler(fileHandler)
+logger.setLevel(level=logging.INFO)
+class LogWallpad(Resource):
+    def post(self):
+        producer = KinesisProducer(stream_name='ambt-preden-test',boto3_session=session)
+        request.json['site']=os.environ['SITE']
+        logger.info(json.dumps(request.json))
+        producer.put(json.dumps(request.json)+'\n')
+        return request.json, 201
+
+api.add_resource(LogWallpad, '/wallpad/log')
+
+

+ 22 - 0
flask_app/requirements.txt

@@ -0,0 +1,22 @@
+gunicorn
+aniso8601==9.0.1
+boto3==1.17.88
+botocore==1.20.88
+click==7.1.2
+Flask==1.1.1
+Flask-RESTful==0.3.9
+importlib-metadata==4.5.0
+itsdangerous==1.1.0
+Jinja2==2.11.2
+jmespath==0.10.0
+kinesis-python==0.2.1
+MarkupSafe==1.1.1
+offspring==0.1.1
+python-dateutil==2.8.1
+pytz==2021.1
+s3transfer==0.4.2
+six==1.16.0
+typing-extensions==3.10.0.0
+urllib3==1.26.5
+Werkzeug==1.0.1
+zipp==3.4.1

+ 4 - 0
flask_app/wsgi.py

@@ -0,0 +1,4 @@
+from app import server
+
+if __name__ == "__main__":
+        server.run(host='0.0.0.0', port=9001)

+ 6 - 0
nginx/Dockerfile

@@ -0,0 +1,6 @@
+FROM nginx:1.15.8
+
+RUN rm /etc/nginx/nginx.conf
+COPY nginx.conf /etc/nginx/
+RUN rm /etc/nginx/conf.d/default.conf
+COPY project.conf /etc/nginx/conf.d/

+ 40 - 0
nginx/nginx.conf

@@ -0,0 +1,40 @@
+# Define the user that will own and run the Nginx server
+user  nginx;
+# Define the number of worker processes; recommended value is the number of
+# cores that are being used by your server
+worker_processes  1;
+# Define the location on the file system of the error log, plus the minimum
+# severity to log messages for
+error_log  /var/log/nginx/error.log warn;
+# Define the file that will store the process ID of the main NGINX process
+pid        /var/run/nginx.pid;
+
+# events block defines the parameters that affect connection processing.
+events {
+    # Define the maximum number of simultaneous connections that can be opened by a worker proce$
+    worker_connections  1024;
+}
+
+# http block defines the parameters for how NGINX should handle HTTP web traffic
+http {
+    # Include the file defining the list of file types that are supported by NGINX
+    include       /etc/nginx/mime.types;
+    # Define the default file type that is returned to the user
+    default_type  text/html;
+    # Define the format of log messages.
+    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
+                      '$status $body_bytes_sent "$http_referer" '
+                      '"$http_user_agent" "$http_x_forwarded_for"';
+                          # Define the location of the log of access attempts to NGINX
+    access_log  /var/log/nginx/access.log  main;
+    # Define the parameters to optimize the delivery of static content
+    sendfile        on;
+    tcp_nopush     on;
+    tcp_nodelay    on;
+    # Define the timeout value for keep-alive connections with the client
+    keepalive_timeout  65;
+    # Define the usage of the gzip compression algorithm to reduce the amount of data to transmit
+    #gzip  on;
+    # Include additional parameters for virtual host(s)/server(s)
+    include /etc/nginx/conf.d/*.conf;
+}

+ 19 - 0
nginx/project.conf

@@ -0,0 +1,19 @@
+server {
+
+    listen 9999;
+    server_name docker_flask_gunicorn_nginx;
+
+    location / {
+        proxy_pass http://flask_app:9001;
+
+        # Do not change this
+        proxy_set_header Host $host;
+        proxy_set_header X-Real-IP $remote_addr;
+        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+    }
+
+    location /static {
+        rewrite ^/static(.*) /$1 break;
+        root /static;
+    }
+}

+ 5 - 0
run_docker.sh

@@ -0,0 +1,5 @@
+echo killing old docker processes
+docker-compose rm -fs
+
+echo building docker containers
+docker-compose up --build -d