github.com/lenfree/buffalo@v0.7.3-0.20170207163156-891616ea4064/buffalo/cmd/app_generators.go (about) 1 package cmd 2 3 import ( 4 "os/exec" 5 6 "github.com/gobuffalo/buffalo/buffalo/cmd/generate" 7 "github.com/markbates/gentronics" 8 ) 9 10 func newAppGenerator(data gentronics.Data) *gentronics.Generator { 11 g := gentronics.New() 12 g.Add(gentronics.NewFile("README.md", nREADME)) 13 g.Add(gentronics.NewFile("main.go", nMain)) 14 g.Add(gentronics.NewFile(".buffalo.dev.yml", nRefresh)) 15 g.Add(gentronics.NewFile(".codeclimate.yml", nCodeClimate)) 16 17 if data["ciProvider"] == "travis" { 18 g.Add(gentronics.NewFile(".travis.yml", nTravis)) 19 } 20 21 g.Add(gentronics.NewFile("actions/app.go", nApp)) 22 g.Add(gentronics.NewFile("actions/home.go", nHomeHandler)) 23 g.Add(gentronics.NewFile("actions/home_test.go", nHomeHandlerTest)) 24 g.Add(gentronics.NewFile("actions/render.go", nRender)) 25 g.Add(gentronics.NewFile("grifts/routes.go", nGriftRoutes)) 26 g.Add(gentronics.NewFile("templates/index.html", nIndexHTML)) 27 g.Add(gentronics.NewFile("templates/application.html", nApplicationHTML)) 28 g.Add(gentronics.NewFile(".gitignore", nGitignore)) 29 g.Add(gentronics.NewCommand(generate.GoGet("github.com/markbates/refresh/..."))) 30 g.Add(gentronics.NewCommand(generate.GoInstall("github.com/markbates/refresh"))) 31 g.Add(gentronics.NewCommand(generate.GoGet("github.com/markbates/grift/..."))) 32 g.Add(gentronics.NewCommand(generate.GoInstall("github.com/markbates/grift"))) 33 g.Add(gentronics.NewCommand(generate.GoGet("github.com/motemen/gore"))) 34 g.Add(gentronics.NewCommand(generate.GoInstall("github.com/motemen/gore"))) 35 g.Add(generate.NewWebpackGenerator(data)) 36 g.Add(newSodaGenerator()) 37 g.Add(gentronics.NewCommand(appGoGet())) 38 g.Add(generate.Fmt) 39 40 return g 41 } 42 43 func appGoGet() *exec.Cmd { 44 appArgs := []string{"get", "-t"} 45 if verbose { 46 appArgs = append(appArgs, "-v") 47 } 48 appArgs = append(appArgs, "./...") 49 return exec.Command("go", appArgs...) 50 } 51 52 const nREADME = ` 53 # Welcome to Buffalo! 54 55 Thank you for chosing Buffalo for your web development needs. 56 57 {{#if withPop}} 58 ## Database Setup 59 60 It looks like you chose to set up your application using a {{dbType}} database! Fantastic! 61 62 The first thing you need to do is open up the "database.yml" file and edit it to use the correct usernames, passwords, hosts, etc... that are appropriate for your environment. 63 64 You will also need to make sure that **you** start/install the database of your choice. Buffalo **won't** install and start {{dbType}} for you. 65 66 ### Create Your Databases 67 68 Ok, so you've edited the "database.yml" file and started {{dbType}}, now Buffalo can create the databases in that file for you: 69 70 $ buffalo db create -a 71 {{/if}} 72 73 ## Starting the Application 74 75 Buffalo ships with a command that will watch your application and automatically rebuild the Go binary and any assets for you. To do that run the "buffalo dev" command: 76 77 $ buffalo dev 78 79 If you point your browser to [http://127.0.0.1:3000](http://127.0.0.1:3000) you should see a "Welcome to Buffalo!" page. 80 81 **Congratulations!** You now have your Buffalo application up and running. 82 83 ## What Next? 84 85 We recommend you heading over to [http://gobuffalo.io](http://gobuffalo.io) and reviewing all of the great documentation there. 86 87 Good luck! 88 89 [Powered by Buffalo](http://gobuffalo.io)` 90 91 const nMain = `package main 92 93 import ( 94 "fmt" 95 "log" 96 "net/http" 97 "os" 98 99 "{{actionsPath}}" 100 "github.com/markbates/going/defaults" 101 ) 102 103 func main() { 104 port := defaults.String(os.Getenv("PORT"), "3000") 105 log.Printf("Starting {{name}} on port %s\n", port) 106 log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), actions.App())) 107 } 108 109 ` 110 const nApp = `package actions 111 112 import ( 113 "os" 114 115 "github.com/gobuffalo/buffalo" 116 {{#if withPop }} 117 "github.com/gobuffalo/buffalo/middleware" 118 "{{modelsPath}}" 119 {{/if}} 120 "github.com/markbates/going/defaults" 121 ) 122 123 // ENV is used to help switch settings based on where the 124 // application is being run. Default is "development". 125 var ENV = defaults.String(os.Getenv("GO_ENV"), "development") 126 var app *buffalo.App 127 128 // App is where all routes and middleware for buffalo 129 // should be defined. This is the nerve center of your 130 // application. 131 func App() *buffalo.App { 132 if app == nil { 133 app = buffalo.Automatic(buffalo.Options{ 134 Env: ENV, 135 SessionName: "_{{name}}_session", 136 }) 137 138 {{#if withPop }} 139 app.Use(middleware.PopTransaction(models.DB)) 140 {{/if}} 141 142 app.GET("/", HomeHandler) 143 144 app.ServeFiles("/assets", assetsPath()) 145 } 146 147 return app 148 } 149 ` 150 151 const nRender = `package actions 152 153 import ( 154 "net/http" 155 156 rice "github.com/GeertJohan/go.rice" 157 "github.com/gobuffalo/buffalo/render" 158 "github.com/gobuffalo/buffalo/render/resolvers" 159 ) 160 161 var r *render.Engine 162 163 func init() { 164 r = render.New(render.Options{ 165 HTMLLayout: "application.html", 166 CacheTemplates: ENV == "production", 167 FileResolverFunc: func() resolvers.FileResolver { 168 return &resolvers.RiceBox{ 169 Box: rice.MustFindBox("../templates"), 170 } 171 }, 172 }) 173 } 174 175 func assetsPath() http.FileSystem { 176 box := rice.MustFindBox("../public/assets") 177 return box.HTTPBox() 178 } 179 ` 180 181 const nHomeHandler = `package actions 182 183 import "github.com/gobuffalo/buffalo" 184 185 // HomeHandler is a default handler to serve up 186 // a home page. 187 func HomeHandler(c buffalo.Context) error { 188 return c.Render(200, r.HTML("index.html")) 189 } 190 ` 191 192 const nHomeHandlerTest = `package actions_test 193 194 import ( 195 "testing" 196 197 "{{actionsPath}}" 198 "github.com/markbates/willie" 199 "github.com/stretchr/testify/require" 200 ) 201 202 func Test_HomeHandler(t *testing.T) { 203 r := require.New(t) 204 205 w := willie.New(actions.App()) 206 res := w.Request("/").Get() 207 208 r.Equal(200, res.Code) 209 r.Contains(res.Body.String(), "Welcome to Buffalo!") 210 } 211 ` 212 213 const nIndexHTML = `<div class="row"> 214 <div class="col-md-2"> 215 <img src="/assets/images/logo.svg" alt="" /> 216 </div> 217 <div class="col-md-10"> 218 <h1>Welcome to Buffalo! [v{{version}}]</h1> 219 <h2> 220 <a href="https://github.com/gobuffalo/buffalo"><i class="fa fa-github" aria-hidden="true"></i> https://github.com/gobuffalo/buffalo</a> 221 </h2> 222 <h2> 223 <a href="http://gobuffalo.io"><i class="fa fa-book" aria-hidden="true"></i> Documentation</a> 224 </h2> 225 226 <hr> 227 <h2>Defined Routes</h2> 228 <table class="table table-striped"> 229 <thead> 230 <tr text-align="left"> 231 <th>METHOD</th> 232 <th>PATH</th> 233 <th>HANDLER</th> 234 </tr> 235 </thead> 236 <tbody> 237 \{{#each routes as |r|}} 238 <tr> 239 <td>\{{r.Method}}</td> 240 <td>\{{r.Path}}</td> 241 <td><code>\{{r.HandlerName}}</code></td> 242 </tr> 243 \{{/each}} 244 </tbody> 245 </table> 246 </div> 247 </div> 248 249 ` 250 251 const nApplicationHTML = `<html> 252 <head> 253 <meta charset="utf-8"> 254 <title>Buffalo - {{ titleName }}</title> 255 <link rel="stylesheet" href="/assets/application.css" type="text/css" media="all" /> 256 </head> 257 <body> 258 259 <div class="container"> 260 \{{ yield }} 261 </div> 262 263 <script src="/assets/application.js" type="text/javascript" charset="utf-8"></script> 264 </body> 265 </html> 266 ` 267 268 const nGitignore = `vendor/ 269 **/*.log 270 **/*.sqlite 271 .idea/ 272 bin/ 273 tmp/ 274 node_modules/ 275 .sass-cache/ 276 rice-box.go 277 public/assets/ 278 {{ name }} 279 ` 280 281 const nGriftRoutes = `package grifts 282 283 import ( 284 "os" 285 286 . "github.com/markbates/grift/grift" 287 "{{actionsPath}}" 288 "github.com/olekukonko/tablewriter" 289 ) 290 291 var _ = Add("routes", func(c *Context) error { 292 a := actions.App() 293 routes := a.Routes() 294 295 table := tablewriter.NewWriter(os.Stdout) 296 table.SetHeader([]string{"Method", "Path", "Handler"}) 297 for _, r := range routes { 298 table.Append([]string{r.Method, r.Path, r.HandlerName}) 299 } 300 table.SetCenterSeparator("|") 301 table.SetAlignment(tablewriter.ALIGN_LEFT) 302 table.Render() 303 return nil 304 })` 305 306 const nRefresh = `app_root: . 307 ignored_folders: 308 - vendor 309 - log 310 - logs 311 - assets 312 - public 313 - grifts 314 - tmp 315 - bin 316 - node_modules 317 - .sass-cache 318 included_extensions: 319 - .go 320 - .html 321 - .md 322 - .js 323 - .tmpl 324 build_path: tmp 325 build_delay: 200ns 326 binary_name: {{name}}-build 327 command_flags: [] 328 enable_colors: true 329 log_name: buffalo 330 ` 331 332 const nCodeClimate = `engines: 333 fixme: 334 enabled: true 335 gofmt: 336 enabled: true 337 golint: 338 enabled: true 339 govet: 340 enabled: true 341 exclude_paths: 342 - grifts/**/* 343 - "**/*_test.go" 344 - "*_test.go" 345 - "**_test.go" 346 - logs/* 347 - public/* 348 - templates/* 349 ratings: 350 paths: 351 - "**.go" 352 353 ` 354 355 const nTravis = `language: go 356 env: 357 - GO_ENV=test 358 359 before_script: 360 - psql -c 'create database {{name}}_test;' -U postgres 361 - mysql -e 'CREATE DATABASE {{name}}_test;' 362 - mkdir -p $TRAVIS_BUILD_DIR/public/assets 363 364 go: 365 - 1.7.x 366 - master 367 368 go_import_path: {{ packagePath }} 369 `