File tree Expand file tree Collapse file tree 5 files changed +146
-0
lines changed Expand file tree Collapse file tree 5 files changed +146
-0
lines changed Original file line number Diff line number Diff line change
1
+ # syntax=docker/dockerfile:1
2
+ # set base image
3
+ FROM python:3.8-slim
4
+ # set the working directory in the container
5
+ WORKDIR /app
6
+ # copy the dependencies list to the working directory
7
+ COPY requirements.txt .
8
+ # install dependencies
9
+ RUN pip3 install -r requirements.txt
10
+ # copy content of the source code folder to the working directory
11
+ COPY src/ .
12
+ # run on container start
13
+ CMD ["python3" , "./app.py" ]
14
+
Original file line number Diff line number Diff line change
1
+ apiVersion : v1
2
+ kind : Service
3
+ metadata :
4
+ name : math-api-service
5
+ spec :
6
+ type : NodePort
7
+ selector :
8
+ app : math-api
9
+ ports :
10
+ - port : 8000
11
+ targetPort : 5005
12
+ nodePort : 30008
13
+ ---
14
+
15
+ apiVersion : apps/v1
16
+ kind : Deployment
17
+ metadata :
18
+ name : math-api
19
+ spec :
20
+ selector :
21
+ matchLabels :
22
+ app : math-api
23
+ replicas : 4
24
+ template :
25
+ metadata :
26
+ labels :
27
+ app : math-api
28
+ spec :
29
+ containers :
30
+ - name : math-api
31
+ image : math-api
32
+ imagePullPolicy : IfNotPresent
33
+ ports :
34
+ - containerPort : 5005
Original file line number Diff line number Diff line change
1
+ Flask == 2.1.2
2
+ waitress == 2.1.2
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Created on Sun Jun 5 22:01:11 2022
5
+
6
+ @author: Ivan Nemov
7
+ """
8
+
9
+
10
+ from flask import Flask , request , jsonify
11
+ from waitress import serve
12
+ import json
13
+
14
+
15
+ def createApp ():
16
+ """
17
+ Flask Application
18
+ """
19
+ app = Flask (__name__ )
20
+
21
+ @app .route ('/' , methods = ['GET' ])
22
+ def index ():
23
+ """
24
+ API index page
25
+ """
26
+ return 'Test Flask-based Web API with math functions.'
27
+
28
+ @app .route ('/square' , methods = ['POST' ])
29
+ def square ():
30
+ """
31
+ Return square of posted number
32
+ """
33
+ req = request .get_json ()
34
+ if (not req ):
35
+ return 'JSON is incorrect.'
36
+
37
+ # check payload
38
+ try :
39
+ payload = json .loads (req )
40
+ except :
41
+ payload = None
42
+
43
+ # if not provided
44
+ if (not payload ):
45
+ return 'JSON is empty.'
46
+
47
+ # calculate square
48
+ try :
49
+ res = pow (float (payload ['val' ]), 2 )
50
+ except :
51
+ return 'Value is not provided or cannot be casted to float type.'
52
+
53
+ return jsonify ({'res' : res })
54
+
55
+ return app
56
+
57
+
58
+ if __name__ == '__main__' :
59
+ """
60
+ Entry point
61
+ """
62
+ app = createApp ()
63
+
64
+ # start web server
65
+ serve (app , host = "0.0.0.0" , port = 5005 )
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Created on Sun Jun 5 22:01:11 2022
5
+
6
+ @author: Ivan Nemov
7
+ """
8
+
9
+
10
+ import requests
11
+ import json
12
+
13
+ # prepare
14
+ base_url = 'http://127.0.0.1'
15
+ port = '8000'
16
+ route = '/square'
17
+ final_url = (':' .join ([base_url , port ]) if port != '' else base_url ) + route
18
+ payload = json .dumps ({'val' : 3.56 })
19
+
20
+ # get response on request
21
+ response = requests .post (final_url , json = payload , timeout = 2 )
22
+ res = json .loads (response .text )
23
+ response .close ()
24
+
25
+ # show results
26
+ print ('Result: ' )
27
+ print (res ['res' ])
28
+ print ('\n ' )
29
+
30
+ print ('Response code and reason: ' )
31
+ print (response .status_code , response .reason )
You can’t perform that action at this time.
0 commit comments