Merge pull request #1 from kdungs/webservice

Adding the beginnings of a webservice.
This commit is contained in:
Tim Head 2014-11-17 14:51:02 +01:00
commit ce8fbf5f04
4 changed files with 110 additions and 0 deletions

View File

@ -78,10 +78,22 @@ have similar `these-3-words` hashes
The other CERN site is [here][othercernmap].
webservice
==========
The file `server.py` provides a tiny webservice that allows to display a
location given by three words on a Google Maps map.
The server requires [bottle.py][bottlepy] to be installed. It can be run
locally by typing `./server.py` or `python server.py` respectively.
this is a [@betatim][betatim] kind of idea
[humanhash]: https://github.com/zacharyvoase/humanhash
[geohash]: https://code.google.com/p/python-geohash/
[cernmap]: https://www.google.ch/maps/place/46%C2%B013'56.4%22N+6%C2%B003'19.5%22E/@46.2323356,6.0554194,17z/data=!3m1!4b1!4m2!3m1!1s0x0:0x0
[othercernmap]: https://www.google.ch/maps/place/46%C2%B015'24.5%22N+6%C2%B003'24.4%22E/@46.256811,6.056792,14z/data=!4m2!3m1!1s0x0:0x0
[bottlepy]: http://bottlepy.org/
[betatim]: https://twitter.com/betatim

27
server.py Executable file
View File

@ -0,0 +1,27 @@
#!/usr/bin/env python
from bottle import (
get,
run,
template
)
import thesethreewords as these
@get('/')
def index():
return template('index', err=None)
@get('/<threewords>')
def showMap(threewords):
try:
lat, lng = these.decode(threewords)
return template('map', lat=lat, lng=lng)
except:
return template('index',
err="Could not find location {}".format(threewords))
if __name__ == '__main__':
run(host='localhost', port=8080)

47
views/index.html Normal file
View File

@ -0,0 +1,47 @@
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Meta -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>these-3-words Map</title>
<!-- Style -->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
% if err:
<aside class="alert alert-danger" role="alert">Error: {{ err }}</aside>
% end
<main class="jumbotron">
<h1>These3Words Map</h1>
<p class="lead">
Find a location anywhere in the world identified by three simple words.
</p>
<p id="input3wordsContainer">
<input type="text" class="form-control" id="input3words" placeholder="engirt-aleutic-canun">
</p>
<p>
<button id="button3words" class="btn btn-lg btn-primary">Find on Map</button>
</p>
</main>
<script>
(function() {
var default3words = 'engirt-aleutic-canun';
var threewordsField = document.getElementById('input3words');
document.getElementById('button3words').addEventListener('click', function(evt) {
var threewords = threewordsField.value;
if (threewords.length === 0) {
threewords = default3words;
}
if (/\w+-\w+-\w+/.test(threewords)) {
window.location = '/' + threewords;
} else {
document.getElementById('input3wordsContainer').classList.add('has-error');
}
});
})();
</script>
</div>
</body>
</html>

24
views/map.html Normal file
View File

@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
html, body, #map-canvas { height: 100%; margin: 0; padding: 0;}
</style>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js">
</script>
<script type="text/javascript">
function initialize() {
var mapOptions = {
center: { lat: {{ lat }}, lng: {{ lng }}},
zoom: 14
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>