changeset 91:6a45e46f0c05

myw: use new redirect_app
author paulo
date Thu, 06 Apr 2017 22:42:03 -0700
parents 302eebbf025f
children 96a5e2055acb
files myw/.htaccess myw/index.fcgi myw/index.html myw/redirect_app.py myw/redirect_test_server.py
diffstat 5 files changed, 95 insertions(+), 21 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/myw/.htaccess	Thu Apr 06 22:42:03 2017 -0700
     1.3 @@ -0,0 +1,4 @@
     1.4 +Redirect permanent "/myw/desktop" "http://forecast.weather.gov/MapClick.php?lat=37.63048605200049&lon=-122.41107706299971"
     1.5 +Redirect permanent "/myw/mobile" "http://mobile.weather.gov/index.php?lat=37.61961&lon=-122.36558"
     1.6 +Redirect permanent "/myw/radar" "https://radar.weather.gov/lite/N0R/MUX_loop.gif"
     1.7 +Redirect permanent "/myw/hourly" "http://forecast.weather.gov/meteograms/Plotter.php?lat=37.6305&lon=-122.4111&wfo=MTR&zcode=CAZ508&gset=18&gdiff=3&unit=0&tinfo=PY8&ahour=0&pcmd=11011111111110000000000000000000000000000000000000000000000&lg=en&indu=1!1!1!&dd=&bw=&hrspan=48&pqpfhr=6&psnwhr=6"
     1.8 \ No newline at end of file
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/myw/index.fcgi	Thu Apr 06 22:42:03 2017 -0700
     2.3 @@ -0,0 +1,8 @@
     2.4 +#!/usr/bin/env python
     2.5 +
     2.6 +from flup.server.fcgi import WSGIServer
     2.7 +
     2.8 +from redirect_app import app
     2.9 +
    2.10 +
    2.11 +WSGIServer(app).run()
     3.1 --- a/myw/index.html	Fri Feb 24 00:30:54 2017 -0700
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,21 +0,0 @@
     3.4 -<html>
     3.5 -<head>
     3.6 -<title>myw</title>
     3.7 -<link rel="stylesheet" type="text/css" href="index.css">
     3.8 -</head>
     3.9 -
    3.10 -<body>
    3.11 -<div id="desktopLink">
    3.12 -  <a href="http://forecast.weather.gov/MapClick.php?lat=37.63048605200049&lon=-122.41107706299971">Desktop</a>
    3.13 -</div>
    3.14 -<div id="mobileLink">
    3.15 -  <a href="http://mobile.weather.gov/index.php?lat=37.61961&lon=-122.36558">Mobile</a>
    3.16 -</div>
    3.17 -<div id="radarLink">
    3.18 -  <a href="http://radar.weather.gov/lite/N0R/MUX_loop.gif">Radar</a>
    3.19 -</div>
    3.20 -<div id="hourlyLink">
    3.21 -  <a href="http://forecast.weather.gov/meteograms/Plotter.php?lat=37.6305&lon=-122.4111&wfo=MTR&zcode=CAZ508&gset=18&gdiff=3&unit=0&tinfo=PY8&ahour=0&pcmd=11011111111110000000000000000000000000000000000000000000000&lg=en&indu=1!1!1!&dd=&bw=&hrspan=48&pqpfhr=6&psnwhr=6">Hourly</a>
    3.22 -</div>
    3.23 -</body>
    3.24 -</html>
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/myw/redirect_app.py	Thu Apr 06 22:42:03 2017 -0700
     4.3 @@ -0,0 +1,45 @@
     4.4 +import os
     4.5 +
     4.6 +import html
     4.7 +
     4.8 +
     4.9 +TITLE = "myw"
    4.10 +REDIRECT_FILE = ".htaccess"
    4.11 +
    4.12 +
    4.13 +def main(environ):
    4.14 +	links = []
    4.15 +	
    4.16 +	with open(REDIRECT_FILE) as f:
    4.17 +		for l in f:
    4.18 +			l = l.lstrip()
    4.19 +			if l.startswith("Redirect"):
    4.20 +				ls = l.split()
    4.21 +				if len(ls) == 4:
    4.22 +					path = ls[2].strip('"')
    4.23 +					links.append(os.path.basename(path))
    4.24 +	
    4.25 +	root = html.HTML("html")
    4.26 +	
    4.27 +	header = root.header
    4.28 +	header.title(TITLE)
    4.29 +	header.link(rel="stylesheet", type="text/css", href="index.css")
    4.30 +	
    4.31 +	body = root.body(klass="body")
    4.32 +	
    4.33 +	for i in links:
    4.34 +		div = body.div()
    4.35 +		div.a(i.capitalize(), href=i)
    4.36 +	
    4.37 +	return unicode(root).encode("utf-8")
    4.38 +
    4.39 +
    4.40 +def app(environ, start_response):
    4.41 +	response_body = main(environ)
    4.42 +	response_headers = [
    4.43 +		("Content-Type", "text/html; charset=UTF-8"),
    4.44 +		("Content-Length", str(len(response_body))),
    4.45 +	]
    4.46 +	start_response("200 OK", response_headers)
    4.47 +
    4.48 +	return [response_body]
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/myw/redirect_test_server.py	Thu Apr 06 22:42:03 2017 -0700
     5.3 @@ -0,0 +1,38 @@
     5.4 +import os
     5.5 +import sys
     5.6 +import signal
     5.7 +
     5.8 +import cherrypy
     5.9 +from cherrypy import wsgiserver
    5.10 +
    5.11 +import redirect_app
    5.12 +
    5.13 +
    5.14 +def sighandler(signum, frame):
    5.15 +	sys.stderr.write("Caught signal: %s \n" % signum)
    5.16 +	server.stop()
    5.17 +
    5.18 +
    5.19 +class FileServerRoot:
    5.20 +	def default(self, *args):
    5.21 +		if len(args) == 0:
    5.22 +			raise cherrypy.HTTPError(404)
    5.23 +
    5.24 +		filepath = os.path.abspath(os.path.join(*args))
    5.25 +		return cherrypy.lib.static.serve_file(filepath)
    5.26 +
    5.27 +	default.exposed = True
    5.28 +
    5.29 +
    5.30 +if __name__ == "__main__":
    5.31 +	fileServerApp = cherrypy.Application(FileServerRoot())
    5.32 +	dispatcher = wsgiserver.WSGIPathInfoDispatcher({
    5.33 +		"/index.fcgi": redirect_app.app,
    5.34 +		"": fileServerApp,
    5.35 +	})
    5.36 +	server = wsgiserver.CherryPyWSGIServer(('0.0.0.0', 8000), dispatcher)
    5.37 +
    5.38 +	signal.signal(signal.SIGINT, sighandler)
    5.39 +	signal.signal(signal.SIGTERM, sighandler)
    5.40 +
    5.41 +	server.start()