-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·178 lines (150 loc) · 5.47 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import os
import certifi
import requests
from flask import Flask, Response, jsonify, request, make_response, render_template, flash, redirect, g, after_this_request
from flask_pymongo import PyMongo
from pymongo import MongoClient
from flask_swagger_ui import get_swaggerui_blueprint
from flask_cors import CORS
from flask_jwt_extended import JWTManager, jwt_required, create_access_token, get_jwt_identity
from bson.json_util import dumps
from bson.objectid import ObjectId
from flask_httpauth import HTTPBasicAuth
from werkzeug.security import generate_password_hash, check_password_hash
from botocore.exceptions import NoCredentialsError
import boto3
app = Flask(__name__)
jwt = JWTManager(app)
cors = CORS(app)
app.config["CORS_HEADERS"] = "Content-Type"
mongo_db_url = os.environ.get("MONGO_DB_CONN_STRING")
client = MongoClient(mongo_db_url)
db = client['blogging']
connection_string = f"mongodb://localhost:27017/blog"
client = MongoClient(connection_string)
app.config['MONGO_URI'] = "mongodb://localhost:27017/blog"
mongo = PyMongo(app)
# Configure SWAGGER
SWAGGER_URL = '/swagger'
API_URL = '/static/swagger.json' # Our API url (can of course be a local resource)
swaggerui_blueprint = get_swaggerui_blueprint(
SWAGGER_URL,
API_URL,
config={
'app_name': "Blogging",
},
)
app.register_blueprint(swaggerui_blueprint, url_prefix = SWAGGER_URL)
auth = HTTPBasicAuth()
# Configure JWT
app.config['JWT_SECRET_KEY'] = '854d9f0a3a754b16a6e1f3655b3cfbb5'
jwt = JWTManager(app)
app.config['JWT_TOKEN_LOCATION'] = ['headers']
app.config['JWT_BLACKLIST_ENABLED'] = True
app.config['PROPAGATE_EXCEPTIONS'] = True
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmcmVzaCI6ZmFsc2UsImlhdCI6MTcwMTM2MTQwMCwianRpIjoiZGJlZmY2NzAtM2IzMi00NGQ3LTlkNzItMjY2NjliNjA3OGM0IiwidHlwZSI6ImFjY2VzcyIsInN1YiI6InVzZXIxIiwibmJmIjoxNzAxMzYxNDAwLCJleHAiOjE3MDEzNjIzMDB9.Il6UB4Til2jOXTTaMhaFe0SOlhKmNkBQn6S3bdKzRtE'}
# Mock user data for demonstration
users = {
'user1': {'password': 'password1'},
"admin": generate_password_hash("admin"),
}
# Token creation route (login)
@app.route('/login', methods=['GET','POST'])
def login():
username = request.json.get('username', None)
password = request.json.get('password', None)
if username in users and users[username]['password'] == password:
access_token = create_access_token(identity=username)
return jsonify(access_token=access_token), 200
else:
return jsonify({'message': 'Invalid credentials'}), 401
# Protected route (CRUD operations)
@app.route('/protected', methods=['GET', 'POST'])
@jwt_required()
def protected():
current_user = get_jwt_identity()
return jsonify(logged_in_as=current_user), 200
# Configure BasicAuth
auth = HTTPBasicAuth()
@auth.verify_password
def verify_password(username, password):
if username in users and \
check_password_hash(users.get(username), password):
return username
# Base
@app.route('/')
@auth.login_required
def index():
return "Hello, {}!".format(auth.current_user())
# Create a User
@app.route('/register', methods=['POST'])
def add_user():
_json = request.json
_name = _json['name']
_email = _json['email']
_pwd = _json['pwd']
if _name and _email and _pwd and request.method == 'POST':
_hashed_password = generate_password_hash(_pwd)
id = mongo.db.user.insert_one({'name': _name, 'email': _email, 'pwd': _hashed_password})
return {"data":"User registered successfully"}
else:
return {'error':'Not found'}
apis = [
"http://localhost:8080/api/blogs"
# Add more endpoints as needed
]
# Get all blogs
@app.route('/api/blogs', methods=['GET'])
def get_blogs():
blogs = mongo.db.blog.find()
resp = dumps(blogs)
return resp
# Get a specific blog by ID
@app.route('/api/blogs/<id>')
@jwt_required()
def blog(id):
blog = mongo.db.blog.find_one({'_id':ObjectId(id)})
resp = dumps(blog)
return resp
# Delete a blog
@app.route('/api/blogs/<id>', methods=['DELETE'])
@jwt_required()
def delete_blog(id):
mongo.db.blog.delete_one({'_id':ObjectId(id)})
resp = jsonify("Blog Deleted Successfully")
resp.status_code = 200
return resp
# Update a blog
@app.route('/api/blogs/<id>', methods=['PUT'])
@jwt_required()
def update_blog(id):
_json = request.json
_id = id
_title = _json['title']
_content = _json['content']
_author = _json['author']
_timestamp = _json['timestamp']
if _title and _content and _author and _timestamp and request.method == 'PUT':
mongo.db.blog.update_one({'_id': ObjectId(_id['$oid']) if '$oid' in _id else ObjectId(_id)}, {'$set': {'title': _title, 'content': _content, 'author': _author, 'timestamp': _timestamp }})
resp = jsonify("Blog Updated Successfully")
resp.status_code = 200
return resp
# Create a blog
@app.route('/api/blogs', methods=['POST'])
@jwt_required()
def create_booking():
_json = request.json
_title = _json['title']
_content = _json['content']
_author = _json['author']
_timestamp = _json['timestamp']
if _title and _content and _author and _timestamp and request.method == 'POST':
id = mongo.db.blog.insert_one({'title': _title, 'content': _content, 'author': _author, 'timestamp': _timestamp })
return {"data":"Blog Added Successfully"}
else:
return {'error':'Blog Not Found'}
# Run the flask App
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)