Advertisement
Guest User

Untitled

a guest
Mar 27th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. #!/bin/bash
  2. read -p "Enter project path: " path
  3. mkdir $path
  4. cd $path
  5. virtualenv venv --python=/usr/bin/python3.6
  6. source venv/bin/activate
  7. pip install flask
  8. pip install mypy
  9. pip install pymysql
  10. pip install requests
  11.  
  12. pip freeze > requirements.txt
  13.  
  14. cat <<EOF >__init__.py
  15. from flask import Flask, request, render_template, Response, abort, send_file, jsonify, redirect, url_for, send_from_directory, session, make_response, flash, g, send_from_directory
  16. import pymysql.cursors
  17. import json, os, requests
  18. from modules.db import DB
  19.  
  20. app = Flask(__name__)
  21.  
  22. @app.route("/")
  23. def index():
  24. output = {
  25. "result": "success"
  26. }
  27. response = make_response(json.dumps(output,indent=4,sort_keys=True))
  28. response.headers['Access-Control-Allow-Origin'] = "*"
  29. response.headers[ 'Content-Type']='application/json'
  30. return response
  31.  
  32. if __name__ == "__main__":
  33. app.secret_key = 'secret123'
  34. app.run(debug=True)
  35.  
  36. EOF
  37. mkdir modules
  38. mkdir sql
  39. mkdir templates
  40. mkdir static
  41. mkdir bin
  42.  
  43. cd modules
  44. touch __init__.py
  45. cat <<EOF >db.py
  46. import pymysql.cursors
  47.  
  48. class DB:
  49. def __init__(self,database,host="localhost",user='root', password='secret',port=3306):
  50. self.connection = pymysql.connect(host='localhost',user=user, password=password,db=database,cursorclass=pymysql.cursors.DictCursor)
  51.  
  52. EOF
  53.  
  54. cd ..
  55. git init
  56. cat <<EOF >.gitignore
  57. .vscode
  58. venv
  59. _labs
  60. *.pyc
  61. __pycache__
  62.  
  63. EOF
  64.  
  65. cat <<EOF >README.md
  66. # Welcome to flaskinit
  67.  
  68. ## Description
  69.  
  70. Flask init is a shell script that creates a basic project structure, with a few dependencies within a virtualenv folder, using python3.6 (which you need to have installed previously, along with virtualenv)
  71. If you can't execute the script, use $ \`chmod +x ./flaskinit.sh\` to provide the script with executable permissions. No sudo permissions are required.
  72.  
  73. ## Requirements
  74.  
  75. * virtualenv
  76. * python3.6 installed on /usr/bin/python3.6 (new versions of this script will accept the python path as arguement before initializing the project)
  77. * MySQL server
  78.  
  79. ## Usage
  80.  
  81. After you execute the script, you will have a folder containing all the project structure and files. The file \`__init__.py\` on the root folder will be the "flask app file". It comes with a default route and some basic stuff to get you started quickly into adding more routes. Even if the script is meant to generate a squeleton for a Rest API, the templates and static folders are also created just in case you want to do a HTML response application.
  82.  
  83. The structure is made so that you include all your custom classes inside the modules folder, and all your "execute" files inside the bin folder.
  84.  
  85. To start the flask app, just activate the virtualenv by doing \`source venv/bin/activate\` and then starting the flask app with \`python __init__.py\`
  86.  
  87. ## Notes
  88.  
  89. * This script uses pymysql as a connector driver for MySQL db. If you dont want to use it, remove it from the virtualenv
  90. EOF
  91. read -p "Press enter to exit" continue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement