# FORDECyT 2018-10

Uses a stripped version of [pgserver](https://github.com/anneb/pgserver) (PostGIS http API server) to serve cached mapbox vector tiles (mvt), geojson, geobuf

Built on [Node Express](https://expressjs.com/)

## Requirements
* Access to a [PostGIS](https://postgis.net) server
* [Node and npm](https://nodejs.org/en/download/)

## PostgreSQL user
User should **only** have SELECT right on tables. You can create one as follows:

```sql
-- Create a group
CREATE ROLE readaccess;

-- Grant access to existing tables
GRANT USAGE ON SCHEMA public TO readaccess;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO readaccess;

-- Create a final user with password
CREATE USER <user> WITH PASSWORD '<pw>';
GRANT readaccess TO <user>;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO <user>;

-- Grant access to future tables on role and user
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readaccess;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO <user>;
```

Make sure to edit /etc/postgresql/10/main/pg_hba.conf line

`local   all     all     peer`

to

`local   all     all     md5`

## Apache and NodeJS to serve through port :80

Edit your site on `etc/apache2/sites-enabled`.

Either at the end of your site config or at the end the VirtualHost directive of add the following:
```
<VirtualHost *:80>
    ...
    ProxyRequests on
    ProxyPass /[path]/ http://localhost:[port]/[path]/
</VirtualHost>
```

Enable the following apache modules:
```
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests
```
And restart Apache:
```
sudo service apache2 restart
```
Now you can serve your app through port 80 instead of 8090 or something else.

NodeJS requests will be routed to port 8090 :)


## Installation
If you don't have git, you can download [a zip file](http://gitlab.geoint.mx/tapiamcclung/fordecyt2019/-/archive/master/fordecyt2019-master.zip) of the project instead.

     git clone this_repository
     cd this_repository
     npm install
     cp config/dbconfig.example.json config/dbconfig.json
     # now edit config/dbconfig.json for your PostGIS database
     node pgserver.js
     or
     npm run dev / npm run start
     # point your browser to [http://localhost:8090](http://localhost:8090) for more info

### Attributions
[pgserver](https://github.com/anneb/pgserver)'s API is based on [Dirt Simple PostGIS http API](https://github.com/tobinbradley/dirt-simple-postgis-http-api)