generated from nighthawkcoders/flask_portfolio
-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.py
108 lines (84 loc) · 3.32 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
import threading
# import "packages" from flask
from flask import render_template,request # import render_template from "public" flask libraries
from flask.cli import AppGroup
# import "packages" from "this" project
from __init__ import app, db # Definitions initialization
from aws_commands import run_aws_command # Import the function
from aws_commands import get_ec2_instance_info
# setup APIs
from api.covid import covid_api # Blueprint import api definition
from api.joke import joke_api # Blueprint import api definition
from api.user import user_api # Blueprint import api definition
from api.player import player_api
# database migrations
from model.users import initUsers
from model.players import initPlayers
# setup App pages
from projects.projects import app_projects # Blueprint directory import projects definition
from flask import request, jsonify
from dotenv import load_dotenv
import os
load_dotenv()
aws_access_key_id = os.getenv('AWS_ACCESS_KEY_ID')
aws_secret_access_key = os.getenv('AWS_SECRET_ACCESS_KEY')
# Initialize the SQLAlchemy object to work with the Flask app instance
db.init_app(app)
# register URIs
app.register_blueprint(joke_api) # register api routes
app.register_blueprint(covid_api) # register api routes
app.register_blueprint(user_api) # register api routes
app.register_blueprint(player_api)
app.register_blueprint(app_projects) # register app pages
@app.errorhandler(404) # catch for URL not found
def page_not_found(e):
# note that we set the 404 status explicitly
return render_template('404.html'), 404
@app.route('/') # connects default URL to index() function
def index():
return render_template("index.html")
@app.route('/table/') # connects /stub/ URL to stub() function
def table():
return render_template("table.html")
@app.route('/run-aws-command')
def aws_command_route():
try:
# Retrieve query parameters
instance = request.args.get('instance')
mycommand = request.args.get('mycommand')
# Ensure both parameters are provided
if not instance or not mycommand:
return jsonify({'error': 'Missing instance or command parameter'}), 400
output = run_aws_command(instance, mycommand)
return jsonify({'output': output})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/get-ec2-instances')
def ec2_instances_route():
try:
instances_info = get_ec2_instance_info()
response = jsonify(instances_info)
# response.headers.add('Access-Control-Allow-Origin', '*')
return response
except Exception as e:
return jsonify({'error': str(e)}), 500
# Create an AppGroup for custom commands
custom_cli = AppGroup('custom', help='Custom commands')
# Define a command to generate data
@custom_cli.command('generate_data')
def generate_data():
initUsers()
initPlayers()
# Register the custom command group with the Flask application
app.cli.add_command(custom_cli)
# this runs the application on the development server
#if __name__ == "__main__":
# change name for testing
#from flask_cors import CORS
#cors = CORS(app)
#app.run(debug=True, host="0.0.0.0", port="8086")
if __name__ == "__main__":
# change name for testing
from flask_cors import CORS
cors = CORS(app)
app.run(debug=True, host="0.0.0.0", port="8086")