Skip to content

Commit 0948092

Browse files
committed
Initial Commit
0 parents  commit 0948092

File tree

11 files changed

+390
-0
lines changed

11 files changed

+390
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Major League Hacking (MLH)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Participants' Home Towns
2+
3+
## Tecnologies used in this project
4+
5+
## NodeJS
6+
7+
Node.js is a platform built on Chrome's JavaScript runtime for easily building fast and scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices. NodeJS was the platform in which we built this application.
8+
9+
## NPM packages used
10+
11+
### [express](https://www.npmjs.com/package/express)
12+
13+
Fast, unopinionated, minimalist web framework for node. This is the framework with which we built the application.
14+
15+
### [dotenv](https://www.npmjs.com/package/dotenv)
16+
17+
Dotenv is a zero-dependency module that loads environment variables from a `.env` file into `process.env`. Storing configuration in the environment separate from code is based on The Twelve-Factor App methodology.
18+
19+
### [node-geocoder](https://www.npmjs.com/package/node-geocoder)
20+
21+
Node library for geocoding and reverse geocoding.
22+
23+
### [ejs](https://www.npmjs.com/package/ejs)
24+
25+
Embedded JavaScript templates
26+
27+
## Prerequisites to run this application:
28+
29+
### Google Maps API Key
30+
31+
To use the Google Maps JavaScript API, you will ned an API Key. The [API key](https://developers.google.com/maps/documentation/javascript/get-api-key) is used to track API requests associated with your project for usage and billing. To learn more about API keys, see the API Key Best Practices and the FAQs.
32+
33+
Read the [tutorial](docs/tutorial.md) and configure your [API Key](https://developers.google.com/maps/documentation/javascript/get-api-key)
34+
35+
36+
## Adding a new location
37+
38+
The App reads its locations from the `locations.txt` file located in the root folder. It will consider each line a different location, so the file should look something like this:
39+
40+
```txt
41+
New York, USA
42+
Brazil
43+
Japan
44+
```
45+
46+
To add a new location, simply append a new line to the locations.txt file
47+
48+
There is also a `locations.txt.example` file that can be used to test the app. Copy its content to locations.txt and restart the app.
49+
50+
## Running the application:
51+
52+
To run this application locally, you'll need to:
53+
54+
* Clone this repository:
55+
```
56+
git clone https://github.com/MLH/localhost-github.git
57+
cd localhost-github
58+
npm install
59+
```
60+
61+
* Set up environment variables:
62+
63+
There is a `.env.example` file that can be used to configure the app. Simply create a copy named `.env`
64+
65+
```txt
66+
PORT= # Port to be used by the app
67+
API_KEY= # Google Maps API KEY
68+
```
69+
70+
Now inside the root folder run:
71+
```
72+
npm start
73+
```
74+
75+
Now you can access locally in your browser the address `http://localhost:3000` and see your locations map.

index.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const server = require("./src/server");
2+
server.start();

locations.txt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
New York, USA
2+
Tokyo, Japan
3+
Paris, France
4+
Montreal, Canada
5+
Goiania, Brazil
6+
Vienna, Austria
7+
Melbourne, Australia
8+
Osaka, Japan
9+
Calgary, Canada
10+
Sydney, Australia
11+
Vancouver, Canada
12+
Toronto, Canada
13+
Tokyo, Japan
14+
Copenhagen, Denmark
15+
Adelaide, Australia
16+
Miami, Florida

package.json

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "maps",
3+
"version": "1.0.0",
4+
"description": "display participant locations in a map",
5+
"main": "server.js",
6+
"directories": {
7+
"doc": "docs"
8+
},
9+
"scripts": {
10+
"start": "node index.js"
11+
},
12+
"engines": {
13+
"node": "10.x"
14+
},
15+
"repository": {
16+
"type": "git",
17+
"url": "git+https://github.com/MLH/localhost-github.git"
18+
},
19+
"author": "Halisson Bruno",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/MLH/localhost-github/issues"
23+
},
24+
"homepage": "https://github.com/MLH/localhost-github#readme",
25+
"dependencies": {
26+
"dotenv": "^6.2.0",
27+
"ejs": "^2.6.1",
28+
"express": "^4.16.4",
29+
"morgan": "^1.9.1",
30+
"node-geocoder": "^3.22.0"
31+
}
32+
}

src/addresses.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const fs = require("fs");
2+
3+
const DEFAULT_FILE_PATH = "../locations.txt";
4+
5+
async function getAddresses(filePath = DEFAULT_FILE_PATH) {
6+
const fileContent = await fs.promises.readFile('locations.txt');
7+
return String(fileContent).split("\n");
8+
}
9+
10+
function getExampleAddresses() {
11+
return [];
12+
}
13+
14+
module.exports = {
15+
getAddresses,
16+
getExampleAddresses
17+
};

src/locations.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const fs = require("fs");
2+
const NodeGeocoder = require("node-geocoder");
3+
4+
var options = {
5+
provider: "google",
6+
httpAdapter: "https",
7+
apiKey: process.env.GOOGLE_API_KEY,
8+
};
9+
10+
var geocoder = NodeGeocoder(options);
11+
12+
async function getLocations(addresses) {
13+
const locations = await geocoder
14+
.batchGeocode(addresses)
15+
.filter(({ error }) => !error)
16+
.map(({ error, value: [result, ...results] }) => ({
17+
lat: result.latitude,
18+
lng: result.longitude
19+
}));
20+
return locations;
21+
}
22+
23+
module.exports = {
24+
getLocations
25+
};

src/server.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require("dotenv").config();
2+
const express = require("express");
3+
const morgan = require("morgan");
4+
const path = require("path");
5+
const Addresses = require("./addresses");
6+
const Locations = require("./locations");
7+
8+
const app = express();
9+
const { PORT } = process.env || 3000;
10+
const googleMapsApiKey = process.env.GOOGLE_API_KEY;
11+
12+
app.use("/static", express.static("static")); // Exposes static folder to serve images and styles
13+
app.use(morgan("combined")); // Logs requests
14+
app.set("view engine", "ejs"); // Set default view engine
15+
16+
const start = async function() {
17+
console.info("Loading addresses.");
18+
const addresses = await Addresses.getAddresses();
19+
console.info(
20+
"Converting addresses into locations. Please notice this may take a while if there are many addresses..."
21+
);
22+
const locations = await Locations.getLocations(addresses);
23+
console.info("Starting application");
24+
app.get("/", function(req, res) {
25+
res.render("index", {
26+
locations,
27+
apiKey: googleMapsApiKey,
28+
});
29+
});
30+
31+
app.listen(PORT, function() {
32+
console.log("Maps app listening on port " + PORT);
33+
});
34+
};
35+
36+
module.exports = {
37+
start
38+
};

0 commit comments

Comments
 (0)