From b698a34e3aa00108569ee8d0f3c76eb31bcbf96f Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Mon, 25 May 2015 22:15:12 -0700 Subject: [PATCH] initial commit --- .gitignore | 8 +++ bower.json | 26 ++++++++++ gulpfile.js | 44 ++++++++++++++++ less/style.less | 5 ++ main.go | 117 +++++++++++++++++++++++++++++++++++++++++++ package.json | 20 ++++++++ scripts/script.js | 27 ++++++++++ templates/index.tmpl | 12 +++++ 8 files changed, 259 insertions(+) create mode 100644 .gitignore create mode 100644 bower.json create mode 100644 gulpfile.js create mode 100644 less/style.less create mode 100644 main.go create mode 100644 package.json create mode 100644 scripts/script.js create mode 100644 templates/index.tmpl diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2b46cfc --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +config.json +coffee +gin-bin +node_modules/ +bower_components/ +public/lib/* +public/css/* +public/scripts/* diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..a05b416 --- /dev/null +++ b/bower.json @@ -0,0 +1,26 @@ +{ + "name": "coffee", + "version": "1.0.0", + "authors": [ + "Justin Beckwith " + ], + "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" + } +} diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..75362b9 --- /dev/null +++ b/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", ]); diff --git a/less/style.less b/less/style.less new file mode 100644 index 0000000..2e1e5e2 --- /dev/null +++ b/less/style.less @@ -0,0 +1,5 @@ +html, body, #map-canvas { + height: 100%; + margin: 0; + padding: 0; +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..ba22de3 --- /dev/null +++ b/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 +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..58cf6cb --- /dev/null +++ b/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" +} diff --git a/scripts/script.js b/scripts/script.js new file mode 100644 index 0000000..830cdc2 --- /dev/null +++ b/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); diff --git a/templates/index.tmpl b/templates/index.tmpl new file mode 100644 index 0000000..87aed42 --- /dev/null +++ b/templates/index.tmpl @@ -0,0 +1,12 @@ + + + + + + + + +
+ +