14 |
15 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/Chapter06/templates/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
Cloud Native App
7 |
8 |
9 |
10 |
11 |
12 |
13 |
Welcome {{ session }}
14 |
15 |
16 |
17 |
18 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/Chapter07/templates/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
Cloud Native App
7 |
8 |
9 |
10 |
11 |
12 |
13 |
Welcome {{ session }}
14 |
15 |
16 |
17 |
18 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/Chapter08/templates/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
Cloud Native App
7 |
8 |
9 |
10 |
11 |
12 |
13 |
Welcome {{ user['nickname'] }}
14 |
15 |
16 |
17 |
18 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/Chapter10/app/cloud-native-app/templates/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
Cloud Native App
7 |
8 |
9 |
10 |
11 |
12 |
13 |
Welcome {{ session }}
14 |
15 |
16 |
17 |
18 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/Chapter02/flask_test.py:
--------------------------------------------------------------------------------
1 | # Test cases for restful API
2 | from app import app
3 | import unittest
4 |
5 | class FlaskappTests(unittest.TestCase):
6 | def setUp(self):
7 | # creates a test client
8 | self.app = app.test_client()
9 | # propagate the exceptions to the test client
10 | self.app.testing = True
11 | def test_users_status_code(self):
12 | # sends HTTP GET request to the application
13 | # on the specified path
14 | result = self.app.get('/api/v1/users')
15 | print (result)
16 | # assert the status code of the response
17 | self.assertEqual(result.status_code, 200)
18 |
19 | def test_tweets_status_code(self):
20 | # sends HTTP GET request to the application
21 | # on the specified path
22 | result = self.app.get('/api/v2/tweets')
23 |
24 | # assert the status code of the response
25 | self.assertEqual(result.status_code, 200)
26 |
27 | def test_addusers_status_code(self):
28 | # sends HTTP POST request to the application
29 | # on the specified path
30 | result = self.app.post('/api/v1/users', data='{ "username":"Ovestint", "email": "ronaldrvera@jourrapide.com", "password": "juzahpei6e", "name":"Ronald R. Vera"}', content_type='application/json')
31 | print (result)
32 | # assert the status code of the response
33 | self.assertEquals(result.status_code, 201)
34 |
35 | def test_updusers_status_code(self):
36 | # sends HTTP PUT request to the application
37 | # on the specified path
38 | result = self.app.put('/api/v1/users/5', data='{"username":"Tagning", "email": "leolaLguertin@teleworm.us"}', content_type='application/json')
39 | # assert the status code of the response
40 | self.assertEquals(result.status_code, 200)
41 | def test_addtweets_status_code(self):
42 | # sends HTTP GET request to the application
43 | # on the specified path
44 | result = self.app.post('/api/v2/tweets', data='{"username":"Tagning", "body": "It Works!#Awesome"}', content_type='application/json')
45 |
46 | # assert the status code of the response
47 | self.assertEqual(result.status_code, 201)
48 |
49 | def test_delusers_status_code(self):
50 | # sends HTTP Delete request to the application
51 | # on the specified path
52 | result = self.app.delete('/api/v1/users', data='{"username":"Ovestint"}', content_type='application/json')
53 | print (result)
54 | # assert the status code of the response
55 | self.assertEquals(result.status_code, 200)
56 |
--------------------------------------------------------------------------------
/Chapter03/flask_test.py:
--------------------------------------------------------------------------------
1 | # Test cases for restful API
2 | from app import app
3 | import unittest
4 |
5 | class FlaskappTests(unittest.TestCase):
6 | def setUp(self):
7 | # creates a test client
8 | self.app = app.test_client()
9 | # propagate the exceptions to the test client
10 | self.app.testing = True
11 | def test_users_status_code(self):
12 | # sends HTTP GET request to the application
13 | # on the specified path
14 | result = self.app.get('/api/v1/users')
15 | print (result)
16 | # assert the status code of the response
17 | self.assertEqual(result.status_code, 200)
18 |
19 | def test_tweets_status_code(self):
20 | # sends HTTP GET request to the application
21 | # on the specified path
22 | result = self.app.get('/api/v2/tweets')
23 |
24 | # assert the status code of the response
25 | self.assertEqual(result.status_code, 200)
26 |
27 | def test_addusers_status_code(self):
28 | # sends HTTP POST request to the application
29 | # on the specified path
30 | result = self.app.post('/api/v1/users', data='{ "username":"Ovestint", "email": "ronaldrvera@jourrapide.com", "password": "juzahpei6e", "name":"Ronald R. Vera"}', content_type='application/json')
31 | print (result)
32 | # assert the status code of the response
33 | self.assertEquals(result.status_code, 201)
34 |
35 | def test_updusers_status_code(self):
36 | # sends HTTP PUT request to the application
37 | # on the specified path
38 | result = self.app.put('/api/v1/users/5', data='{"username":"Tagning", "email": "leolaLguertin@teleworm.us"}', content_type='application/json')
39 | # assert the status code of the response
40 | self.assertEquals(result.status_code, 200)
41 | def test_addtweets_status_code(self):
42 | # sends HTTP GET request to the application
43 | # on the specified path
44 | result = self.app.post('/api/v2/tweets', data='{"username":"Tagning", "body": "It Works!#Awesome"}', content_type='application/json')
45 |
46 | # assert the status code of the response
47 | self.assertEqual(result.status_code, 201)
48 |
49 | def test_delusers_status_code(self):
50 | # sends HTTP Delete request to the application
51 | # on the specified path
52 | result = self.app.delete('/api/v1/users', data='{"username":"Ovestint"}', content_type='application/json')
53 | print (result)
54 | # assert the status code of the response
55 | self.assertEquals(result.status_code, 200)
56 |
--------------------------------------------------------------------------------
/Chapter04/flask_test.py:
--------------------------------------------------------------------------------
1 | # Test cases for restful API
2 | from app import app
3 | import unittest
4 |
5 | class FlaskappTests(unittest.TestCase):
6 | def setUp(self):
7 | # creates a test client
8 | self.app = app.test_client()
9 | # propagate the exceptions to the test client
10 | self.app.testing = True
11 | def test_users_status_code(self):
12 | # sends HTTP GET request to the application
13 | # on the specified path
14 | result = self.app.get('/api/v1/users')
15 | print (result)
16 | # assert the status code of the response
17 | self.assertEqual(result.status_code, 200)
18 |
19 | def test_tweets_status_code(self):
20 | # sends HTTP GET request to the application
21 | # on the specified path
22 | result = self.app.get('/api/v2/tweets')
23 |
24 | # assert the status code of the response
25 | self.assertEqual(result.status_code, 200)
26 |
27 | def test_addusers_status_code(self):
28 | # sends HTTP POST request to the application
29 | # on the specified path
30 | result = self.app.post('/api/v1/users', data='{ "username":"Ovestint", "email": "ronaldrvera@jourrapide.com", "password": "juzahpei6e", "name":"Ronald R. Vera"}', content_type='application/json')
31 | print (result)
32 | # assert the status code of the response
33 | self.assertEquals(result.status_code, 201)
34 |
35 | def test_updusers_status_code(self):
36 | # sends HTTP PUT request to the application
37 | # on the specified path
38 | result = self.app.put('/api/v1/users/5', data='{"username":"Tagning", "email": "leolaLguertin@teleworm.us"}', content_type='application/json')
39 | # assert the status code of the response
40 | self.assertEquals(result.status_code, 200)
41 | def test_addtweets_status_code(self):
42 | # sends HTTP GET request to the application
43 | # on the specified path
44 | result = self.app.post('/api/v2/tweets', data='{"username":"Tagning", "body": "It Works!#Awesome"}', content_type='application/json')
45 |
46 | # assert the status code of the response
47 | self.assertEqual(result.status_code, 201)
48 |
49 | def test_delusers_status_code(self):
50 | # sends HTTP Delete request to the application
51 | # on the specified path
52 | result = self.app.delete('/api/v1/users', data='{"username":"Ovestint"}', content_type='application/json')
53 | print (result)
54 | # assert the status code of the response
55 | self.assertEquals(result.status_code, 200)
56 |
--------------------------------------------------------------------------------
/Chapter05/node_modules/requirejs/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "_args": [
3 | [
4 | {
5 | "raw": "requirejs@^2.3.3",
6 | "scope": null,
7 | "escapedName": "requirejs",
8 | "name": "requirejs",
9 | "rawSpec": "^2.3.3",
10 | "spec": ">=2.3.3 <3.0.0",
11 | "type": "range"
12 | },
13 | "/vagrant/github/Cloud-Native-Python/chapter 05"
14 | ]
15 | ],
16 | "_from": "requirejs@>=2.3.3 <3.0.0",
17 | "_id": "requirejs@2.3.3",
18 | "_inCache": true,
19 | "_location": "/requirejs",
20 | "_nodeVersion": "6.7.0",
21 | "_npmOperationalInternal": {
22 | "host": "packages-12-west.internal.npmjs.com",
23 | "tmp": "tmp/requirejs-2.3.3.tgz_1487524083275_0.3237284303177148"
24 | },
25 | "_npmUser": {
26 | "name": "jrburke",
27 | "email": "jrburke@gmail.com"
28 | },
29 | "_npmVersion": "3.10.3",
30 | "_phantomChildren": {},
31 | "_requested": {
32 | "raw": "requirejs@^2.3.3",
33 | "scope": null,
34 | "escapedName": "requirejs",
35 | "name": "requirejs",
36 | "rawSpec": "^2.3.3",
37 | "spec": ">=2.3.3 <3.0.0",
38 | "type": "range"
39 | },
40 | "_requiredBy": [
41 | "/"
42 | ],
43 | "_resolved": "https://registry.npmjs.org/requirejs/-/requirejs-2.3.3.tgz",
44 | "_shasum": "aa59fd3a0287eaf407959a138228044b5dd6a6a3",
45 | "_shrinkwrap": null,
46 | "_spec": "requirejs@^2.3.3",
47 | "_where": "/vagrant/github/Cloud-Native-Python/chapter 05",
48 | "author": {
49 | "name": "James Burke",
50 | "email": "jrburke@gmail.com",
51 | "url": "http://github.com/jrburke"
52 | },
53 | "bin": {
54 | "r.js": "./bin/r.js",
55 | "r_js": "./bin/r.js"
56 | },
57 | "bugs": {
58 | "url": "https://github.com/jrburke/r.js/issues"
59 | },
60 | "dependencies": {},
61 | "description": "Node adapter for RequireJS, for loading AMD modules. Includes RequireJS optimizer",
62 | "devDependencies": {},
63 | "directories": {},
64 | "dist": {
65 | "shasum": "aa59fd3a0287eaf407959a138228044b5dd6a6a3",
66 | "tarball": "https://registry.npmjs.org/requirejs/-/requirejs-2.3.3.tgz"
67 | },
68 | "engines": {
69 | "node": ">=0.4.0"
70 | },
71 | "homepage": "http://github.com/jrburke/r.js",
72 | "license": "MIT",
73 | "main": "./bin/r.js",
74 | "maintainers": [
75 | {
76 | "name": "jrburke",
77 | "email": "jrburke@gmail.com"
78 | }
79 | ],
80 | "name": "requirejs",
81 | "optionalDependencies": {},
82 | "readme": "ERROR: No README data found!",
83 | "repository": {
84 | "type": "git",
85 | "url": "git+https://github.com/jrburke/r.js.git"
86 | },
87 | "scripts": {},
88 | "version": "2.3.3"
89 | }
90 |
--------------------------------------------------------------------------------
/Chapter03/static/style.css:
--------------------------------------------------------------------------------
1 | ::selection{ background-color: #E13300; color: white; }
2 | ::moz-selection{ background-color: #E13300; color: white; }
3 | ::webkit-selection{ background-color: #E13300; color: white; }
4 |
5 | body {
6 | font-family: "Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;
7 | color: #888;
8 | font-style: normal;
9 | font-size: 14px;
10 | line-height: 22px;
11 | }
12 |
13 | a {
14 | color: #003399;
15 | background-color: transparent;
16 | font-weight: normal;
17 | }
18 |
19 | .title {
20 | padding: 0 20px;
21 | background: #32373a;
22 | position: relative;
23 | height: 50px;
24 | }
25 |
26 | h1 {
27 | color: #fff;
28 | margin: 0;
29 | font-size: 18px;
30 | font-weight: 400;
31 | line-height: 50px;
32 | text-transform: capitalize;
33 | }
34 |
35 | code {
36 | font-family: Consolas, Monaco, Courier New, Courier, monospace;
37 | font-size: 12px;
38 | background-color: #f9f9f9;
39 | border: 1px solid #D0D0D0;
40 | color: #002166;
41 | display: block;
42 | padding: 12px 10px 12px 10px;
43 | }
44 |
45 | #content {
46 | padding: 20px;
47 | zoom: 1;
48 | }
49 |
50 | #body{
51 | margin: 0 15px 0 15px;
52 | }
53 |
54 | p.footer{
55 | text-align: right;
56 | font-size: 11px;
57 | border-top: 1px solid #D0D0D0;
58 | line-height: 32px;
59 | padding: 0 10px 0 10px;
60 | margin: 20px 0 0 0;
61 | }
62 |
63 | #container{
64 | margin: 10px;
65 | border: 1px solid #D0D0D0;
66 | -webkit-box-shadow: 0 0 8px #D0D0D0;
67 | }
68 |
69 | /* Form Styles */
70 | form label{
71 | display: block;
72 | margin-bottom: 5px;
73 | }
74 | form input[type=text]{
75 | border: 1px solid #aaa;
76 | border-radius: 2px;
77 | box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
78 | box-sizing: border-box;
79 | padding: 5px;
80 | margin-bottom: 20px;
81 | outline: 0;
82 | -webkit-border-radius: 2px;
83 | -moz-border-radius: 2px;
84 | -webkit-box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
85 | -moz-box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
86 | }
87 | form input[type=submit]{
88 | background: #198ad1;
89 | color: #fff;
90 | display: inline-block;
91 | padding: 0 20px;
92 | font-size: 16px;
93 | line-height: 2.6em;
94 | font-family: "Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;
95 | font-weight: 400;
96 | -webkit-box-shadow: inset 0 -1px 0 rgba(0,0,0,.1);
97 | -moz-box-shadow: inset 0 -1px 0 rgba(0,0,0,.1);
98 | box-shadow: inset 0 -1px 0 rgba(0,0,0,.1);
99 | -webkit-border-radius: 2px;
100 | -moz-border-radius: 2px;
101 | border-radius: 2px;
102 | text-transform: capitalize;
103 | border: 0;
104 | cursor: pointer;
105 | text-align: center;
106 | }
107 |
--------------------------------------------------------------------------------
/Chapter04/static/style.css:
--------------------------------------------------------------------------------
1 | ::selection{ background-color: #E13300; color: white; }
2 | ::moz-selection{ background-color: #E13300; color: white; }
3 | ::webkit-selection{ background-color: #E13300; color: white; }
4 |
5 | body {
6 | font-family: "Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;
7 | color: #888;
8 | font-style: normal;
9 | font-size: 14px;
10 | line-height: 22px;
11 | }
12 |
13 | a {
14 | color: #003399;
15 | background-color: transparent;
16 | font-weight: normal;
17 | }
18 |
19 | .title {
20 | padding: 0 20px;
21 | background: #32373a;
22 | position: relative;
23 | height: 50px;
24 | }
25 |
26 | h1 {
27 | color: #fff;
28 | margin: 0;
29 | font-size: 18px;
30 | font-weight: 400;
31 | line-height: 50px;
32 | text-transform: capitalize;
33 | }
34 |
35 | code {
36 | font-family: Consolas, Monaco, Courier New, Courier, monospace;
37 | font-size: 12px;
38 | background-color: #f9f9f9;
39 | border: 1px solid #D0D0D0;
40 | color: #002166;
41 | display: block;
42 | padding: 12px 10px 12px 10px;
43 | }
44 |
45 | #content {
46 | padding: 20px;
47 | zoom: 1;
48 | }
49 |
50 | #body{
51 | margin: 0 15px 0 15px;
52 | }
53 |
54 | p.footer{
55 | text-align: right;
56 | font-size: 11px;
57 | border-top: 1px solid #D0D0D0;
58 | line-height: 32px;
59 | padding: 0 10px 0 10px;
60 | margin: 20px 0 0 0;
61 | }
62 |
63 | #container{
64 | margin: 10px;
65 | border: 1px solid #D0D0D0;
66 | -webkit-box-shadow: 0 0 8px #D0D0D0;
67 | }
68 |
69 | /* Form Styles */
70 | form label{
71 | display: block;
72 | margin-bottom: 5px;
73 | }
74 | form input[type=text]{
75 | border: 1px solid #aaa;
76 | border-radius: 2px;
77 | box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
78 | box-sizing: border-box;
79 | padding: 5px;
80 | margin-bottom: 20px;
81 | outline: 0;
82 | -webkit-border-radius: 2px;
83 | -moz-border-radius: 2px;
84 | -webkit-box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
85 | -moz-box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
86 | }
87 | form input[type=submit]{
88 | background: #198ad1;
89 | color: #fff;
90 | display: inline-block;
91 | padding: 0 20px;
92 | font-size: 16px;
93 | line-height: 2.6em;
94 | font-family: "Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;
95 | font-weight: 400;
96 | -webkit-box-shadow: inset 0 -1px 0 rgba(0,0,0,.1);
97 | -moz-box-shadow: inset 0 -1px 0 rgba(0,0,0,.1);
98 | box-shadow: inset 0 -1px 0 rgba(0,0,0,.1);
99 | -webkit-border-radius: 2px;
100 | -moz-border-radius: 2px;
101 | border-radius: 2px;
102 | text-transform: capitalize;
103 | border: 0;
104 | cursor: pointer;
105 | text-align: center;
106 | }
107 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | # Cloud Native Python
5 | This is the code repository for [Cloud Native Python](https://www.packtpub.com/application-development/cloud-native-python?utm_source=github&utm_medium=repository&utm_campaign=9781787129313), published by [Packt](https://www.packtpub.com/?utm_source=github). It contains all the supporting project files necessary to work through the book from start to finish.
6 | ## About the Book
7 | Businesses today are evolving so rapidly that having their own infrastructure to support their expansion is not feasible. As a result, they have been resorting to the elasticity of the cloud to provide a platform to build and deploy their highly scalable applications.
8 |
9 |
10 | ## Instructions and Navigation
11 | All of the code is organized into folders. Each folder starts with a number followed by the application name. For example, Chapter02.
12 |
13 | All the code files are present in their respective chapter folders.
14 | Chapters 9, 12, and 13 do not contain any code files.
15 |
16 | The code will look like the following:
17 | ```
18 | self.addUser = function() {
19 | self.save();
20 | self.name("");
21 | self.username("");
22 | self.email("");
23 | self.password("");
24 | };
25 | ```
26 |
27 | You will need to have Python installed on your system. A text editor, preferably
28 | Vim/Sublime/Notepad++, would be great. For one of the chapters, you may be required to
29 | download POSTMAN, which is a powerful API testing suite available as a Chrome
30 | extension. You can download this at https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en.
31 | Other than these, it would be great if you have an account on the following web applications:
32 |
33 | * Jenkins
34 | * Docker
35 | * Amazon Web Services
36 | * Terraform
37 |
38 | In case you do not have an account, this book will guide you, or at least direct you with
39 | regards to creating an account on the previously mentioned web applications.
40 |
41 | ## Related Products
42 | * [Cloud Native Applications in Java](https://www.packtpub.com/application-development/cloud-native-applications-java?utm_source=github&utm_medium=repository&utm_campaign=9781787124349)
43 |
44 | * [Deploying Cloud-Native AWS components [Video]](https://www.packtpub.com/virtualization-and-cloud/deploying-cloud-native-aws-components-video?utm_source=github&utm_medium=repository&utm_campaign=9781788299756)
45 |
46 | * [Cloud Development and Deployment with CloudBees](https://www.packtpub.com/virtualization-and-cloud/cloud-development-and-deployment-cloudbees?utm_source=github&utm_medium=repository&utm_campaign=9781783281633)
47 | ### Download a free PDF
48 |
49 |
If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost. Simply click on the link to claim your free PDF.
50 |
https://packt.link/free-ebook/9781787129313
--------------------------------------------------------------------------------
/Chapter11/cloudformation/vpc.template:
--------------------------------------------------------------------------------
1 | {
2 | "AWSTemplateFormatVersion" : "2010-09-09",
3 |
4 | "Description" : "Builds a VPC w/ INET Gateway and a public subnets. This template creates Amazon EC2 instance(s)",
5 |
6 | "Resources" : {
7 |
8 | "VPC" : {
9 | "Type" : "AWS::EC2::VPC",
10 | "Properties" : {
11 | "CidrBlock" : "172.31.0.0/16",
12 | "Tags" : [
13 | {"Key" : "Application", "Value" : { "Ref" : "AWS::StackName"} },
14 | {"Key" : "Network", "Value" : "Public" }
15 | ]
16 | }
17 | },
18 |
19 | "PublicSubnet" : {
20 | "Type" : "AWS::EC2::Subnet",
21 | "Properties" : {
22 | "VpcId" : { "Ref" : "VPC" },
23 | "CidrBlock" : "172.31.16.0/20",
24 | "AvailabilityZone" : { "Fn::Select": [ "0", {"Fn::GetAZs": {"Ref": "AWS::Region"}} ]},
25 | "Tags" : [
26 | {"Key" : "Application", "Value" : { "Ref" : "AWS::StackName"} },
27 | {"Key" : "Network", "Value" : "Public" }
28 | ]
29 | }
30 | },
31 | "PrivateSubnet" : {
32 | "Type" : "AWS::EC2::Subnet",
33 | "Properties" : {
34 | "VpcId" : { "Ref" : "VPC" },
35 | "CidrBlock" : "172.31.0.0/20",
36 | "AvailabilityZone" : { "Fn::Select": [ "0", {"Fn::GetAZs": {"Ref": "AWS::Region"}} ]},
37 | "Tags" : [
38 | {"Key" : "Application", "Value" : { "Ref" : "AWS::StackName"} },
39 | {"Key" : "Network", "Value" : "Public" }
40 | ]
41 | }
42 | },
43 |
44 |
45 | "InternetGateway" : {
46 | "Type" : "AWS::EC2::InternetGateway",
47 | "Properties" : {
48 | "Tags" : [
49 | {"Key" : "Application", "Value" : { "Ref" : "AWS::StackName"} },
50 | {"Key" : "Network", "Value" : "Public" }
51 | ]
52 | }
53 | },
54 |
55 | "AttachGateway" : {
56 | "Type" : "AWS::EC2::VPCGatewayAttachment",
57 | "Properties" : {
58 | "VpcId" : { "Ref" : "VPC" },
59 | "InternetGatewayId" : { "Ref" : "InternetGateway" }
60 | }
61 | },
62 |
63 | "PublicRouteTable" : {
64 | "Type" : "AWS::EC2::RouteTable",
65 | "Properties" : {
66 | "VpcId" : {"Ref" : "VPC"},
67 | "Tags" : [
68 | {"Key" : "Application", "Value" : { "Ref" : "AWS::StackName"} },
69 | {"Key" : "Network", "Value" : "Public" }
70 | ]
71 | }
72 | },
73 |
74 | "PublicRoute" : {
75 | "Type" : "AWS::EC2::Route",
76 | "Properties" : {
77 | "RouteTableId" : { "Ref" : "PublicRouteTable" },
78 | "DestinationCidrBlock" : "0.0.0.0/0",
79 | "GatewayId" : { "Ref" : "InternetGateway" }
80 | }
81 | },
82 |
83 | "PublicSubnetRouteTableAssociation" : {
84 | "Type" : "AWS::EC2::SubnetRouteTableAssociation",
85 | "Properties" : {
86 | "SubnetId" : { "Ref" : "PublicSubnet" },
87 | "RouteTableId" : { "Ref" : "PublicRouteTable" }
88 | }
89 | }
90 | },
91 |
92 | "Outputs" : {
93 | "VpcId" : {
94 | "Value" : {"Ref" : "VPC"},
95 | "Description" : "VPC ID of newly created VPC"
96 | },
97 |
98 | "PublicSubnet" : {
99 | "Value" : {"Ref" : "PublicSubnet"},
100 | "Description" : "Public Subnet in AZ "
101 | }
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/Chapter05/requests.py:
--------------------------------------------------------------------------------
1 | from flask import Flask, render_template, request, jsonify, redirect, session
2 | from flask import abort
3 | from flask_cors import CORS, cross_origin
4 | from flask import make_response, url_for
5 | import json
6 | import random
7 | from pymongo import MongoClient
8 | from time import gmtime, strftime
9 |
10 | # connection to MongoDB Database
11 | connection = MongoClient("mongodb://localhost:27017/")
12 |
13 | class Requests:
14 | # List users
15 | def list_users():
16 | api_list=[]
17 | db = connection.app.users
18 | for row in db.find():
19 | api_list.append(str(row))
20 | # print (api_list)
21 | return jsonify({'user_list': api_list})
22 |
23 | # List specific users
24 | def list_user(user_id):
25 | print (user_id)
26 | api_list=[]
27 | db = connection.app.users
28 | for i in db.find({'id':user_id}):
29 | api_list.append(str(i))
30 |
31 | if api_list == []:
32 | abort(404)
33 | return jsonify({'user_details':api_list})
34 |
35 | # List specific tweet
36 | def list_tweet(user_id):
37 | print (user_id)
38 | db = connection.app.tweets
39 | api_list=[]
40 | tweet = db.find({'tweetedby':user_id})
41 | for i in tweet:
42 | api_list.append(str(i))
43 | if api_list == []:
44 | abort(404)
45 | return jsonify({'tweet': api_list})
46 |
47 | # Adding user
48 | def add_user(new_user):
49 | api_list=[]
50 | print (new_user)
51 | db = connection.app.users
52 | user = db.find({'$or':[{"username":new_user['username']} ,{"email":new_user['email']}]})
53 | for i in user:
54 | print (str(i))
55 | api_list.append(str(i))
56 |
57 | # print (api_list)
58 | if api_list == []:
59 | # print(new_user)
60 | db.insert(new_user)
61 | return "Success"
62 | else :
63 | abort(409)
64 |
65 | # Deleting User
66 | def del_user(del_user):
67 | db = connection.app.users
68 | api_list=[]
69 | for i in db.find({'username':del_user}):
70 | api_list.append(str(i))
71 |
72 | if api_list == []:
73 | abort(404)
74 | else:
75 | db.remove({"username":del_user})
76 | return "Success"
77 |
78 | # List tweets
79 | def list_tweets():
80 |
81 | api_list=[]
82 | dict = {}
83 | db = connection.app.tweets
84 | for row in db.find():
85 | print (row)
86 | dict = {}
87 | dict['id'] = row['id']
88 | dict['timestamp'] = row['timestamp']
89 | dict['tweetedby'] = row['tweetedby']
90 | dict['body'] = row['body']
91 | api_list.append(dict)
92 |
93 | print (api_list)
94 | return json.dumps(api_list)
95 |
96 | # Adding tweets
97 | def add_tweet(new_tweet):
98 | api_list=[]
99 | print (new_tweet)
100 | db_user = connection.app.users
101 | db_tweet = connection.app.tweets
102 |
103 | user = db_user.find({"username":new_tweet['tweetedby']})
104 | for i in user:
105 | api_list.append(str(i))
106 | if api_list == []:
107 | abort(404)
108 | else:
109 | db_tweet.insert(new_tweet)
110 | return "Success"
111 |
112 | def upd_user(user):
113 | api_list=[]
114 | print (user)
115 | db_user = connection.app.users
116 | users = db_user.find_one({"id":user['id']})
117 | for i in users:
118 | api_list.append(str(i))
119 | if api_list == []:
120 | abort(409)
121 | else:
122 | db_user.update({'id':user['id']},{'$set': user}, upsert=False )
123 | return "Success"
124 |
--------------------------------------------------------------------------------
/Chapter06/requests.py:
--------------------------------------------------------------------------------
1 | from flask import Flask, render_template, request, jsonify, redirect, session
2 | from flask import abort
3 | from flask_cors import CORS, cross_origin
4 | from flask import make_response, url_for
5 | import json
6 | import random
7 | from pymongo import MongoClient
8 | from time import gmtime, strftime
9 |
10 | # connection to MongoDB Database
11 | connection = MongoClient("mongodb://localhost:27017/")
12 |
13 | class Requests:
14 | # List users
15 | def list_users():
16 | api_list=[]
17 | db = connection.app.users
18 | for row in db.find():
19 | api_list.append(str(row))
20 | # print (api_list)
21 | return jsonify({'user_list': api_list})
22 |
23 | # List specific users
24 | def list_user(user_id):
25 | print (user_id)
26 | api_list=[]
27 | db = connection.app.users
28 | for i in db.find({'id':user_id}):
29 | api_list.append(str(i))
30 |
31 | if api_list == []:
32 | abort(404)
33 | return jsonify({'user_details':api_list})
34 |
35 | # List specific tweet
36 | def list_tweet(user_id):
37 | print (user_id)
38 | db = connection.app.tweets
39 | api_list=[]
40 | tweet = db.find({'tweetedby':user_id})
41 | for row in tweet:
42 | dict = {}
43 | dict['id'] = row['id']
44 | dict['timestamp'] = row['timestamp']
45 | dict['tweetedby'] = row['tweetedby']
46 | dict['body'] = row['body']
47 | api_list.append(dict)
48 | if api_list == []:
49 | abort(404)
50 | print (api_list)
51 | return json.dumps(api_list)
52 |
53 | # Adding user
54 | def add_user(new_user):
55 | api_list=[]
56 | print (new_user)
57 | db = connection.app.users
58 | user = db.find({'$or':[{"username":new_user['username']} ,{"email":new_user['email']}]})
59 | for i in user:
60 | print (str(i))
61 | api_list.append(str(i))
62 |
63 | # print (api_list)
64 | if api_list == []:
65 | # print(new_user)
66 | db.insert(new_user)
67 | return "Success"
68 | else :
69 | abort(409)
70 |
71 | # Deleting User
72 | def del_user(del_user):
73 | db = connection.app.users
74 | api_list=[]
75 | for i in db.find({'username':del_user}):
76 | api_list.append(str(i))
77 |
78 | if api_list == []:
79 | abort(404)
80 | else:
81 | db.remove({"username":del_user})
82 | return "Success"
83 |
84 | # List tweets
85 | def list_tweets():
86 |
87 | api_list=[]
88 | dict = {}
89 | db = connection.app.tweets
90 | for row in db.find():
91 | print (row)
92 | dict = {}
93 | dict['id'] = row['id']
94 | dict['timestamp'] = row['timestamp']
95 | dict['tweetedby'] = row['tweetedby']
96 | dict['body'] = row['body']
97 | api_list.append(dict)
98 |
99 | print (api_list)
100 | return json.dumps(api_list)
101 |
102 | # Adding tweets
103 | def add_tweet(new_tweet):
104 | api_list=[]
105 | print (new_tweet)
106 | db_user = connection.app.users
107 | db_tweet = connection.app.tweets
108 |
109 | user = db_user.find({"username":new_tweet['tweetedby']})
110 | for i in user:
111 | api_list.append(str(i))
112 | if api_list == []:
113 | abort(404)
114 | else:
115 | db_tweet.insert(new_tweet)
116 | return "Success"
117 |
118 | def upd_user(user):
119 | api_list=[]
120 | print (user)
121 | db_user = connection.app.users
122 | users = db_user.find_one({"id":user['id']})
123 | for i in users:
124 | api_list.append(str(i))
125 | if api_list == []:
126 | abort(409)
127 | else:
128 | db_user.update({'id':user['id']},{'$set': user}, upsert=False )
129 | return "Success"
130 |
--------------------------------------------------------------------------------
/Chapter07/requests.py:
--------------------------------------------------------------------------------
1 | from flask import Flask, render_template, request, jsonify, redirect, session
2 | from flask import abort
3 | from flask_cors import CORS, cross_origin
4 | from flask import make_response, url_for
5 | import json
6 | import random
7 | from pymongo import MongoClient
8 | from time import gmtime, strftime
9 |
10 | # connection to MongoDB Database
11 | connection = MongoClient("mongodb://localhost:27017/")
12 |
13 | class Requests:
14 | # List users
15 | def list_users():
16 | api_list=[]
17 | db = connection.app.users
18 | for row in db.find():
19 | api_list.append(str(row))
20 | # print (api_list)
21 | return jsonify({'user_list': api_list})
22 |
23 | # List specific users
24 | def list_user(user_id):
25 | print (user_id)
26 | api_list=[]
27 | db = connection.app.users
28 | for i in db.find({'id':user_id}):
29 | api_list.append(str(i))
30 |
31 | if api_list == []:
32 | abort(404)
33 | return jsonify({'user_details':api_list})
34 |
35 | # List specific tweet
36 | def list_tweet(user_id):
37 | print (user_id)
38 | db = connection.app.tweets
39 | api_list=[]
40 | tweet = db.find({'tweetedby':user_id})
41 | for row in tweet:
42 | dict = {}
43 | dict['id'] = row['id']
44 | dict['timestamp'] = row['timestamp']
45 | dict['tweetedby'] = row['tweetedby']
46 | dict['body'] = row['body']
47 | api_list.append(dict)
48 | if api_list == []:
49 | abort(404)
50 | print (api_list)
51 | return json.dumps(api_list)
52 |
53 | # Adding user
54 | def add_user(new_user):
55 | api_list=[]
56 | print (new_user)
57 | db = connection.app.users
58 | user = db.find({'$or':[{"username":new_user['username']} ,{"email":new_user['email']}]})
59 | for i in user:
60 | print (str(i))
61 | api_list.append(str(i))
62 |
63 | # print (api_list)
64 | if api_list == []:
65 | # print(new_user)
66 | db.insert(new_user)
67 | return "Success"
68 | else :
69 | abort(409)
70 |
71 | # Deleting User
72 | def del_user(del_user):
73 | db = connection.app.users
74 | api_list=[]
75 | for i in db.find({'username':del_user}):
76 | api_list.append(str(i))
77 |
78 | if api_list == []:
79 | abort(404)
80 | else:
81 | db.remove({"username":del_user})
82 | return "Success"
83 |
84 | # List tweets
85 | def list_tweets():
86 |
87 | api_list=[]
88 | dict = {}
89 | db = connection.app.tweets
90 | for row in db.find():
91 | print (row)
92 | dict = {}
93 | dict['id'] = row['id']
94 | dict['timestamp'] = row['timestamp']
95 | dict['tweetedby'] = row['tweetedby']
96 | dict['body'] = row['body']
97 | api_list.append(dict)
98 |
99 | print (api_list)
100 | return json.dumps(api_list)
101 |
102 | # Adding tweets
103 | def add_tweet(new_tweet):
104 | api_list=[]
105 | print (new_tweet)
106 | db_user = connection.app.users
107 | db_tweet = connection.app.tweets
108 |
109 | user = db_user.find({"username":new_tweet['tweetedby']})
110 | for i in user:
111 | api_list.append(str(i))
112 | if api_list == []:
113 | abort(404)
114 | else:
115 | db_tweet.insert(new_tweet)
116 | return "Success"
117 |
118 | def upd_user(user):
119 | api_list=[]
120 | print (user)
121 | db_user = connection.app.users
122 | users = db_user.find_one({"id":user['id']})
123 | for i in users:
124 | api_list.append(str(i))
125 | if api_list == []:
126 | abort(409)
127 | else:
128 | db_user.update({'id':user['id']},{'$set': user}, upsert=False )
129 | return "Success"
130 |
--------------------------------------------------------------------------------
/Chapter10/app/cloud-native-app/requests.py:
--------------------------------------------------------------------------------
1 | from flask import Flask, render_template, request, jsonify, redirect, session
2 | from flask import abort
3 | from flask_cors import CORS, cross_origin
4 | from flask import make_response, url_for
5 | import json
6 | import random
7 | from pymongo import MongoClient
8 | from time import gmtime, strftime
9 |
10 | # connection to MongoDB Database
11 | connection = MongoClient("mongodb://localhost:27017/")
12 |
13 | class Requests:
14 | # List users
15 | def list_users():
16 | api_list=[]
17 | db = connection.app.users
18 | for row in db.find():
19 | api_list.append(str(row))
20 | # print (api_list)
21 | return jsonify({'user_list': api_list})
22 |
23 | # List specific users
24 | def list_user(user_id):
25 | print (user_id)
26 | api_list=[]
27 | db = connection.app.users
28 | for i in db.find({'id':user_id}):
29 | api_list.append(str(i))
30 |
31 | if api_list == []:
32 | abort(404)
33 | return jsonify({'user_details':api_list})
34 |
35 | # List specific tweet
36 | def list_tweet(user_id):
37 | print (user_id)
38 | db = connection.app.tweets
39 | api_list=[]
40 | tweet = db.find({'tweetedby':user_id})
41 | for row in tweet:
42 | dict = {}
43 | dict['id'] = row['id']
44 | dict['timestamp'] = row['timestamp']
45 | dict['tweetedby'] = row['tweetedby']
46 | dict['body'] = row['body']
47 | api_list.append(dict)
48 | if api_list == []:
49 | abort(404)
50 | print (api_list)
51 | return json.dumps(api_list)
52 |
53 | # Adding user
54 | def add_user(new_user):
55 | api_list=[]
56 | print (new_user)
57 | db = connection.app.users
58 | user = db.find({'$or':[{"username":new_user['username']} ,{"email":new_user['email']}]})
59 | for i in user:
60 | print (str(i))
61 | api_list.append(str(i))
62 |
63 | # print (api_list)
64 | if api_list == []:
65 | # print(new_user)
66 | db.insert(new_user)
67 | return "Success"
68 | else :
69 | abort(409)
70 |
71 | # Deleting User
72 | def del_user(del_user):
73 | db = connection.app.users
74 | api_list=[]
75 | for i in db.find({'username':del_user}):
76 | api_list.append(str(i))
77 |
78 | if api_list == []:
79 | abort(404)
80 | else:
81 | db.remove({"username":del_user})
82 | return "Success"
83 |
84 | # List tweets
85 | def list_tweets():
86 |
87 | api_list=[]
88 | dict = {}
89 | db = connection.app.tweets
90 | for row in db.find():
91 | print (row)
92 | dict = {}
93 | dict['id'] = row['id']
94 | dict['timestamp'] = row['timestamp']
95 | dict['tweetedby'] = row['tweetedby']
96 | dict['body'] = row['body']
97 | api_list.append(dict)
98 |
99 | print (api_list)
100 | return json.dumps(api_list)
101 |
102 | # Adding tweets
103 | def add_tweet(new_tweet):
104 | api_list=[]
105 | print (new_tweet)
106 | db_user = connection.app.users
107 | db_tweet = connection.app.tweets
108 |
109 | user = db_user.find({"username":new_tweet['tweetedby']})
110 | for i in user:
111 | api_list.append(str(i))
112 | if api_list == []:
113 | abort(404)
114 | else:
115 | db_tweet.insert(new_tweet)
116 | return "Success"
117 |
118 | def upd_user(user):
119 | api_list=[]
120 | print (user)
121 | db_user = connection.app.users
122 | users = db_user.find_one({"id":user['id']})
123 | for i in users:
124 | api_list.append(str(i))
125 | if api_list == []:
126 | abort(409)
127 | else:
128 | db_user.update({'id':user['id']},{'$set': user}, upsert=False )
129 | return "Success"
130 |
--------------------------------------------------------------------------------
/Chapter08/request.py:
--------------------------------------------------------------------------------
1 | from flask import Flask, render_template, request, jsonify, redirect, session
2 | from flask import abort
3 | from flask_cors import CORS, cross_origin
4 | from flask import make_response, url_for
5 | import json
6 | import random
7 | from pymongo import MongoClient
8 | from time import gmtime, strftime
9 |
10 | # connection to MongoDB Database
11 | connection = MongoClient("mongodb://localhost:27017/")
12 |
13 | class Requests:
14 | # List users
15 | def list_users():
16 | api_list=[]
17 | db = connection.app.users
18 | for row in db.find():
19 | api_list.append(str(row))
20 | # print (api_list)
21 | return jsonify({'user_list': api_list})
22 |
23 | # List specific users
24 | def list_user(user_id):
25 | print (user_id)
26 | api_list=[]
27 | db = connection.app.users
28 | for i in db.find({'id':user_id}):
29 | api_list.append(str(i))
30 |
31 | if api_list == []:
32 | abort(404)
33 | return jsonify({'user_details':api_list})
34 |
35 | # List specific tweet
36 | def list_tweet(user_id):
37 | print (user_id)
38 | db = connection.app.tweets
39 | api_list=[]
40 | tweet = db.find({'tweetedby':user_id})
41 | for row in tweet:
42 | dict = {}
43 | dict['id'] = row['id']
44 | dict['timestamp'] = row['timestamp']
45 | dict['tweetedby'] = row['tweetedby']
46 | dict['body'] = row['body']
47 | api_list.append(dict)
48 | if api_list == []:
49 | abort(404)
50 | print (api_list)
51 | return json.dumps(api_list)
52 |
53 | # Adding user
54 | def add_user(new_user):
55 | # api_list=[]
56 | # print (new_user)
57 | # db = connection.app.users
58 | # user = db.find({'$or':[{"username":new_user['username']} ,{"email":new_user['email']}]})
59 | # for i in user:
60 | # print (str(i))
61 | # api_list.append(str(i))
62 | #
63 | # # print (api_list)
64 | # if api_list == []:
65 | # # print(new_user)
66 | db.insert(new_user)
67 | return "Success"
68 | # else :
69 | # abort(409)
70 |
71 | # Deleting User
72 | def del_user(del_user):
73 | db = connection.app.users
74 | api_list=[]
75 | for i in db.find({'username':del_user}):
76 | api_list.append(str(i))
77 |
78 | if api_list == []:
79 | abort(404)
80 | else:
81 | db.remove({"username":del_user})
82 | return "Success"
83 |
84 | # List tweets
85 | def list_tweets():
86 |
87 | api_list=[]
88 | dict = {}
89 | db = connection.app.tweets
90 | for row in db.find():
91 | print (row)
92 | dict = {}
93 | dict['id'] = row['id']
94 | dict['timestamp'] = row['timestamp']
95 | dict['tweetedby'] = row['tweetedby']
96 | dict['body'] = row['body']
97 | api_list.append(dict)
98 |
99 | print (api_list)
100 | return json.dumps(api_list)
101 |
102 | # Adding tweets
103 | def add_tweet(new_tweet):
104 | api_list=[]
105 | print (new_tweet)
106 | db_user = connection.app.users
107 | db_tweet = connection.app.tweets
108 |
109 | user = db_user.find({"username":new_tweet['tweetedby']})
110 | for i in user:
111 | api_list.append(str(i))
112 | if api_list == []:
113 | abort(404)
114 | else:
115 | db_tweet.insert(new_tweet)
116 | return "Success"
117 |
118 | def upd_user(user):
119 | api_list=[]
120 | print (user)
121 | db_user = connection.app.users
122 | users = db_user.find_one({"id":user['id']})
123 | for i in users:
124 | api_list.append(str(i))
125 | if api_list == []:
126 | abort(409)
127 | else:
128 | db_user.update({'id':user['id']},{'$set': user}, upsert=False )
129 | return "Success"
130 |
--------------------------------------------------------------------------------
/Chapter05/static/style.css:
--------------------------------------------------------------------------------
1 | ::selection{ background-color: #E13300; color: white; }
2 | ::moz-selection{ background-color: #E13300; color: white; }
3 | ::webkit-selection{ background-color: #E13300; color: white; }
4 |
5 | body {
6 | font-family: "Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;
7 | color: #888;
8 | font-style: normal;
9 | font-size: 14px;
10 | line-height: px;
11 | }
12 |
13 | a {
14 | color: #003399;
15 | background-color: transparent;
16 | font-weight: normal;
17 | }
18 |
19 | .title {
20 | padding: 0 20px;
21 | background: #32373a;
22 | position: relative;
23 | height: 50px;
24 | }
25 |
26 | h1 {
27 | color: #fff;
28 | margin: 0;
29 | font-size: 18px;
30 | font-weight: 400;
31 | line-height: 50px;
32 | text-transform: capitalize;
33 | }
34 |
35 | code {
36 | font-family: Consolas, Monaco, Courier New, Courier, monospace;
37 | font-size: 12px;
38 | background-color: #f9f9f9;
39 | border: 1px solid #D0D0D0;
40 | color: #002166;
41 | display: block;
42 | padding: 12px 10px 12px 10px;
43 | }
44 |
45 | #content {
46 | padding: 20px;
47 | zoom: 1;
48 | }
49 |
50 | #body{
51 | margin: 0 15px 0 15px;
52 | }
53 |
54 | p.footer{
55 | text-align: right;
56 | font-size: 11px;
57 | border-top: 1px solid #D0D0D0;
58 | line-height: 32px;
59 | padding: 0 10px 0 10px;
60 | margin: 20px 0 0 0;
61 | }
62 |
63 | #container{
64 | margin: 10px;
65 | border: 1px solid #D0D0D0;
66 | -webkit-box-shadow: 0 0 8px #D0D0D0;
67 | }
68 |
69 | /* Form Styles */
70 | form label{
71 | display: block;
72 | margin-bottom: 5px;
73 | }
74 | form input[type=text]{
75 | border: 1px solid #aaa;
76 | border-radius: 2px;
77 | box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
78 | box-sizing: border-box;
79 | padding: 5px;
80 | margin-bottom: 20px;
81 | outline: 0;
82 | -webkit-border-radius: 2px;
83 | -moz-border-radius: 2px;
84 | -webkit-box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
85 | -moz-box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
86 | }
87 | form input[type=submit]{
88 | background: #198ad1;
89 | color: #fff;
90 | display: inline-block;
91 | padding: 0 20px;
92 | font-size: 16px;
93 | line-height: 2.6em;
94 | font-family: "Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;
95 | font-weight: 400;
96 | -webkit-box-shadow: inset 0 -1px 0 rgba(0,0,0,.1);
97 | -moz-box-shadow: inset 0 -1px 0 rgba(0,0,0,.1);
98 | box-shadow: inset 0 -1px 0 rgba(0,0,0,.1);
99 | -webkit-border-radius: 2px;
100 | -moz-border-radius: 2px;
101 | border-radius: 2px;
102 | text-transform: capitalize;
103 | border: 0;
104 | cursor: pointer;
105 | text-align: center;
106 | }
107 |
108 | * {
109 | box-sizing: border-box;
110 | }
111 |
112 | *:focus {
113 | outline: none;
114 | }
115 | body {
116 | font-family: Arial;
117 | background-color: #3498DB;
118 | padding: 50px;
119 | }
120 | .login {
121 | margin: 20px auto;
122 | width: 300px;
123 | }
124 | .login-screen {
125 | background-color: #FFF;
126 | padding: 20px;
127 | border-radius: 5px
128 | }
129 |
130 | .app-title {
131 | text-align: center;
132 | color: #777;
133 | }
134 |
135 | .login-form {
136 | text-align: center;
137 | }
138 | .control-group {
139 | margin-bottom: 10px;
140 | }
141 |
142 | input {
143 | text-align: center;
144 | background-color: #ECF0F1;
145 | border: 2px solid transparent;
146 | border-radius: 3px;
147 | font-size: 16px;
148 | font-weight: 200;
149 | padding: 10px 0;
150 | width: 250px;
151 | transition: border .5s;
152 | }
153 |
154 | input:focus {
155 | border: 2px solid #3498DB;
156 | box-shadow: none;
157 | }
158 |
159 | .btn {
160 | border: 2px solid transparent;
161 | background: #3498DB;
162 | color: #ffffff;
163 | font-size: 16px;
164 | line-height: 25px;
165 | padding: 10px 0;
166 | text-decoration: none;
167 | text-shadow: none;
168 | border-radius: 3px;
169 | box-shadow: none;
170 | transition: 0.25s;
171 | display: block;
172 | width: 250px;
173 | margin: 0 auto;
174 | }
175 |
176 | .btn:hover {
177 | background-color: #2980B9;
178 | }
179 |
180 | .login-link {
181 | font-size: 12px;
182 | color: #444;
183 | display: block;
184 | margin-top: 12px;
185 | }
186 |
--------------------------------------------------------------------------------
/Chapter06/static/style.css:
--------------------------------------------------------------------------------
1 | ::selection{ background-color: #E13300; color: white; }
2 | ::moz-selection{ background-color: #E13300; color: white; }
3 | ::webkit-selection{ background-color: #E13300; color: white; }
4 |
5 | body {
6 | font-family: "Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;
7 | color: #888;
8 | font-style: normal;
9 | font-size: 14px;
10 | line-height: px;
11 | }
12 |
13 | a {
14 | color: #003399;
15 | background-color: transparent;
16 | font-weight: normal;
17 | }
18 |
19 | .title {
20 | padding: 0 20px;
21 | background: #32373a;
22 | position: relative;
23 | height: 50px;
24 | }
25 |
26 | h1 {
27 | color: #fff;
28 | margin: 0;
29 | font-size: 18px;
30 | font-weight: 400;
31 | line-height: 50px;
32 | text-transform: capitalize;
33 | }
34 |
35 | code {
36 | font-family: Consolas, Monaco, Courier New, Courier, monospace;
37 | font-size: 12px;
38 | background-color: #f9f9f9;
39 | border: 1px solid #D0D0D0;
40 | color: #002166;
41 | display: block;
42 | padding: 12px 10px 12px 10px;
43 | }
44 |
45 | #content {
46 | padding: 20px;
47 | zoom: 1;
48 | }
49 |
50 | #body{
51 | margin: 0 15px 0 15px;
52 | }
53 |
54 | p.footer{
55 | text-align: right;
56 | font-size: 11px;
57 | border-top: 1px solid #D0D0D0;
58 | line-height: 32px;
59 | padding: 0 10px 0 10px;
60 | margin: 20px 0 0 0;
61 | }
62 |
63 | #container{
64 | margin: 10px;
65 | border: 1px solid #D0D0D0;
66 | -webkit-box-shadow: 0 0 8px #D0D0D0;
67 | }
68 |
69 | /* Form Styles */
70 | form label{
71 | display: block;
72 | margin-bottom: 5px;
73 | }
74 | form input[type=text]{
75 | border: 1px solid #aaa;
76 | border-radius: 2px;
77 | box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
78 | box-sizing: border-box;
79 | padding: 5px;
80 | margin-bottom: 20px;
81 | outline: 0;
82 | -webkit-border-radius: 2px;
83 | -moz-border-radius: 2px;
84 | -webkit-box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
85 | -moz-box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
86 | }
87 | form input[type=submit]{
88 | background: #198ad1;
89 | color: #fff;
90 | display: inline-block;
91 | padding: 0 20px;
92 | font-size: 16px;
93 | line-height: 2.6em;
94 | font-family: "Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;
95 | font-weight: 400;
96 | -webkit-box-shadow: inset 0 -1px 0 rgba(0,0,0,.1);
97 | -moz-box-shadow: inset 0 -1px 0 rgba(0,0,0,.1);
98 | box-shadow: inset 0 -1px 0 rgba(0,0,0,.1);
99 | -webkit-border-radius: 2px;
100 | -moz-border-radius: 2px;
101 | border-radius: 2px;
102 | text-transform: capitalize;
103 | border: 0;
104 | cursor: pointer;
105 | text-align: center;
106 | }
107 |
108 | * {
109 | box-sizing: border-box;
110 | }
111 |
112 | *:focus {
113 | outline: none;
114 | }
115 | body {
116 | font-family: Arial;
117 | background-color: #3498DB;
118 | padding: 50px;
119 | }
120 | .login {
121 | margin: 20px auto;
122 | width: 300px;
123 | }
124 | .login-screen {
125 | background-color: #FFF;
126 | padding: 20px;
127 | border-radius: 5px
128 | }
129 |
130 | .app-title {
131 | text-align: center;
132 | color: #777;
133 | }
134 |
135 | .login-form {
136 | text-align: center;
137 | }
138 | .control-group {
139 | margin-bottom: 10px;
140 | }
141 |
142 | input {
143 | text-align: center;
144 | background-color: #ECF0F1;
145 | border: 2px solid transparent;
146 | border-radius: 3px;
147 | font-size: 16px;
148 | font-weight: 200;
149 | padding: 10px 0;
150 | width: 250px;
151 | transition: border .5s;
152 | }
153 |
154 | input:focus {
155 | border: 2px solid #3498DB;
156 | box-shadow: none;
157 | }
158 |
159 | .btn {
160 | border: 2px solid transparent;
161 | background: #3498DB;
162 | color: #ffffff;
163 | font-size: 16px;
164 | line-height: 25px;
165 | padding: 10px 0;
166 | text-decoration: none;
167 | text-shadow: none;
168 | border-radius: 3px;
169 | box-shadow: none;
170 | transition: 0.25s;
171 | display: block;
172 | width: 250px;
173 | margin: 0 auto;
174 | }
175 |
176 | .btn:hover {
177 | background-color: #2980B9;
178 | }
179 |
180 | .login-link {
181 | font-size: 12px;
182 | color: #444;
183 | display: block;
184 | margin-top: 12px;
185 | }
186 |
--------------------------------------------------------------------------------
/Chapter07/static/style.css:
--------------------------------------------------------------------------------
1 | ::selection{ background-color: #E13300; color: white; }
2 | ::moz-selection{ background-color: #E13300; color: white; }
3 | ::webkit-selection{ background-color: #E13300; color: white; }
4 |
5 | body {
6 | font-family: "Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;
7 | color: #888;
8 | font-style: normal;
9 | font-size: 14px;
10 | line-height: px;
11 | }
12 |
13 | a {
14 | color: #003399;
15 | background-color: transparent;
16 | font-weight: normal;
17 | }
18 |
19 | .title {
20 | padding: 0 20px;
21 | background: #32373a;
22 | position: relative;
23 | height: 50px;
24 | }
25 |
26 | h1 {
27 | color: #fff;
28 | margin: 0;
29 | font-size: 18px;
30 | font-weight: 400;
31 | line-height: 50px;
32 | text-transform: capitalize;
33 | }
34 |
35 | code {
36 | font-family: Consolas, Monaco, Courier New, Courier, monospace;
37 | font-size: 12px;
38 | background-color: #f9f9f9;
39 | border: 1px solid #D0D0D0;
40 | color: #002166;
41 | display: block;
42 | padding: 12px 10px 12px 10px;
43 | }
44 |
45 | #content {
46 | padding: 20px;
47 | zoom: 1;
48 | }
49 |
50 | #body{
51 | margin: 0 15px 0 15px;
52 | }
53 |
54 | p.footer{
55 | text-align: right;
56 | font-size: 11px;
57 | border-top: 1px solid #D0D0D0;
58 | line-height: 32px;
59 | padding: 0 10px 0 10px;
60 | margin: 20px 0 0 0;
61 | }
62 |
63 | #container{
64 | margin: 10px;
65 | border: 1px solid #D0D0D0;
66 | -webkit-box-shadow: 0 0 8px #D0D0D0;
67 | }
68 |
69 | /* Form Styles */
70 | form label{
71 | display: block;
72 | margin-bottom: 5px;
73 | }
74 | form input[type=text]{
75 | border: 1px solid #aaa;
76 | border-radius: 2px;
77 | box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
78 | box-sizing: border-box;
79 | padding: 5px;
80 | margin-bottom: 20px;
81 | outline: 0;
82 | -webkit-border-radius: 2px;
83 | -moz-border-radius: 2px;
84 | -webkit-box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
85 | -moz-box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
86 | }
87 | form input[type=submit]{
88 | background: #198ad1;
89 | color: #fff;
90 | display: inline-block;
91 | padding: 0 20px;
92 | font-size: 16px;
93 | line-height: 2.6em;
94 | font-family: "Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;
95 | font-weight: 400;
96 | -webkit-box-shadow: inset 0 -1px 0 rgba(0,0,0,.1);
97 | -moz-box-shadow: inset 0 -1px 0 rgba(0,0,0,.1);
98 | box-shadow: inset 0 -1px 0 rgba(0,0,0,.1);
99 | -webkit-border-radius: 2px;
100 | -moz-border-radius: 2px;
101 | border-radius: 2px;
102 | text-transform: capitalize;
103 | border: 0;
104 | cursor: pointer;
105 | text-align: center;
106 | }
107 |
108 | * {
109 | box-sizing: border-box;
110 | }
111 |
112 | *:focus {
113 | outline: none;
114 | }
115 | body {
116 | font-family: Arial;
117 | background-color: #3498DB;
118 | padding: 50px;
119 | }
120 | .login {
121 | margin: 20px auto;
122 | width: 300px;
123 | }
124 | .login-screen {
125 | background-color: #FFF;
126 | padding: 20px;
127 | border-radius: 5px
128 | }
129 |
130 | .app-title {
131 | text-align: center;
132 | color: #777;
133 | }
134 |
135 | .login-form {
136 | text-align: center;
137 | }
138 | .control-group {
139 | margin-bottom: 10px;
140 | }
141 |
142 | input {
143 | text-align: center;
144 | background-color: #ECF0F1;
145 | border: 2px solid transparent;
146 | border-radius: 3px;
147 | font-size: 16px;
148 | font-weight: 200;
149 | padding: 10px 0;
150 | width: 250px;
151 | transition: border .5s;
152 | }
153 |
154 | input:focus {
155 | border: 2px solid #3498DB;
156 | box-shadow: none;
157 | }
158 |
159 | .btn {
160 | border: 2px solid transparent;
161 | background: #3498DB;
162 | color: #ffffff;
163 | font-size: 16px;
164 | line-height: 25px;
165 | padding: 10px 0;
166 | text-decoration: none;
167 | text-shadow: none;
168 | border-radius: 3px;
169 | box-shadow: none;
170 | transition: 0.25s;
171 | display: block;
172 | width: 250px;
173 | margin: 0 auto;
174 | }
175 |
176 | .btn:hover {
177 | background-color: #2980B9;
178 | }
179 |
180 | .login-link {
181 | font-size: 12px;
182 | color: #444;
183 | display: block;
184 | margin-top: 12px;
185 | }
186 |
--------------------------------------------------------------------------------
/Chapter08/static/style.css:
--------------------------------------------------------------------------------
1 | ::selection{ background-color: #E13300; color: white; }
2 | ::moz-selection{ background-color: #E13300; color: white; }
3 | ::webkit-selection{ background-color: #E13300; color: white; }
4 |
5 | body {
6 | font-family: "Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;
7 | color: #888;
8 | font-style: normal;
9 | font-size: 14px;
10 | line-height: px;
11 | }
12 |
13 | a {
14 | color: #003399;
15 | background-color: transparent;
16 | font-weight: normal;
17 | }
18 |
19 | .title {
20 | padding: 0 20px;
21 | background: #32373a;
22 | position: relative;
23 | height: 50px;
24 | }
25 |
26 | h1 {
27 | color: #fff;
28 | margin: 0;
29 | font-size: 18px;
30 | font-weight: 400;
31 | line-height: 50px;
32 | text-transform: capitalize;
33 | }
34 |
35 | code {
36 | font-family: Consolas, Monaco, Courier New, Courier, monospace;
37 | font-size: 12px;
38 | background-color: #f9f9f9;
39 | border: 1px solid #D0D0D0;
40 | color: #002166;
41 | display: block;
42 | padding: 12px 10px 12px 10px;
43 | }
44 |
45 | #content {
46 | padding: 20px;
47 | zoom: 1;
48 | }
49 |
50 | #body{
51 | margin: 0 15px 0 15px;
52 | }
53 |
54 | p.footer{
55 | text-align: right;
56 | font-size: 11px;
57 | border-top: 1px solid #D0D0D0;
58 | line-height: 32px;
59 | padding: 0 10px 0 10px;
60 | margin: 20px 0 0 0;
61 | }
62 |
63 | #container{
64 | margin: 10px;
65 | border: 1px solid #D0D0D0;
66 | -webkit-box-shadow: 0 0 8px #D0D0D0;
67 | }
68 |
69 | /* Form Styles */
70 | form label{
71 | display: block;
72 | margin-bottom: 5px;
73 | }
74 | form input[type=text]{
75 | border: 1px solid #aaa;
76 | border-radius: 2px;
77 | box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
78 | box-sizing: border-box;
79 | padding: 5px;
80 | margin-bottom: 20px;
81 | outline: 0;
82 | -webkit-border-radius: 2px;
83 | -moz-border-radius: 2px;
84 | -webkit-box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
85 | -moz-box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
86 | }
87 | form input[type=submit]{
88 | background: #198ad1;
89 | color: #fff;
90 | display: inline-block;
91 | padding: 0 20px;
92 | font-size: 16px;
93 | line-height: 2.6em;
94 | font-family: "Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;
95 | font-weight: 400;
96 | -webkit-box-shadow: inset 0 -1px 0 rgba(0,0,0,.1);
97 | -moz-box-shadow: inset 0 -1px 0 rgba(0,0,0,.1);
98 | box-shadow: inset 0 -1px 0 rgba(0,0,0,.1);
99 | -webkit-border-radius: 2px;
100 | -moz-border-radius: 2px;
101 | border-radius: 2px;
102 | text-transform: capitalize;
103 | border: 0;
104 | cursor: pointer;
105 | text-align: center;
106 | }
107 |
108 | * {
109 | box-sizing: border-box;
110 | }
111 |
112 | *:focus {
113 | outline: none;
114 | }
115 | body {
116 | font-family: Arial;
117 | background-color: #3498DB;
118 | padding: 50px;
119 | }
120 | .login {
121 | margin: 20px auto;
122 | width: 300px;
123 | }
124 | .login-screen {
125 | background-color: #FFF;
126 | padding: 20px;
127 | border-radius: 5px
128 | }
129 |
130 | .app-title {
131 | text-align: center;
132 | color: #777;
133 | }
134 |
135 | .login-form {
136 | text-align: center;
137 | }
138 | .control-group {
139 | margin-bottom: 10px;
140 | }
141 |
142 | input {
143 | text-align: center;
144 | background-color: #ECF0F1;
145 | border: 2px solid transparent;
146 | border-radius: 3px;
147 | font-size: 16px;
148 | font-weight: 200;
149 | padding: 10px 0;
150 | width: 250px;
151 | transition: border .5s;
152 | }
153 |
154 | input:focus {
155 | border: 2px solid #3498DB;
156 | box-shadow: none;
157 | }
158 |
159 | .btn {
160 | border: 2px solid transparent;
161 | background: #3498DB;
162 | color: #ffffff;
163 | font-size: 16px;
164 | line-height: 25px;
165 | padding: 10px 0;
166 | text-decoration: none;
167 | text-shadow: none;
168 | border-radius: 3px;
169 | box-shadow: none;
170 | transition: 0.25s;
171 | display: block;
172 | width: 250px;
173 | margin: 0 auto;
174 | }
175 |
176 | .btn:hover {
177 | background-color: #2980B9;
178 | }
179 |
180 | .login-link {
181 | font-size: 12px;
182 | color: #444;
183 | display: block;
184 | margin-top: 12px;
185 | }
186 |
--------------------------------------------------------------------------------
/Chapter10/app/cloud-native-app/static/style.css:
--------------------------------------------------------------------------------
1 | ::selection{ background-color: #E13300; color: white; }
2 | ::moz-selection{ background-color: #E13300; color: white; }
3 | ::webkit-selection{ background-color: #E13300; color: white; }
4 |
5 | body {
6 | font-family: "Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;
7 | color: #888;
8 | font-style: normal;
9 | font-size: 14px;
10 | line-height: px;
11 | }
12 |
13 | a {
14 | color: #003399;
15 | background-color: transparent;
16 | font-weight: normal;
17 | }
18 |
19 | .title {
20 | padding: 0 20px;
21 | background: #32373a;
22 | position: relative;
23 | height: 50px;
24 | }
25 |
26 | h1 {
27 | color: #fff;
28 | margin: 0;
29 | font-size: 18px;
30 | font-weight: 400;
31 | line-height: 50px;
32 | text-transform: capitalize;
33 | }
34 |
35 | code {
36 | font-family: Consolas, Monaco, Courier New, Courier, monospace;
37 | font-size: 12px;
38 | background-color: #f9f9f9;
39 | border: 1px solid #D0D0D0;
40 | color: #002166;
41 | display: block;
42 | padding: 12px 10px 12px 10px;
43 | }
44 |
45 | #content {
46 | padding: 20px;
47 | zoom: 1;
48 | }
49 |
50 | #body{
51 | margin: 0 15px 0 15px;
52 | }
53 |
54 | p.footer{
55 | text-align: right;
56 | font-size: 11px;
57 | border-top: 1px solid #D0D0D0;
58 | line-height: 32px;
59 | padding: 0 10px 0 10px;
60 | margin: 20px 0 0 0;
61 | }
62 |
63 | #container{
64 | margin: 10px;
65 | border: 1px solid #D0D0D0;
66 | -webkit-box-shadow: 0 0 8px #D0D0D0;
67 | }
68 |
69 | /* Form Styles */
70 | form label{
71 | display: block;
72 | margin-bottom: 5px;
73 | }
74 | form input[type=text]{
75 | border: 1px solid #aaa;
76 | border-radius: 2px;
77 | box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
78 | box-sizing: border-box;
79 | padding: 5px;
80 | margin-bottom: 20px;
81 | outline: 0;
82 | -webkit-border-radius: 2px;
83 | -moz-border-radius: 2px;
84 | -webkit-box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
85 | -moz-box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
86 | }
87 | form input[type=submit]{
88 | background: #198ad1;
89 | color: #fff;
90 | display: inline-block;
91 | padding: 0 20px;
92 | font-size: 16px;
93 | line-height: 2.6em;
94 | font-family: "Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;
95 | font-weight: 400;
96 | -webkit-box-shadow: inset 0 -1px 0 rgba(0,0,0,.1);
97 | -moz-box-shadow: inset 0 -1px 0 rgba(0,0,0,.1);
98 | box-shadow: inset 0 -1px 0 rgba(0,0,0,.1);
99 | -webkit-border-radius: 2px;
100 | -moz-border-radius: 2px;
101 | border-radius: 2px;
102 | text-transform: capitalize;
103 | border: 0;
104 | cursor: pointer;
105 | text-align: center;
106 | }
107 |
108 | * {
109 | box-sizing: border-box;
110 | }
111 |
112 | *:focus {
113 | outline: none;
114 | }
115 | body {
116 | font-family: Arial;
117 | background-color: #3498DB;
118 | padding: 50px;
119 | }
120 | .login {
121 | margin: 20px auto;
122 | width: 300px;
123 | }
124 | .login-screen {
125 | background-color: #FFF;
126 | padding: 20px;
127 | border-radius: 5px
128 | }
129 |
130 | .app-title {
131 | text-align: center;
132 | color: #777;
133 | }
134 |
135 | .login-form {
136 | text-align: center;
137 | }
138 | .control-group {
139 | margin-bottom: 10px;
140 | }
141 |
142 | input {
143 | text-align: center;
144 | background-color: #ECF0F1;
145 | border: 2px solid transparent;
146 | border-radius: 3px;
147 | font-size: 16px;
148 | font-weight: 200;
149 | padding: 10px 0;
150 | width: 250px;
151 | transition: border .5s;
152 | }
153 |
154 | input:focus {
155 | border: 2px solid #3498DB;
156 | box-shadow: none;
157 | }
158 |
159 | .btn {
160 | border: 2px solid transparent;
161 | background: #3498DB;
162 | color: #ffffff;
163 | font-size: 16px;
164 | line-height: 25px;
165 | padding: 10px 0;
166 | text-decoration: none;
167 | text-shadow: none;
168 | border-radius: 3px;
169 | box-shadow: none;
170 | transition: 0.25s;
171 | display: block;
172 | width: 250px;
173 | margin: 0 auto;
174 | }
175 |
176 | .btn:hover {
177 | background-color: #2980B9;
178 | }
179 |
180 | .login-link {
181 | font-size: 12px;
182 | color: #444;
183 | display: block;
184 | margin-top: 12px;
185 | }
186 |
--------------------------------------------------------------------------------
/Chapter11/terraform-app/main.tf:
--------------------------------------------------------------------------------
1 | #Author: Manish Sethi
2 | # Specify the provider and access details
3 | provider "aws" {
4 | region = "${var.aws_region}"
5 | access_key = "${var.aws_access_key}"
6 | secret_key = "${var.aws_secret_key}"
7 | }
8 |
9 | # Create a VPC to launch our instances into
10 | resource "aws_vpc" "default" {
11 | cidr_block = "${var.vpc_cidr}"
12 | enable_dns_hostnames = true
13 | tags {
14 | Name = "ms-cloud-native-app"
15 | }
16 | }
17 |
18 | # Create an internet gateway to give our subnet access to the outside world
19 | resource "aws_internet_gateway" "default" {
20 | vpc_id = "${aws_vpc.default.id}"
21 | }
22 |
23 | # Grant the VPC internet access on its main route table
24 | resource "aws_route" "internet_access" {
25 | route_table_id = "${aws_vpc.default.main_route_table_id}"
26 | destination_cidr_block = "0.0.0.0/0"
27 | gateway_id = "${aws_internet_gateway.default.id}"
28 | }
29 |
30 | # Create a subnet to launch our instances into
31 | resource "aws_subnet" "default" {
32 | vpc_id = "${aws_vpc.default.id}"
33 | cidr_block = "${var.subnet_cidr}"
34 | map_public_ip_on_launch = true
35 | }
36 |
37 |
38 | # A security group for the ELB so it is accessible via the web
39 | resource "aws_security_group" "elb" {
40 | name = "cna_sg_elb"
41 | description = "Security_group_elb"
42 | vpc_id = "${aws_vpc.default.id}"
43 |
44 | # HTTP access from anywhere
45 | ingress {
46 | from_port = 5000
47 | to_port = 5000
48 | protocol = "tcp"
49 | cidr_blocks = ["0.0.0.0/0"]
50 | }
51 |
52 | # outbound internet access
53 | egress {
54 | from_port = 0
55 | to_port = 0
56 | protocol = "-1"
57 | cidr_blocks = ["0.0.0.0/0"]
58 | }
59 |
60 | }
61 |
62 | # Our default security group to access
63 | # the instances over SSH and HTTP
64 | resource "aws_security_group" "default" {
65 | name = "cna-sg-ec2"
66 | description = "Security group of app servers"
67 | vpc_id = "${aws_vpc.default.id}"
68 |
69 | # SSH access from anywhere
70 | ingress {
71 | from_port = 22
72 | to_port = 22
73 | protocol = "tcp"
74 | cidr_blocks = ["0.0.0.0/0"]
75 | }
76 |
77 | # HTTP access from the VPC
78 | ingress {
79 | from_port = 5000
80 | to_port = 5000
81 | protocol = "tcp"
82 | cidr_blocks = ["${var.vpc_cidr}"]
83 | }
84 |
85 | # outbound internet access
86 | egress {
87 | from_port = 0
88 | to_port = 0
89 | protocol = "-1"
90 | cidr_blocks = ["0.0.0.0/0"]
91 | }
92 | }
93 |
94 | resource "aws_security_group" "mongodb" {
95 | name = "cna-sg-mongodb"
96 | description = "Security group of mongodb server"
97 | vpc_id = "${aws_vpc.default.id}"
98 |
99 | # SSH access from anywhere
100 | ingress {
101 | from_port = 22
102 | to_port = 22
103 | protocol = "tcp"
104 | cidr_blocks = ["0.0.0.0/0"]
105 | }
106 |
107 | # HTTP access from the VPC
108 | ingress {
109 | from_port = 27017
110 | to_port = 27017
111 | protocol = "tcp"
112 | cidr_blocks = ["${var.vpc_cidr}"]
113 | }
114 | # HTTP access from the VPC
115 | ingress {
116 | from_port = 28017
117 | to_port = 28017
118 | protocol = "tcp"
119 | cidr_blocks = ["${var.vpc_cidr}"]
120 | }
121 |
122 | # outbound internet access
123 | egress {
124 | from_port = 0
125 | to_port = 0
126 | protocol = "-1"
127 | cidr_blocks = ["0.0.0.0/0"]
128 | }
129 | }
130 |
131 | resource "aws_elb" "web" {
132 | name = "cna-elb"
133 |
134 | subnets = ["${aws_subnet.default.id}"]
135 | security_groups = ["${aws_security_group.elb.id}"]
136 | instances = ["${aws_instance.web.*.id}"]
137 | listener {
138 | instance_port = 5000
139 | instance_protocol = "http"
140 | lb_port = 80
141 | lb_protocol = "http"
142 | }
143 | }
144 |
145 | resource "aws_key_pair" "auth" {
146 | key_name = "${var.key_name}"
147 | public_key = "${file(var.public_key_path)}"
148 | }
149 |
150 | resource "aws_instance" "web" {
151 | # The connection block tells our provisioner how to
152 | # communicate with the resource (instance)
153 | connection {
154 | # The default username for our AMI
155 | user = "ubuntu"
156 | key_file = "${var.key_file_path}"
157 | timeout = "5m"
158 | }
159 | # Tags for machine
160 | tags {Name = "cna-web"}
161 | instance_type = "t2.micro"
162 | # Number of EC2 to spin up
163 | count = "1"
164 | ami = "${lookup(var.aws_amis, var.aws_region)}"
165 | iam_instance_profile = "CodeDeploy-Instance-Role"
166 | # The name of our SSH keypair we created above.
167 | key_name = "${aws_key_pair.auth.id}"
168 |
169 | # Our Security group to allow HTTP and SSH access
170 | vpc_security_group_ids = ["${aws_security_group.default.id}"]
171 | subnet_id = "${aws_subnet.default.id}"
172 | }
173 | resource "aws_instance" "mongodb" {
174 | # The connection block tells our provisioner how to
175 | # communicate with the resource (instance)
176 | connection {
177 | # The default username for our AMI
178 | user = "ubuntu"
179 | private_key = "${file(var.key_file_path)}"
180 | timeout = "5m"
181 | # The connection will use the local SSH agent for authentication.
182 | }
183 | # Tags for machine
184 | tags {Name = "cna-web-mongodb"}
185 | instance_type = "t2.micro"
186 | # Number of EC2 to spin up
187 | count = "1"
188 | # Lookup the correct AMI based on the region
189 | # we specified
190 | ami = "${lookup(var.aws_amis, var.aws_region)}"
191 | iam_instance_profile = "CodeDeploy-Instance-Role"
192 | # The name of our SSH keypair we created above.
193 | key_name = "${aws_key_pair.auth.id}"
194 |
195 | # Our Security group to allow HTTP and SSH access
196 | vpc_security_group_ids = ["${aws_security_group.mongodb.id}"]
197 |
198 | subnet_id = "${aws_subnet.default.id}"
199 | provisioner "remote-exec" {
200 | inline = [
201 | "sudo echo -ne '\n' | apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10",
202 | "echo 'deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse' | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list",
203 | "sudo apt-get update -y && sudo apt-get install mongodb-org --force-yes -y",
204 | ]
205 | }
206 |
207 | }
208 |
--------------------------------------------------------------------------------
/Chapter02/app.py:
--------------------------------------------------------------------------------
1 | from flask import Flask, request, jsonify
2 | from flask import abort
3 | from flask import make_response, url_for
4 | import json
5 | from time import gmtime, strftime
6 | import sqlite3
7 |
8 |
9 |
10 | app = Flask(__name__)
11 |
12 | def list_users():
13 | conn = sqlite3.connect('mydb.db')
14 | print ("Opened database successfully");
15 | api_list=[]
16 | cursor = conn.execute("SELECT username, full_name, email, password, id from users")
17 | for row in cursor:
18 | a_dict = {}
19 | a_dict['username'] = row[0]
20 | a_dict['name'] = row[1]
21 | a_dict['email'] = row[2]
22 | a_dict['password'] = row[3]
23 | a_dict['id'] = row[4]
24 | api_list.append(a_dict)
25 |
26 | conn.close()
27 | return jsonify({'user_list': api_list})
28 |
29 | def list_user(user_id):
30 | conn = sqlite3.connect('mydb.db')
31 | print ("Opened database successfully");
32 | api_list=[]
33 | cursor=conn.cursor()
34 | cursor.execute("SELECT * from users where id=?",(user_id,))
35 | data = cursor.fetchall()
36 | print (data)
37 | if len(data) == 0:
38 | abort(404)
39 | else:
40 |
41 | user = {}
42 | user['username'] = data[0][0]
43 | user['name'] = data[0][1]
44 | user['email'] = data[0][2]
45 | user['password'] = data[0][3]
46 | user['id'] = data[0][4]
47 |
48 | conn.close()
49 | return jsonify(user)
50 |
51 |
52 | def list_tweet(user_id):
53 | print (user_id)
54 | conn = sqlite3.connect('mydb.db')
55 | print ("Opened database successfully");
56 | api_list=[]
57 | cursor=conn.cursor()
58 | cursor.execute("SELECT * from tweets where id=?",(user_id,))
59 | data = cursor.fetchall()
60 | print (data)
61 | if len(data) == 0:
62 | abort(404)
63 | else:
64 |
65 | user = {}
66 | user['id'] = data[0][0]
67 | user['username'] = data[0][1]
68 | user['body'] = data[0][2]
69 | user['tweet_time'] = data[0][3]
70 |
71 | conn.close()
72 | return jsonify(user)
73 |
74 | def add_user(new_user):
75 | conn = sqlite3.connect('mydb.db')
76 | print ("Opened database successfully");
77 | api_list=[]
78 | cursor=conn.cursor()
79 | cursor.execute("SELECT * from users where username=? or email=?",(new_user['username'],new_user['email']))
80 | data = cursor.fetchall()
81 | if len(data) != 0:
82 | abort(409)
83 | else:
84 | cursor.execute("insert into users (username, email, password, full_name) values(?,?,?,?)",(new_user['username'],new_user['email'], new_user['password'], new_user['name']))
85 | conn.commit()
86 | return "Success"
87 | conn.close()
88 | return jsonify(a_dict)
89 |
90 |
91 | def del_user(del_user):
92 | conn = sqlite3.connect('mydb.db')
93 | print ("Opened database successfully");
94 | cursor=conn.cursor()
95 | cursor.execute("SELECT * from users where username=? ",(del_user,))
96 | data = cursor.fetchall()
97 | print ("Data" ,data)
98 | if len(data) == 0:
99 | abort(404)
100 | else:
101 | cursor.execute("delete from users where username==?",(del_user,))
102 | conn.commit()
103 | return "Success"
104 |
105 | def list_tweets():
106 | conn = sqlite3.connect('mydb.db')
107 | print ("Opened database successfully");
108 | api_list=[]
109 | cursor=conn.cursor()
110 | cursor.execute("SELECT username, body, tweet_time, id from tweets")
111 | data = cursor.fetchall()
112 | print (data)
113 | print (len(data))
114 | if len(data) == 0:
115 | return api_list
116 | else:
117 | for row in data:
118 | tweets = {}
119 |
120 | tweets['tweetedby'] = row[0]
121 | tweets['body'] = row[1]
122 | tweets['timestamp'] = row[2]
123 | tweets['id'] = row[3]
124 |
125 | print (tweets)
126 | api_list.append(tweets)
127 |
128 | conn.close()
129 | print (api_list)
130 | return jsonify({'tweets_list': api_list})
131 |
132 | def add_tweet(new_tweets):
133 | conn = sqlite3.connect('mydb.db')
134 | print ("Opened database successfully");
135 | cursor=conn.cursor()
136 | cursor.execute("SELECT * from users where username=? ",(new_tweets['username'],))
137 | data = cursor.fetchall()
138 |
139 | if len(data) == 0:
140 | abort(404)
141 | else:
142 | cursor.execute("INSERT into tweets (username, body, tweet_time) values(?,?,?)",(new_tweets['username'],new_tweets['body'], new_tweets['created_at']))
143 | conn.commit()
144 | return "Success"
145 |
146 | def upd_user(user):
147 | conn = sqlite3.connect('mydb.db')
148 | print ("Opened database successfully");
149 | cursor=conn.cursor()
150 | cursor.execute("SELECT * from users where id=? ",(user['id'],))
151 | data = cursor.fetchall()
152 | print (data)
153 | if len(data) == 0:
154 | abort(404)
155 | else:
156 | key_list=user.keys()
157 | for i in key_list:
158 | if i != "id":
159 | print (user, i)
160 | # cursor.execute("UPDATE users set {0}=? where id=? ", (i, user[i], user['id']))
161 | cursor.execute("""UPDATE users SET {0} = ? WHERE id = ?""".format(i), (user[i], user['id']))
162 | conn.commit()
163 | return "Success"
164 |
165 |
166 |
167 | @app.route("/api/v1/info")
168 | def home_index():
169 | conn = sqlite3.connect('mydb.db')
170 | print ("Opened database successfully");
171 | api_list=[]
172 | cursor = conn.execute("SELECT buildtime, version, methods, links from apirelease")
173 | for row in cursor:
174 | api = {}
175 | api['version'] = row[0]
176 | api['buildtime'] = row[1]
177 | api['methods'] = row[2]
178 | api['links'] = row[3]
179 | api_list.append(api)
180 | conn.close()
181 | return jsonify({'api_version': api_list}), 200
182 |
183 |
184 |
185 | @app.route('/api/v1/users', methods=['GET'])
186 | def get_users():
187 | return list_users()
188 |
189 | @app.route('/api/v1/users/
', methods=['GET'])
190 | def get_user(user_id):
191 | return list_user(user_id)
192 |
193 |
194 | @app.route('/api/v1/users', methods=['POST'])
195 | def create_user():
196 | if not request.json or not 'username' in request.json or not 'email' in request.json or not 'password' in request.json:
197 | abort(400)
198 | user = {
199 | 'username': request.json['username'],
200 | 'email': request.json['email'],
201 | 'name': request.json.get('name',""),
202 | 'password': request.json['password']
203 | }
204 | return jsonify({'status': add_user(user)}), 201
205 |
206 | @app.route('/api/v1/users', methods=['DELETE'])
207 | def delete_user():
208 | if not request.json or not 'username' in request.json:
209 | abort(400)
210 | user=request.json['username']
211 | return jsonify({'status': del_user(user)}), 200
212 |
213 |
214 | @app.route('/api/v1/users/', methods=['PUT'])
215 | def update_user(user_id):
216 | user = {}
217 | # if not request.json:
218 | # abort(400)
219 | user['id']=user_id
220 | key_list = request.json.keys()
221 | for i in key_list:
222 | user[i] = request.json[i]
223 | print (user)
224 |
225 | return jsonify({'status': upd_user(user)}), 200
226 |
227 |
228 |
229 |
230 |
231 | @app.route('/api/v2/tweets', methods=['GET'])
232 | def get_tweets():
233 | return list_tweets()
234 |
235 | @app.route('/api/v2/tweets', methods=['POST'])
236 | def add_tweets():
237 |
238 | user_tweet = {}
239 | if not request.json or not 'username' in request.json or not 'body' in request.json:
240 | abort(400)
241 | user_tweet['username'] = request.json['username']
242 | user_tweet['body'] = request.json['body']
243 | user_tweet['created_at']=strftime("%Y-%m-%dT%H:%M:%SZ", gmtime())
244 | print (user_tweet)
245 | return jsonify({'status': add_tweet(user_tweet)}), 201
246 |
247 | @app.route('/api/v2/tweets/', methods=['GET'])
248 | def get_tweet(id):
249 | return list_tweet(id)
250 |
251 |
252 | @app.errorhandler(404)
253 | def resource_not_found(error):
254 | return make_response(jsonify({'error': 'Resource not found!'}), 404)
255 |
256 | @app.errorhandler(409)
257 | def user_found(error):
258 | return make_response(jsonify({'error': 'Conflict! Record exist'}), 409)
259 |
260 | @app.errorhandler(400)
261 | def invalid_request(error):
262 | return make_response(jsonify({'error': 'Bad Request'}), 400)
263 |
264 | if __name__ == '__main__':
265 | app.run(host='0.0.0.0', port=5000, debug=True)
266 |
--------------------------------------------------------------------------------
/Chapter05/app.py:
--------------------------------------------------------------------------------
1 | # Importing modules
2 | from flask import Flask, flash, render_template, flash, request, jsonify, redirect, session
3 | from flask import abort
4 | import bcrypt
5 | from flask_cors import CORS, cross_origin
6 | from flask import make_response, url_for
7 | import json
8 | import random
9 | from requests import Requests
10 | from pymongo import MongoClient
11 | from flask.ext.pymongo import PyMongo
12 | from time import gmtime, strftime
13 |
14 | from flask.ext.mongoalchemy import MongoAlchemy
15 |
16 |
17 | # Object creation
18 | app = Flask(__name__)
19 | app.config.from_object(__name__)
20 | app.secret_key = ''
21 | CORS(app)
22 |
23 | app.config['MONGOALCHEMY_DATABASE'] = 'app'
24 | app.config['MONGOALCHEMY_CONNECTION_STRING'] = 'mongodb://localhost:27017/'
25 | connection = MongoClient("mongodb://localhost:27017/")
26 |
27 |
28 | db = MongoAlchemy()
29 |
30 | mongo=PyMongo(app)
31 | # Initialize Database
32 | def create_mongodatabase():
33 | try:
34 | dbnames = connection.database_names()
35 | if 'app' not in dbnames:
36 | db_api = connection.app.apirelease
37 | db_api.insert( {
38 | "buildtime": "2017-01-01 10:00:00",
39 | "links": "/api/v1/users",
40 | "methods": "get, post, put, delete",
41 | "version": "v1"
42 | })
43 | db_api.insert( {
44 | "buildtime": "2017-02-11 10:00:00",
45 | "links": "api/v2/tweets",
46 | "methods": "get, post",
47 | "version": "2017-01-10 10:00:00"
48 | })
49 | print ("Database Initialize completed!")
50 | else:
51 | print ("Database already Initialized!")
52 | except:
53 | print ("Database creation failed!!")
54 |
55 |
56 | # API Routes
57 | @app.route('/')
58 | def home():
59 | if not session.get('logged_in'):
60 | return render_template('login.html')
61 | else:
62 | return render_template('index.html', session = session['username'])
63 |
64 | @app.route('/index')
65 | def index():
66 | return render_template('index.html', session = session['username'])
67 |
68 | @app.route('/login', methods=['POST'])
69 | def do_admin_login():
70 | users = mongo.db.users
71 | api_list=[]
72 | login_user = users.find({'username': request.form['username']})
73 | for i in login_user:
74 | api_list.append(i)
75 | print (api_list)
76 | if api_list != []:
77 | # print (api_list[0]['password'].decode('utf-8'), bcrypt.hashpw(request.form['password'].encode('utf-8'), api_list[0]['password']).decode('utf-8'))
78 | if api_list[0]['password'].decode('utf-8') == bcrypt.hashpw(request.form['password'].encode('utf-8'), api_list[0]['password']).decode('utf-8'):
79 | session['logged_in'] = api_list[0]['username']
80 | return redirect(url_for('index'))
81 | return 'Invalide username/password!'
82 | else:
83 | flash("Invalid Authentication")
84 |
85 | return 'Invalid User!'
86 |
87 |
88 |
89 | @app.route('/signup', methods=['GET', 'POST'])
90 | def signup():
91 | if request.method=='POST':
92 | users = mongo.db.users
93 | api_list=[]
94 | existing_user = users.find({'$or':[{"username":request.form['username']} ,{"email":request.form['email']}]})
95 | for i in existing_user:
96 | # print (str(i))
97 | api_list.append(str(i))
98 |
99 | # print (api_list)
100 | if api_list == []:
101 | users.insert({
102 | "email": request.form['email'],
103 | "id": random.randint(1,1000),
104 | "name": request.form['name'],
105 | "password": bcrypt.hashpw(request.form['pass'].encode('utf-8'), bcrypt.gensalt()),
106 | "username": request.form['username']
107 | })
108 | session['username'] = request.form['username']
109 | return redirect(url_for('home'))
110 |
111 | return 'That user already exists'
112 | else :
113 | return render_template('signup.html')
114 |
115 |
116 |
117 |
118 | @app.route("/logout")
119 | def logout():
120 | session['logged_in'] = False
121 | return redirect(url_for('home'))
122 |
123 | @app.route('/profile', methods=['GET', 'POST'])
124 | def profile():
125 | if request.method=='POST':
126 | users = mongo.db.users
127 | api_list=[]
128 | existing_users = users.find({"username":session['username']})
129 | for i in existing_users:
130 | # print (str(i))
131 | api_list.append(str(i))
132 | user = {}
133 | print (api_list)
134 | if api_list != []:
135 | print (request.form['email'])
136 | user['email']=request.form['email']
137 | user['name']= request.form['name']
138 | user['password']=request.form['pass']
139 | users.update({'username':session['username']},{'$set': user} )
140 | else:
141 | return 'User not found!'
142 | return redirect(url_for('index'))
143 | if request.method=='GET':
144 | users = mongo.db.users
145 | user=[]
146 | print (session['username'])
147 | existing_user = users.find({"username":session['username']})
148 | for i in existing_user:
149 | user.append(i)
150 | return render_template('profile.html', name=user[0]['name'], username=user[0]['username'], password=user[0]['password'], email=user[0]['email'])
151 |
152 |
153 |
154 | @app.route("/api/v1/info")
155 | def home_index():
156 | api_list=[]
157 | db = connection.app.apirelease
158 | for row in db.find():
159 | print (row)
160 | # api_list['buildtime']. str(row['buildtime'])
161 | # api_list['version'] = str(row['version'])
162 | # api_list['links'] = str(row['links'])
163 | # api_list['methods'] = str(row['methods'])
164 | api_list.append(str(row))
165 | print (api_list)
166 | return json.dumps(api_list), 200
167 |
168 |
169 |
170 | @app.route('/api/v1/users', methods=['GET'])
171 | def get_users():
172 | return Requests.list_users()
173 |
174 | @app.route('/api/v1/users/', methods=['GET'])
175 | def get_user(user_id):
176 | return Requests.list_user(user_id)
177 |
178 |
179 | @app.route('/api/v1/users', methods=['POST'])
180 | def create_user():
181 | if not request.json or not 'username' in request.json or not 'email' in request.json or not 'password' in request.json:
182 | abort(400)
183 | user = {
184 | 'username': request.json['username'],
185 | 'email': request.json['email'],
186 | 'name': request.json.get('name',""),
187 | 'password': request.json['password'],
188 | 'id': random.randint(1,1000)
189 | }
190 | return jsonify({'status': Requests.add_user(user)}), 201
191 |
192 | @app.route('/api/v1/users', methods=['DELETE'])
193 | def delete_user():
194 | if not request.json or not 'username' in request.json:
195 | abort(400)
196 | user=request.json['username']
197 | return jsonify({'status': Requests.del_user(user)}), 200
198 |
199 |
200 | @app.route('/api/v1/users/', methods=['PUT'])
201 | def update_user(user_id):
202 | user = {}
203 | user['id']=user_id
204 | key_list = request.json.keys()
205 | for i in key_list:
206 | user[i] = request.json[i]
207 | return jsonify({'status': Requests.upd_user(user)}), 200
208 |
209 | @app.route('/api/v2/tweets', methods=['GET'])
210 | def get_tweets():
211 | return Requests.list_tweets()
212 |
213 | @app.route('/api/v2/tweets', methods=['POST'])
214 | def add_tweets():
215 |
216 | user_tweet = {}
217 | if not request.json or not 'username' in request.json or not 'body' in request.json:
218 | abort(400)
219 | user_tweet['tweetedby'] = request.json['username']
220 | user_tweet['body'] = request.json['body']
221 | user_tweet['timestamp']=strftime("%Y-%m-%dT%H:%M:%SZ", gmtime())
222 | user_tweet['id'] = random.randint(1,1000)
223 |
224 | return jsonify({'status': Requests.add_tweet(user_tweet)}), 201
225 |
226 | @app.route('/api/v2/tweets/', methods=['GET'])
227 | def get_tweet(tweetedby):
228 | return Requests.list_tweet(tweetedby)
229 |
230 | # Error handling
231 | @app.errorhandler(404)
232 | def resource_not_found(error):
233 | return make_response(jsonify({'error': 'Resource not found!'}), 404)
234 |
235 | @app.errorhandler(409)
236 | def user_found(error):
237 | return make_response(jsonify({'error': 'Conflict! Record exist'}), 409)
238 |
239 | @app.errorhandler(400)
240 | def invalid_request(error):
241 | return make_response(jsonify({'error': 'Bad Request'}), 400)
242 |
243 | # Main Function
244 | if __name__ == '__main__':
245 | create_mongodatabase()
246 | app.run(host='0.0.0.0', port=5000, debug=True)
247 |
--------------------------------------------------------------------------------
/Chapter06/app.py:
--------------------------------------------------------------------------------
1 | # Importing modules
2 | from flask import Flask, flash, render_template, flash, request, jsonify, redirect, session
3 | from flask import abort
4 | import bcrypt
5 | from flask_cors import CORS, cross_origin
6 | from flask import make_response, url_for
7 | import json
8 | import random
9 | from requests import Requests
10 | from pymongo import MongoClient
11 | from flask.ext.pymongo import PyMongo
12 | from time import gmtime, strftime
13 |
14 | from flask.ext.mongoalchemy import MongoAlchemy
15 |
16 |
17 | # Object creation
18 | app = Flask(__name__)
19 | app.config.from_object(__name__)
20 | app.secret_key = ''
21 | CORS(app)
22 |
23 | app.config['MONGOALCHEMY_DATABASE'] = 'app'
24 | app.config['MONGOALCHEMY_CONNECTION_STRING'] = 'mongodb://localhost:27017/'
25 | connection = MongoClient("mongodb://localhost:27017/")
26 |
27 |
28 | db = MongoAlchemy()
29 |
30 | mongo=PyMongo(app)
31 | # Initialize Database
32 | def create_mongodatabase():
33 | try:
34 | dbnames = connection.database_names()
35 | if 'app' not in dbnames:
36 | db_api = connection.app.apirelease
37 | db_api.insert( {
38 | "buildtime": "2017-01-01 10:00:00",
39 | "links": "/api/v1/users",
40 | "methods": "get, post, put, delete",
41 | "version": "v1"
42 | })
43 | db_api.insert( {
44 | "buildtime": "2017-02-11 10:00:00",
45 | "links": "api/v2/tweets",
46 | "methods": "get, post",
47 | "version": "2017-01-10 10:00:00"
48 | })
49 | print ("Database Initialize completed!")
50 | else:
51 | print ("Database already Initialized!")
52 | except:
53 | print ("Database creation failed!!")
54 |
55 |
56 | # API Routes
57 | @app.route('/')
58 | def home():
59 | if not session.get('logged_in'):
60 | return render_template('login.html')
61 | else:
62 | return render_template('index.html', session = session['logged_in'])
63 |
64 | @app.route('/index')
65 | def index():
66 | return render_template('index.html', session = session['logged_in'])
67 |
68 | @app.route('/login', methods=['POST'])
69 | def do_admin_login():
70 | users = mongo.db.users
71 | api_list=[]
72 | login_user = users.find({'username': request.form['username']})
73 | for i in login_user:
74 | api_list.append(i)
75 | print (api_list)
76 | if api_list != []:
77 | #print (api_list[0]['password'].decode('utf-8'), bcrypt.hashpw(request.form['password'].encode('utf-8'), api_list[0]['password']).decode('utf-8'))
78 | if api_list[0]['password'].decode('utf-8') == bcrypt.hashpw(request.form['password'].encode('utf-8'), api_list[0]['password']).decode('utf-8'):
79 | session['logged_in'] = api_list[0]['username']
80 | return redirect(url_for('index'))
81 | return 'Invalide username/password!'
82 | else:
83 | flash("Invalid Authentication")
84 |
85 | return 'Invalid User!'
86 |
87 |
88 |
89 | @app.route('/signup', methods=['GET', 'POST'])
90 | def signup():
91 | if request.method=='POST':
92 | users = mongo.db.users
93 | api_list=[]
94 | existing_user = users.find({'$or':[{"username":request.form['username']} ,{"email":request.form['email']}]})
95 | for i in existing_user:
96 | # print (str(i))
97 | api_list.append(str(i))
98 |
99 | # print (api_list)
100 | if api_list == []:
101 | users.insert({
102 | "email": (request.form['email']).lower(),
103 | "id": random.randint(1,1000),
104 | "name": request.form['name'],
105 | "password": bcrypt.hashpw(request.form['pass'].encode('utf-8'), bcrypt.gensalt()),
106 | "username": request.form['username']
107 | })
108 | session['username'] = request.form['username']
109 | return redirect(url_for('home'))
110 |
111 | return 'That user already exists'
112 | else :
113 | return render_template('signup.html')
114 |
115 |
116 |
117 |
118 | @app.route("/logout")
119 | def logout():
120 | session['logged_in'] = False
121 | return redirect(url_for('home'))
122 |
123 | @app.route('/profile', methods=['GET', 'POST'])
124 | def profile():
125 | if request.method=='POST':
126 | users = mongo.db.users
127 | api_list=[]
128 | existing_users = users.find({"username":session['username']})
129 | for i in existing_users:
130 | # print (str(i))
131 | api_list.append(str(i))
132 | user = {}
133 | print (api_list)
134 | if api_list != []:
135 | print (request.form['email'])
136 | user['email']=(request.form['email']).lower()
137 | user['name']= request.form['name']
138 | user['password']=bcrypt.hashpw(request.form['pass'].encode('utf-8'), bcrypt.gensalt())
139 | users.update({'username':session['username']},{'$set': user} )
140 | else:
141 | return 'User not found!'
142 | return redirect(url_for('index'))
143 | if request.method=='GET':
144 | users = mongo.db.users
145 | user=[]
146 | print (session['username'])
147 | existing_user = users.find({"username":session['username']})
148 | for i in existing_user:
149 | user.append(i)
150 | return render_template('profile.html', name=user[0]['name'], username=user[0]['username'], password=user[0]['password'], email=user[0]['email'])
151 |
152 |
153 |
154 | @app.route("/api/v1/info")
155 | def home_index():
156 | api_list=[]
157 | db = connection.app.apirelease
158 | for row in db.find():
159 | print (row)
160 | # api_list['buildtime']. str(row['buildtime'])
161 | # api_list['version'] = str(row['version'])
162 | # api_list['links'] = str(row['links'])
163 | # api_list['methods'] = str(row['methods'])
164 | api_list.append(str(row))
165 | print (api_list)
166 | return json.dumps(api_list), 200
167 |
168 |
169 |
170 | @app.route('/api/v1/users', methods=['GET'])
171 | def get_users():
172 | return Requests.list_users()
173 |
174 | @app.route('/api/v1/users/', methods=['GET'])
175 | def get_user(user_id):
176 | return Requests.list_user(user_id)
177 |
178 |
179 | @app.route('/api/v1/users', methods=['POST'])
180 | def create_user():
181 | if not request.json or not 'username' in request.json or not 'email' in request.json or not 'password' in request.json:
182 | abort(400)
183 | user = {
184 | 'username': request.json['username'],
185 | 'email': request.json['email'],
186 | 'name': request.json.get('name',""),
187 | 'password': request.json['password'],
188 | 'id': random.randint(1,1000)
189 | }
190 | return jsonify({'status': Requests.add_user(user)}), 201
191 |
192 | @app.route('/api/v1/users', methods=['DELETE'])
193 | def delete_user():
194 | if not request.json or not 'username' in request.json:
195 | abort(400)
196 | user=request.json['username']
197 | return jsonify({'status': Requests.del_user(user)}), 200
198 |
199 |
200 | @app.route('/api/v1/users/', methods=['PUT'])
201 | def update_user(user_id):
202 | user = {}
203 | user['id']=user_id
204 | key_list = request.json.keys()
205 | for i in key_list:
206 | user[i] = request.json[i]
207 | return jsonify({'status': Requests.upd_user(user)}), 200
208 |
209 | @app.route('/api/v2/tweets', methods=['GET'])
210 | def get_tweets():
211 | return Requests.list_tweets()
212 |
213 | @app.route('/api/v2/tweets', methods=['POST'])
214 | def add_tweets():
215 |
216 | user_tweet = {}
217 | if not request.json or not 'username' in request.json or not 'body' in request.json:
218 | abort(400)
219 | user_tweet['tweetedby'] = request.json['username']
220 | user_tweet['body'] = request.json['body']
221 | user_tweet['timestamp']=strftime("%Y-%m-%dT%H:%M:%SZ", gmtime())
222 | user_tweet['id'] = random.randint(1,1000)
223 |
224 | return jsonify({'status': Requests.add_tweet(user_tweet)}), 201
225 |
226 | @app.route('/api/v2/tweets/', methods=['GET'])
227 | def get_tweet(tweetedby):
228 | return Requests.list_tweet(tweetedby)
229 |
230 |
231 |
232 | # Error handling
233 | @app.errorhandler(404)
234 | def resource_not_found(error):
235 | return make_response(jsonify({'error': 'Resource not found!'}), 404)
236 |
237 | @app.errorhandler(409)
238 | def user_found(error):
239 | return make_response(jsonify({'error': 'Conflict! Record exist'}), 409)
240 |
241 | @app.errorhandler(400)
242 | def invalid_request(error):
243 | return make_response(jsonify({'error': 'Bad Request'}), 400)
244 |
245 | # Main Function
246 | if __name__ == '__main__':
247 | create_mongodatabase()
248 | app.run(host='0.0.0.0', port=5000, debug=True)
249 |
--------------------------------------------------------------------------------
/Chapter07/app.py:
--------------------------------------------------------------------------------
1 | # Importing modules
2 | from flask import Flask, flash, render_template, flash, request, jsonify, redirect, session
3 | from flask import abort
4 | import bcrypt
5 | from flask_cors import CORS, cross_origin
6 | from flask import make_response, url_for
7 | import json
8 | import random
9 | from requests import Requests
10 | from pymongo import MongoClient
11 | from flask.ext.pymongo import PyMongo
12 | from time import gmtime, strftime
13 |
14 | from flask.ext.mongoalchemy import MongoAlchemy
15 |
16 |
17 | # Object creation
18 | app = Flask(__name__)
19 | app.config.from_object(__name__)
20 | app.secret_key = ''
21 | CORS(app)
22 |
23 | app.config['MONGOALCHEMY_DATABASE'] = 'app'
24 | app.config['MONGOALCHEMY_CONNECTION_STRING'] = 'mongodb://localhost:27017/'
25 | connection = MongoClient("mongodb://localhost:27017/")
26 |
27 |
28 | db = MongoAlchemy()
29 |
30 | mongo=PyMongo(app)
31 | # Initialize Database
32 | def create_mongodatabase():
33 | try:
34 | dbnames = connection.database_names()
35 | if 'app' not in dbnames:
36 | db_api = connection.app.apirelease
37 | db_api.insert( {
38 | "buildtime": "2017-01-01 10:00:00",
39 | "links": "/api/v1/users",
40 | "methods": "get, post, put, delete",
41 | "version": "v1"
42 | })
43 | db_api.insert( {
44 | "buildtime": "2017-02-11 10:00:00",
45 | "links": "api/v2/tweets",
46 | "methods": "get, post",
47 | "version": "2017-01-10 10:00:00"
48 | })
49 | print ("Database Initialize completed!")
50 | else:
51 | print ("Database already Initialized!")
52 | except:
53 | print ("Database creation failed!!")
54 |
55 |
56 | # API Routes
57 | @app.route('/')
58 | def home():
59 | if not session.get('logged_in'):
60 | return render_template('login.html')
61 | else:
62 | return render_template('index.html', session = session['logged_in'])
63 |
64 | @app.route('/index')
65 | def index():
66 | return render_template('index.html', session = session['logged_in'])
67 |
68 | @app.route('/login', methods=['POST'])
69 | def do_admin_login():
70 | users = mongo.db.users
71 | api_list=[]
72 | login_user = users.find({'username': request.form['username']})
73 | for i in login_user:
74 | api_list.append(i)
75 | print (api_list)
76 | if api_list != []:
77 | #print (api_list[0]['password'].decode('utf-8'), bcrypt.hashpw(request.form['password'].encode('utf-8'), api_list[0]['password']).decode('utf-8'))
78 | if api_list[0]['password'].decode('utf-8') == bcrypt.hashpw(request.form['password'].encode('utf-8'), api_list[0]['password']).decode('utf-8'):
79 | session['logged_in'] = api_list[0]['username']
80 | return redirect(url_for('index'))
81 | return 'Invalide username/password!'
82 | else:
83 | flash("Invalid Authentication")
84 |
85 | return 'Invalid User!'
86 |
87 |
88 |
89 | @app.route('/signup', methods=['GET', 'POST'])
90 | def signup():
91 | if request.method=='POST':
92 | users = mongo.db.users
93 | api_list=[]
94 | existing_user = users.find({'$or':[{"username":request.form['username']} ,{"email":request.form['email']}]})
95 | for i in existing_user:
96 | # print (str(i))
97 | api_list.append(str(i))
98 |
99 | # print (api_list)
100 | if api_list == []:
101 | users.insert({
102 | "email": (request.form['email']).lower(),
103 | "id": random.randint(1,1000),
104 | "name": request.form['name'],
105 | "password": bcrypt.hashpw(request.form['pass'].encode('utf-8'), bcrypt.gensalt()),
106 | "username": request.form['username']
107 | })
108 | session['username'] = request.form['username']
109 | return redirect(url_for('home'))
110 |
111 | return 'That user already exists'
112 | else :
113 | return render_template('signup.html')
114 |
115 |
116 |
117 |
118 | @app.route("/logout")
119 | def logout():
120 | session['logged_in'] = False
121 | return redirect(url_for('home'))
122 |
123 | @app.route('/profile', methods=['GET', 'POST'])
124 | def profile():
125 | if request.method=='POST':
126 | users = mongo.db.users
127 | api_list=[]
128 | existing_users = users.find({"username":session['username']})
129 | for i in existing_users:
130 | # print (str(i))
131 | api_list.append(str(i))
132 | user = {}
133 | print (api_list)
134 | if api_list != []:
135 | print (request.form['email'])
136 | user['email']=(request.form['email']).lower()
137 | user['name']= request.form['name']
138 | user['password']=bcrypt.hashpw(request.form['pass'].encode('utf-8'), bcrypt.gensalt())
139 | users.update({'username':session['username']},{'$set': user} )
140 | else:
141 | return 'User not found!'
142 | return redirect(url_for('index'))
143 | if request.method=='GET':
144 | users = mongo.db.users
145 | user=[]
146 | print (session['username'])
147 | existing_user = users.find({"username":session['username']})
148 | for i in existing_user:
149 | user.append(i)
150 | return render_template('profile.html', name=user[0]['name'], username=user[0]['username'], password=user[0]['password'], email=user[0]['email'])
151 |
152 |
153 |
154 | @app.route("/api/v1/info")
155 | def home_index():
156 | api_list=[]
157 | db = connection.app.apirelease
158 | for row in db.find():
159 | print (row)
160 | # api_list['buildtime']. str(row['buildtime'])
161 | # api_list['version'] = str(row['version'])
162 | # api_list['links'] = str(row['links'])
163 | # api_list['methods'] = str(row['methods'])
164 | api_list.append(str(row))
165 | print (api_list)
166 | return json.dumps(api_list), 200
167 |
168 |
169 |
170 | @app.route('/api/v1/users', methods=['GET'])
171 | def get_users():
172 | return Requests.list_users()
173 |
174 | @app.route('/api/v1/users/', methods=['GET'])
175 | def get_user(user_id):
176 | return Requests.list_user(user_id)
177 |
178 |
179 | @app.route('/api/v1/users', methods=['POST'])
180 | def create_user():
181 | if not request.json or not 'username' in request.json or not 'email' in request.json or not 'password' in request.json:
182 | abort(400)
183 | user = {
184 | 'username': request.json['username'],
185 | 'email': request.json['email'],
186 | 'name': request.json.get('name',""),
187 | 'password': request.json['password'],
188 | 'id': random.randint(1,1000)
189 | }
190 | return jsonify({'status': Requests.add_user(user)}), 201
191 |
192 | @app.route('/api/v1/users', methods=['DELETE'])
193 | def delete_user():
194 | if not request.json or not 'username' in request.json:
195 | abort(400)
196 | user=request.json['username']
197 | return jsonify({'status': Requests.del_user(user)}), 200
198 |
199 |
200 | @app.route('/api/v1/users/', methods=['PUT'])
201 | def update_user(user_id):
202 | user = {}
203 | user['id']=user_id
204 | key_list = request.json.keys()
205 | for i in key_list:
206 | user[i] = request.json[i]
207 | return jsonify({'status': Requests.upd_user(user)}), 200
208 |
209 | @app.route('/api/v2/tweets', methods=['GET'])
210 | def get_tweets():
211 | return Requests.list_tweets()
212 |
213 | @app.route('/api/v2/tweets', methods=['POST'])
214 | def add_tweets():
215 |
216 | user_tweet = {}
217 | if not request.json or not 'username' in request.json or not 'body' in request.json:
218 | abort(400)
219 | user_tweet['tweetedby'] = request.json['username']
220 | user_tweet['body'] = request.json['body']
221 | user_tweet['timestamp']=strftime("%Y-%m-%dT%H:%M:%SZ", gmtime())
222 | user_tweet['id'] = random.randint(1,1000)
223 |
224 | return jsonify({'status': Requests.add_tweet(user_tweet)}), 201
225 |
226 | @app.route('/api/v2/tweets/', methods=['GET'])
227 | def get_tweet(tweetedby):
228 | return Requests.list_tweet(tweetedby)
229 |
230 |
231 |
232 | # Error handling
233 | @app.errorhandler(404)
234 | def resource_not_found(error):
235 | return make_response(jsonify({'error': 'Resource not found!'}), 404)
236 |
237 | @app.errorhandler(409)
238 | def user_found(error):
239 | return make_response(jsonify({'error': 'Conflict! Record exist'}), 409)
240 |
241 | @app.errorhandler(400)
242 | def invalid_request(error):
243 | return make_response(jsonify({'error': 'Bad Request'}), 400)
244 |
245 | # Main Function
246 | if __name__ == '__main__':
247 | create_mongodatabase()
248 | app.run(host='0.0.0.0', port=5000, debug=True)
249 |
--------------------------------------------------------------------------------