Last active
August 29, 2015 14:05
-
-
Save scottrice10/138a5c64d3bd9fbcb7af to your computer and use it in GitHub Desktop.
This script allows an AngularJS app with html5mode set to true to refresh and navigate correctly with an Apache web server on a MacBook. Specifically, the script 1. creates an .htaccess files in every git repo containing an index.html file, 2. creates sym links from all webapp directories in a local git directory to the apache document root, and…
This file contains hidden or 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
| # all apache files need writeable permissions for this script to run | |
| import sys, inspect, os, shutil | |
| home = os.path.expanduser("~") | |
| top = home + '/git' | |
| apacheRoot = '/Library/WebServer/Documents/' | |
| #first remind user to have proper permissions on /etc/apache2/ | |
| var = raw_input("\nScript requires that /etc/apache2/ is writeable. Running 'sudo chmod -R 777 /etc/apache2' will accomplish this. Enter 'y' to proceed.\n") | |
| if var != 'y': | |
| sys.exit(0) | |
| try: | |
| # Create .htaccess files that support AngularJS html5mode in every file in git directory, under home, that includes an index.html file. | |
| # *** Note that this will create more .htaccess files than needed, particularly if the JBoss module is cloned in Git folder *** | |
| for root, dirs, files in os.walk(top, topdown=False): | |
| for file in files: | |
| if file == 'index.html': | |
| with open(root + '/.htaccess', 'w') as newFile: | |
| #skip creating file if in jboss-development-server repo | |
| directory = os.path.dirname(os.path.realpath(str(newFile))) | |
| print "Creating new .htaccess file at: " + root + "/.htaccess" # script filename (usually with path) | |
| newFile.write("# Apache .htaccess\n") | |
| newFile.write("# angularjs pushstate (history) support:\n") | |
| newFile.write("# See http://www.josscrowcroft.com/2012/code/htaccess-for-html5-history-pushstate-url-routing/\n") | |
| newFile.write("<ifModule mod_rewrite.c>\n") | |
| newFile.write(" RewriteEngine On\n") | |
| newFile.write(" RewriteCond %{REQUEST_FILENAME} !-f\n") | |
| newFile.write(" RewriteCond %{REQUEST_FILENAME} !-d\n") | |
| newFile.write(" RewriteCond %{REQUEST_URI} !index\n") | |
| newFile.write(" RewriteRule (.*) index.html [L]\n") | |
| newFile.write("</ifModule>\n") | |
| newFile.close() | |
| #Create symlinks from all webapp directories in git directory | |
| # to /Library/WebServer/Documents - the default apache folder in Mac OSX. | |
| for root, dirs, files in os.walk(top, topdown=False): | |
| for directory in dirs: | |
| if directory == 'webapp': | |
| appNamesList = root.split("/") | |
| appName = appNamesList[4] | |
| # if sym link already exists under apache root, remove it before creating symlink | |
| if os.path.exists(apacheRoot + "/" + appName): | |
| os.unlink(apacheRoot + "/" + appName) | |
| #create symlink | |
| os.symlink(os.path.join(root, directory), apacheRoot + appName) | |
| print "Creating sym link: " + apacheRoot + appName | |
| #Write apache reverse proxy configuration file | |
| print "Adding Apache proxy config file at /etc/apache2/other/infoplus-api-proxy.conf." | |
| proxyFile = open('/etc/apache2/other/infoplus-api-proxy.conf', 'w') | |
| proxyFile.write("#########################################\n") | |
| proxyFile.write("## turn on api urls ##\n") | |
| proxyFile.write("#########################################\n") | |
| proxyFile.write("\n") | |
| proxyFile.write("SSLProxyEngine On\n") | |
| proxyFile.write("RequestHeader set Front-End-Https 'On'\n") | |
| proxyFile.write("RewriteEngine On\n") | |
| proxyFile.write("\n") | |
| proxyFile.write("RewriteRule ^/(.*)/api/ " + qaProxy + "/%{REQUEST_URI} [P]\n") | |
| proxyFile.write("ProxyPassReverse /login/api/ " + qaProxy + "/login/api/\n") | |
| proxyFile.write("\n") | |
| proxyFile.write("CacheDisable *\n") | |
| proxyFile.write("\n") | |
| proxyFile.close() | |
| #Include apache reverse proxy configuration file in httpd.conf | |
| print "Adding include proxy to httpd.conf." | |
| httpdFile = open('/etc/apache2/httpd.conf', 'a') | |
| httpdFile.write("\n") | |
| httpdFile.write("Include /etc/apache2/other/infoplus-api-proxy.conf\n") | |
| httpdFile.close() | |
| except IOError as (errno,strerror): | |
| print "I/O error({0}): {1}".format(errno, strerror) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment