implement tilesets struct

This commit is contained in:
Stepan Kuzmin 2017-11-26 15:30:51 +03:00
parent 2a7af41782
commit a5c8294d3c
3 changed files with 59 additions and 3 deletions

View File

@ -1,4 +1,6 @@
use std::error::Error;
use std::collections::HashMap;
use iron::typemap::Key;
use iron::prelude::{Plugin, Request};
use persistent::Read;
@ -42,3 +44,44 @@ pub fn get_tile(conn: PostgresPooledConnection, schema: &str, table: &str, z: &s
let tile = rows.get(0).get("st_asmvt");
Ok(tile)
}
#[derive(Debug)]
pub struct Tileset {
pub schema: String,
pub table: String,
pub geometry_column: String,
pub srid: i32,
pub _type: String
}
pub struct Tilesets;
impl Key for Tilesets { type Value = HashMap<String, Tileset>; }
pub fn get_tilesets(conn: PostgresPooledConnection) -> Result<HashMap<String, Tileset>, Box<Error>> {
let query = "
select
f_table_schema, f_table_name, f_geometry_column, srid, type
from geometry_columns
";
let mut tilesets = HashMap::new();
let rows = try!(conn.query(&query, &[]));
for row in &rows {
let schema = row.get("f_table_schema");
let table = row.get("f_table_name");
let id = format!("{}.{}", schema, table);
let tileset = Tileset {
schema: schema,
table: table,
geometry_column: row.get("f_geometry_column"),
srid: row.get("srid"),
_type: row.get("type")
};
tilesets.insert(id, tileset);
}
Ok(tilesets)
}

View File

@ -22,7 +22,13 @@ fn main() {
println!("Connecting to postgres: {}", conn_string);
match db::setup_connection_pool(&conn_string, 10) {
Ok(pool) => chain.link(Read::<db::DB>::both(pool)),
Ok(pool) => {
let conn = pool.get().unwrap();
let tilesets = db::get_tilesets(conn).unwrap();
chain.link(Read::<db::Tilesets>::both(tilesets));
chain.link(Read::<db::DB>::both(pool));
},
Err(error) => {
eprintln!("Error connectiong to postgres: {}", error);
std::process::exit(-1);

View File

@ -1,6 +1,8 @@
use url::Url;
use regex::{Regex, Captures};
use iron::prelude::{Plugin};
use iron::{status, mime, Request, Response, IronResult};
use persistent::Read;
use super::db;
@ -38,6 +40,12 @@ fn tileset(_req: &mut Request, _caps: Captures) -> IronResult<Response> {
}
fn tile(req: &mut Request, caps: Captures) -> IronResult<Response> {
let tilesets = req.get::<Read<db::Tilesets>>().unwrap();
let tileset = match tilesets.get(&caps["tileset"]) {
Some(tileset) => tileset,
None => return Ok(Response::with((status::NotFound)))
};
let conn = match db::get_connection(req) {
Ok(conn) => conn,
Err(error) => {
@ -46,8 +54,7 @@ fn tile(req: &mut Request, caps: Captures) -> IronResult<Response> {
}
};
let tile = match db::get_tile(conn, &caps["schema"], &caps["table"], &caps["z"], &caps["x"], &caps["y"]) {
let tile = match db::get_tile(conn, &tileset.schema, &tileset.table, &caps["z"], &caps["x"], &caps["y"]) {
Ok(tile) => tile,
Err(error) => {
eprintln!("Couldn't get a tile: {}", error);