Created
April 27, 2013 19:52
-
-
Save snosrap/5474442 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ $# -eq 0 ] ; then | |
echo "Usage: $0 <flask_app_name>" | |
exit 1 | |
fi | |
ROOT=$HOME | |
#### Flask #### | |
# Create the directories if they don't exist | |
mkdir -p "$ROOT/$1" | |
mkdir -p "$ROOT/$1/templates" | |
mkdir -p "$ROOT/$1/static" | |
# Create a favicon if it doesn't exist | |
touch "$ROOT/$1/static/favicon.ico" | |
touch "$ROOT/$1/static/js.js" | |
touch "$ROOT/$1/static/css.css" | |
# Make the main Flask file | |
cat <<EOF > "$ROOT/$1/$1.py" | |
from flask import * | |
app = Flask(__name__) | |
@app.route('/') | |
def index(): | |
return render_template('index.html') | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0', debug=True) | |
EOF | |
# Make the base template | |
cat <<EOF > "$ROOT/$1/templates/base.html" | |
<!doctype html> | |
<html lang="en"> | |
<head> | |
<title>{% block title %}{% endblock %}</title> | |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> | |
<script src="{{ url_for('static', filename='js.js') }}"></script> | |
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css.css') }}"> | |
<link rel="apple-touch-icon" href="{{ url_for('static', filename='favicon.ico') }}" /> | |
</head> | |
<body> | |
<header><a href="/">{% block header %}{% endblock %}</a></header> | |
<section>{% block content %}{% endblock %}</section> | |
</body> | |
</html> | |
EOF | |
# Make the index template | |
cat <<EOF > "$ROOT/$1/templates/index.html" | |
{% extends "base.html" %} | |
{% block title %}Hello{% endblock %} | |
{% block header %}Hello{% endblock %} | |
{% block content %} | |
Hello, Flask! | |
{% endblock %} | |
EOF | |
# Make the css template | |
cat <<EOF > "$ROOT/$1/static/css.css" | |
body{font-family:Helvetica;padding:0px;margin:0px;background-color:#f8f8f8} | |
header{background-color:#ddd;padding:10px;margin:0px;border-bottom:1px solid #bbb;font-size:24px;font-weight:bold} | |
header a{text-decoration:none;color:#000;} | |
section{padding:10px;margin:0px;} | |
EOF | |
chmod -R 755 "$ROOT/$1/$1.py" | |
chmod -R 755 "$ROOT/$1/templates" | |
chmod -R 755 "$ROOT/$1/static" | |
# Find free paired ports (8080 == 3030) | |
pnginx=8080 | |
puwsgi=3030 | |
while netstat -atwn | grep ":\(${pnginx}\|${puwsgi}\)\s"; do | |
pnginx=$(( $pnginx + 1 )); | |
puwsgi=$(( $puwsgi + 1 )); | |
done | |
echo "Using ports $pnginx and $puwsgi" | |
# Make the NGINX config file | |
cat <<EOF > "$ROOT/$1/nginx_$1.conf" | |
server { | |
listen $pnginx default; | |
client_max_body_size 4G; | |
server_name raspberry; | |
keepalive_timeout 5; | |
root $ROOT/$1/static; | |
location / { | |
try_files \$uri @proxy_to_app; | |
} | |
location @proxy_to_app { | |
include uwsgi_params; | |
uwsgi_pass 127.0.0.1:$puwsgi; | |
} | |
} | |
EOF | |
# Make the UWSGI config file | |
cat <<EOF > "$ROOT/$1/uwsgi_$1.ini" | |
[uwsgi] | |
plugin = python | |
socket = 127.0.0.1:$puwsgi | |
wsgi-file = $ROOT/$1/$1.py | |
callable = app | |
pythonpath = $ROOT/$1 | |
chdir = $ROOT/$1 | |
EOF | |
# Set up nginx and uwsgi | |
sudo ln -s "$ROOT/$1/nginx_$1.conf" "/etc/nginx/sites-available/." | |
sudo ln -s "/etc/nginx/sites-available/nginx_$1.conf" "/etc/nginx/sites-enabled/." | |
sudo ln -s "$ROOT/$1/uwsgi_$1.ini" "/etc/uwsgi/apps-available/." | |
sudo ln -s "/etc/uwsgi/apps-available/uwsgi_$1.ini" "/etc/uwsgi/apps-enabled/." | |
cat <<EOF > "$ROOT/$1/uninstall_$1.sh" | |
sudo rm "/etc/nginx/sites-enabled/nginx_$1.conf" | |
sudo rm "/etc/nginx/sites-available/nginx_$1.conf" | |
sudo rm "/etc/uwsgi/apps-enabled/uwsgi_$1.ini" | |
sudo rm "/etc/uwsgi/apps-available/uwsgi_$1.ini" | |
sudo service nginx restart | |
sudo service uwsgi restart | |
EOF | |
chmod -R 755 "$ROOT/$1/uninstall_$1.sh" | |
# Restart | |
sudo service nginx restart | |
sudo service uwsgi restart |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment