postserve/server.py

49 lines
1.5 KiB
Python
Raw Normal View History

import asyncpg
2017-05-04 19:30:28 -04:00
import io
import os
import logging
import tornado
import tornado.web
from tornado.log import enable_pretty_logging
2019-10-21 05:28:34 +03:00
from functools import partial
2017-05-04 19:30:28 -04:00
import mercantile
import pyproj
import yaml
import sys
import itertools
log = logging.getLogger('tornado.application')
2017-05-04 19:30:28 -04:00
def GetTM2Source(file):
with open(file,'r') as stream:
tm2source = yaml.load(stream)
return tm2source
dsn = 'postgresql://'+os.getenv('POSTGRES_USER','openmaptiles')+':'+os.getenv('POSTGRES_PASSWORD','openmaptiles')+'@'+os.getenv('POSTGRES_HOST','postgres')+':'+os.getenv('POSTGRES_PORT','5432')+'/'+os.getenv('POSTGRES_DB','openmaptiles')
2017-05-04 19:30:28 -04:00
class GetTile(tornado.web.RequestHandler):
def initialize(self, pool):
self.pool = pool
async def get(self, zoom,x,y):
2017-05-04 19:30:28 -04:00
self.set_header("Content-Type", "application/x-protobuf")
self.set_header("Content-Disposition", "attachment")
self.set_header("Access-Control-Allow-Origin", "*")
async with self.pool.acquire() as connection:
2019-10-21 05:28:34 +03:00
tile = await connection.fetchval("SELECT gettile($1, $2, $3)", int(zoom), int(x), int(y))
self.write(tile)
2017-05-04 19:30:28 -04:00
def m():
2019-10-21 05:28:34 +03:00
enable_pretty_logging()
io_loop = tornado.ioloop.IOLoop.current()
pool = io_loop.run_sync(partial(asyncpg.create_pool, dsn=dsn))
application = tornado.web.Application([(r"/tiles/([0-9]+)/([0-9]+)/([0-9]+).pbf", GetTile, dict(pool=pool))])
print("Postserve started..")
application.listen(int(os.getenv("LISTEN_PORT", "8080")))
io_loop.start()
2017-05-04 19:30:28 -04:00
if __name__ == "__main__":
m()