Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #66 from markbates/goth
Browse files Browse the repository at this point in the history
Add a generator for Goth closes #65
  • Loading branch information
markbates authored Dec 19, 2016
2 parents f0aad36 + 8fe888f commit 3561891
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 15 deletions.
35 changes: 20 additions & 15 deletions buffalo/cmd/app_generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ func main() {
const nApp = `package actions
import (
"net/http"
"os"
"github.com/markbates/buffalo"
Expand All @@ -76,24 +75,31 @@ import (
"github.com/markbates/going/defaults"
)
// ENV is used to help switch settings based on where the
// application is being run. Default is "development".
var ENV = defaults.String(os.Getenv("GO_ENV"), "development")
// HOST that the application can be found. Default is "localhost".
var HOST = defaults.String(os.Getenv("HOST"), "http://127.0.0.1:3000")
var app *buffalo.App
// App is where all routes and middleware for buffalo
// should be defined. This is the nerve center of your
// application.
func App() http.Handler {
a := buffalo.Automatic(buffalo.Options{
Env: ENV,
})
{{if .withPop -}}
a.Use(middleware.PopTransaction(models.DB))
{{end -}}
a.ServeFiles("/assets", assetsPath())
a.GET("/", HomeHandler)
func App() *buffalo.App {
if app == nil {
app = buffalo.Automatic(buffalo.Options{
Env: ENV,
})
{{if .withPop -}}
app.Use(middleware.PopTransaction(models.DB))
{{end -}}
app.ServeFiles("/assets", assetsPath())
app.GET("/", HomeHandler)
}
return a
return app
}
`

Expand Down Expand Up @@ -202,14 +208,13 @@ const nGriftRoutes = `package grifts
import (
"os"
"github.com/markbates/buffalo"
. "github.com/markbates/grift/grift"
"{{.actionsPath}}"
"github.com/olekukonko/tablewriter"
)
var _ = Add("routes", func(c *Context) error {
a := actions.App().(*buffalo.App)
a := actions.App()
routes := a.Routes()
table := tablewriter.NewWriter(os.Stdout)
Expand Down
1 change: 1 addition & 0 deletions buffalo/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ func init() {
generateCmd.AddCommand(generate.BootstrapCmd)
generateCmd.AddCommand(generate.BootswatchCmd)
generateCmd.AddCommand(generate.ResourceCmd)
generateCmd.AddCommand(generate.GothCmd)
RootCmd.AddCommand(generateCmd)
}
90 changes: 90 additions & 0 deletions buffalo/cmd/generate/goth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright © 2016 Mark Bates <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package generate

import (
"errors"
"path/filepath"

"github.com/markbates/gentronics"
"github.com/spf13/cobra"
)

// GothCmd generates a actions/goth.go file configured to the specified providers.
var GothCmd = &cobra.Command{
Use: "goth [provider provider...]",
Short: "Generates a actions/goth.go file configured to the specified providers.",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("You must specifiy at least one provider!")
}
return NewGothGenerator().Run(".", gentronics.Data{
"providers": args,
})
},
}

// NewGothGenerator a actions/goth.go file configured to the specified providers.
func NewGothGenerator() *gentronics.Generator {
g := gentronics.New()
f := gentronics.NewFile(filepath.Join("actions", "goth.go"), gGoth)
g.Add(f)
g.Add(Fmt)
return g
}

var gGoth = `package actions
import (
"fmt"
"os"
"github.com/markbates/buffalo"
"github.com/markbates/goth"
"github.com/markbates/goth/gothic"
{{ range .providers -}}
"github.com/markbates/goth/providers/{{. | downcase}}"
{{ end -}}
)
func init() {
gothic.Store = App().SessionStore
goth.UseProviders(
{{ range .providers -}}
{{.|downcase}}.New(os.Getenv("{{.|upcase}}_KEY"), os.Getenv("{{.|upcase}}_SECRET"), fmt.Sprintf("%s%s", HOST, "/auth/{{.|downcase}}/callback")),
{{ end -}}
)
app := App().Group("/auth")
app.GET("/{provider}", buffalo.WrapHandlerFunc(gothic.BeginAuthHandler))
app.GET("/{provider}/callback", AuthCallback)
}
func AuthCallback(c buffalo.Context) error {
user, err := gothic.CompleteUserAuth(c.Response(), c.Request())
if err != nil {
return c.Error(401, err)
}
// Do something with the user, maybe register them/sign them in
return c.Render(200, r.JSON(user))
}
`

0 comments on commit 3561891

Please sign in to comment.