Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed May 26, 2015
0 parents commit b698a34
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
@@ -0,0 +1,8 @@
config.json
coffee
gin-bin
node_modules/
bower_components/
public/lib/*
public/css/*
public/scripts/*
26 changes: 26 additions & 0 deletions bower.json
@@ -0,0 +1,26 @@
{
"name": "coffee",
"version": "1.0.0",
"authors": [
"Justin Beckwith <justin.beckwith@gmail.com>"
],
"description": "A silly app I use to find coffee.",
"keywords": [
"go",
"coffee",
"gin"
],
"license": "MIT",
"homepage": "jbeckwith.com",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
],
"dependencies": {
"fetch": "~0.8.2",
"es6-promise": "~2.1.0"
}
}
44 changes: 44 additions & 0 deletions gulpfile.js
@@ -0,0 +1,44 @@
var gulp = require("gulp");
var mainBowerFiles = require('main-bower-files');
var gulpgo = require("gulp-go");
var livereload = require('gulp-livereload');
var less = require('gulp-less');
var path = require('path');
var uglify = require('gulp-uglify');

var go;

gulp.task("go-run", function() {
go = gulpgo.run("main.go", [], {cwd: __dirname, stdio: 'inherit'});
});

gulp.task("bower-files", function(){
gulp.src(mainBowerFiles()).pipe(gulp.dest("./public/lib"));
});

gulp.task('less', function() {
gulp.src('./less/**/*.less')
.pipe(less({
paths: [ path.join(__dirname, 'less', 'includes') ]
}))
.pipe(gulp.dest('./public/css'))
.pipe(livereload());
});

gulp.task('compress', function() {
return gulp.src('scripts/*.js')
.pipe(uglify())
.pipe(gulp.dest('public/scripts'))
.pipe(livereload());
});

gulp.task("watch", function() {
livereload.listen();
gulp.watch('./less/**/*.less', ['less']);
gulp.watch('./scripts/**/*.js', ['compress']);
gulp.watch([__dirname+"/**/*.go"]).on("change", function() {
go.restart();
});
})

gulp.task("default", ["bower-files", "go-run", "watch", ]);
5 changes: 5 additions & 0 deletions less/style.less
@@ -0,0 +1,5 @@
html, body, #map-canvas {
height: 100%;
margin: 0;
padding: 0;
}
117 changes: 117 additions & 0 deletions main.go
@@ -0,0 +1,117 @@
package main

import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"os"
"strconv"

"github.com/JustinBeckwith/go-yelp/yelp"
"github.com/gin-gonic/contrib/static"
"github.com/gin-gonic/gin"
"github.com/guregu/null"
)

func main() {

// set up the server
r := gin.Default()
r.Use(static.Serve("/", static.LocalFile("public", false)))
r.LoadHTMLGlob("templates/*")

// index route
r.GET("/", func(c *gin.Context) {
obj := gin.H{"titler": "Main website"}
c.HTML(http.StatusOK, "index.tmpl", obj)
})

r.GET("/GetCoffee", func(c *gin.Context) {
q := c.Request.URL.Query()

// get the latitude from the query string
strlat := q.Get("lat")
lat, err := strconv.ParseFloat(strlat, 64)
if err != nil {
c.JSON(400, gin.H{
"status": "failed",
"error": "invalid latitude",
})
return
}

// get the longitude from the query string
strlon := q.Get("lon")
lon, err := strconv.ParseFloat(strlon, 64)
if err != nil {
c.JSON(400, gin.H{
"status": "failed",
"error": "invalid longitude",
})
return
}

// get the yelp API keys from config or environment
options, err := getOptions()
if err != nil {
c.JSON(400, gin.H{
"status": "failed",
"error": "unable to query yelp api",
})
}

// create a new yelp client with the auth keys
client := yelp.New(options, nil)

// search for all coffee near the given coordinates
searchOptions := yelp.SearchOptions{
CoordinateOptions: &yelp.CoordinateOptions{
Latitude: null.FloatFrom(lat),
Longitude: null.FloatFrom(lon),
Accuracy: null.FloatFromPtr(nil),
Altitude: null.FloatFromPtr(nil),
AltitudeAccuracy: null.FloatFromPtr(nil),
},
}
results, err := client.DoSearch(searchOptions)
if err != nil {
c.JSON(400, gin.H{
"status": "failed",
"error": "unable to query yelp api",
})
}

c.JSON(http.StatusOK, results)

})

// run the server
r.Run(":8080")
}

// getOptions obtains the keys required to use the Yelp API from a config file
// or from environment variables.
func getOptions() (options *yelp.AuthOptions, err error) {

var o *yelp.AuthOptions

// start by looking for the keys in config.json
data, err := ioutil.ReadFile("config.json")
if err != nil {
// if the file isn't there, check environment variables
o = &yelp.AuthOptions{
ConsumerKey: os.Getenv("CONSUMER_KEY"),
ConsumerSecret: os.Getenv("CONSUMER_SECRET"),
AccessToken: os.Getenv("ACCESS_TOKEN"),
AccessTokenSecret: os.Getenv("ACCESS_TOKEN_SECRET"),
}
if o.ConsumerKey == "" || o.ConsumerSecret == "" || o.AccessToken == "" || o.AccessTokenSecret == "" {
return o, errors.New("to use the sample, keys must be provided either in a config.json file at the root of the repo, or in environment variables")
}
} else {
err = json.Unmarshal(data, &o)
return o, err
}
return o, nil
}
20 changes: 20 additions & 0 deletions package.json
@@ -0,0 +1,20 @@
{
"name": "coffee",
"version": "1.0.0",
"description": "A silly app I use to find coffee.",
"main": "gulpfile.js",
"dependencies": {},
"devDependencies": {
"gulp": "^3.8.11",
"gulp-go": "^1.1.0",
"gulp-less": "^3.0.3",
"gulp-livereload": "^3.8.0",
"gulp-uglify": "^1.2.0",
"main-bower-files": "^2.8.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Justin Beckwith",
"license": "MIT"
}
27 changes: 27 additions & 0 deletions scripts/script.js
@@ -0,0 +1,27 @@
function initialize() {
var mapOptions = {
center: {
lat: 47.649038,
lng: -122.3502358
},
zoom: 16
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

navigator.geolocation.getCurrentPosition(function(position) {
getCoffee(position);
});
}

function getCoffee(position) {
fetch('/GetCoffee?lat=' + position.coords.latitude + "&lon=" + position.coords.longitude)
.then(function(response) {
return response.json()
}).then(function(json) {
console.log('parsed json', json)
}).catch(function(ex) {
console.log('parsing failed', ex)
});
}

google.maps.event.addDomListener(window, 'load', initialize);
12 changes: 12 additions & 0 deletions templates/index.tmpl
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/style.css"/>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAAOr4bCj0Yr2mj5us0ABLhnIfTYQnsBxg">
</script>
<script type="text/javascript" src="scripts/script.js"></script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>

0 comments on commit b698a34

Please sign in to comment.