gokart 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/assets/config/config-debug.json +22 -0
  2. data/assets/config/config-release.json +22 -0
  3. data/assets/config.rb +1 -1
  4. data/assets/src/server/config/config.go +46 -0
  5. data/assets/src/server/controller/home.go +43 -0
  6. data/assets/src/server/controller/static_assets.go +57 -0
  7. data/assets/src/server/main.go +59 -17
  8. data/assets/src/server/view/home.go +28 -0
  9. data/assets/src/www/app/erb_helpers/erb_helper.rb +4 -2
  10. data/assets/src/www/app/scripts/application.js.coffee +9 -4
  11. data/assets/src/www/app/scripts/pages/home.js +6 -0
  12. data/assets/src/www/app/scripts/vendor_wrapper.js +11 -0
  13. data/assets/src/www/app/templates/application.gotmpl +5 -2
  14. data/assets/src/www/app/templates/base.gotmpl.erb +1 -1
  15. data/assets/src/www/app/templates/{home.gotmpl → pages/home.gotmpl} +12 -7
  16. data/assets/src/www/app/templates/partials/test.gotmpl +3 -0
  17. data/assets/src/www/app/templates/partials/test2.gotmpl +5 -0
  18. data/assets/src/www/app/templates/partials_end.gotmpl +1 -0
  19. data/assets/src/www/app/templates/partials_start.gotmpl +1 -0
  20. data/assets/src/www/spec/all-spec.js +1 -1
  21. data/assets/src/www/spec/helpers/mocks.coffee +1 -1
  22. data/assets/src/www/vendor/js/backbone.js +1478 -0
  23. data/assets/src/www/vendor/js/mustache.js +625 -0
  24. data/assets/src/www/vendor/js/underscore-min.js +5 -0
  25. data/assets/src/www/vendor/js/underscore.js +1204 -0
  26. data/assets/tasks/server.rake +9 -9
  27. data/assets/tasks/www.rake +2 -18
  28. data/bin/gokart +7 -0
  29. data/lib/gokart/base.rb +1 -1
  30. data/lib/gokart/environment.rb +27 -1
  31. data/lib/gokart/version.rb +1 -1
  32. metadata +49 -38
  33. data/assets/src/server/http_handler.go +0 -139
  34. data/assets/src/server/my_test.go +0 -13
  35. data/assets/src/server/templates.go +0 -42
  36. data/assets/src/www/app/partials/index.html +0 -92
  37. data/assets/src/www/app/scripts/main.js.coffee +0 -9
@@ -0,0 +1,22 @@
1
+ {
2
+ "templates": "./bin/assets/templates/*.gotmpl",
3
+ "assets": "./bin/assets/www-debug",
4
+
5
+ "urlAssets": "/assets/",
6
+ "urlRoot": "/",
7
+
8
+ "static": true,
9
+ "debug": true,
10
+
11
+ "http": {
12
+ "port": 8080,
13
+ "address": ""
14
+ },
15
+
16
+ "https": {
17
+ "port": 8081,
18
+ "address": "",
19
+ "key": "",
20
+ "cert": ""
21
+ }
22
+ }
@@ -0,0 +1,22 @@
1
+ {
2
+ "templates": "./bin/assets/templates/*.gotmpl",
3
+ "assets": "./bin/assets/www-min",
4
+
5
+ "urlAssets": "/assets/",
6
+ "urlRoot": "/",
7
+
8
+ "static": true,
9
+ "debug": false,
10
+
11
+ "http": {
12
+ "port": 8080,
13
+ "address": ""
14
+ },
15
+
16
+ "https": {
17
+ "port": 8081,
18
+ "address": "",
19
+ "key": "",
20
+ "cert": ""
21
+ }
22
+ }
data/assets/config.rb CHANGED
@@ -6,7 +6,7 @@ Bundler.require
6
6
  unless defined? ROOT
7
7
  ROOT = Pathname(File.dirname(__FILE__))
8
8
 
9
- ASSET_BUNDLES = %w( application.css application.js )
9
+ ASSET_BUNDLES = %w( application.css application.js )
10
10
  GO_APP_NAME = 'server'
11
11
  GO_BUILD_PATH = ROOT.join('bin')
12
12
 
@@ -0,0 +1,46 @@
1
+ package config
2
+
3
+ import (
4
+ "fmt"
5
+ "github.com/jasondelponte/golib/config"
6
+ "net"
7
+ "path"
8
+ )
9
+
10
+ type Config struct {
11
+ Http struct {
12
+ Address string `json:"address"`
13
+ Port int `json:"port"`
14
+ } `json:"http"`
15
+
16
+ Templates string `json:"templates"`
17
+ Assets string `json:"assets"`
18
+
19
+ URLRoot string `json:"urlRoot"`
20
+ URLRelAssets string `json:"urlAssets"`
21
+
22
+ Debug bool `json:"debug"`
23
+
24
+ URLFullAssetsPath string
25
+ }
26
+
27
+ func LoadConfig(fileName string) (*Config, error) {
28
+ loader := config.NewJsonLoader(fileName)
29
+ c := new(Config)
30
+
31
+ if err := loader.Load(c); err != nil {
32
+ return nil, err
33
+ }
34
+
35
+ c.URLFullAssetsPath = c.buildURLFullAssetsPath()
36
+
37
+ return c, nil
38
+ }
39
+
40
+ func (c Config) GetAddressWithPort() string {
41
+ return net.JoinHostPort(c.Http.Address, fmt.Sprintf("%d", c.Http.Port))
42
+ }
43
+
44
+ func (c Config) buildURLFullAssetsPath() string {
45
+ return path.Join(c.URLRoot, c.URLRelAssets) + "/"
46
+ }
@@ -0,0 +1,43 @@
1
+ package controller
2
+
3
+ import (
4
+ "github.com/jasondelponte/golib/net/conn/httpserver"
5
+ "log"
6
+ "net/http"
7
+ "server/config"
8
+ "server/view"
9
+ )
10
+
11
+ type HomeController struct {
12
+ view *view.HomeView
13
+ config *config.Config
14
+ HandlerFuncs struct {
15
+ Welcome httpserver.HandlerFunc
16
+ }
17
+ }
18
+
19
+ func NewHomeController(view *view.HomeView, cfg *config.Config) *HomeController {
20
+ c := &HomeController{
21
+ view: view,
22
+ config: cfg,
23
+ }
24
+
25
+ c.HandlerFuncs.Welcome = c.buildWelcomeHandler()
26
+
27
+ return c
28
+ }
29
+
30
+ func (c HomeController) buildWelcomeHandler() httpserver.HandlerFunc {
31
+ return func(w http.ResponseWriter, r *http.Request) {
32
+ if c.config.Debug {
33
+ log.Println("HomeController: DEBUG, Request received. Path:", r.URL.Path)
34
+ }
35
+ b, err := c.view.RenderWelcome()
36
+ if err != nil {
37
+ // TODO error response
38
+ return
39
+ }
40
+
41
+ w.Write(b)
42
+ }
43
+ }
@@ -0,0 +1,57 @@
1
+ package controller
2
+
3
+ import (
4
+ "github.com/jasondelponte/golib/net/conn/httpserver"
5
+ "log"
6
+ "net/http"
7
+ "os"
8
+ "path"
9
+ "server/config"
10
+ )
11
+
12
+ type StaticAssetController struct {
13
+ HandlerFuncs struct {
14
+ StaticAssets httpserver.HandlerFunc
15
+ }
16
+
17
+ config *config.Config
18
+ urlAssetPathLen int
19
+ }
20
+
21
+ func NewStaticAssetController(cfg *config.Config) *StaticAssetController {
22
+ c := &StaticAssetController{
23
+ config: cfg,
24
+ urlAssetPathLen: len(cfg.URLFullAssetsPath),
25
+ }
26
+
27
+ c.HandlerFuncs.StaticAssets = c.buildStaticHandler()
28
+
29
+ return c
30
+ }
31
+
32
+ func (c StaticAssetController) buildStaticHandler() httpserver.HandlerFunc {
33
+ return func(w http.ResponseWriter, r *http.Request) {
34
+ if c.config.Debug {
35
+ log.Println("StaticAssetController: DEBUG, Request received. Path:", r.URL.Path)
36
+ }
37
+
38
+ asset := r.URL.Path[c.urlAssetPathLen:]
39
+ fullAssetPath := path.Join(c.config.Assets, asset)
40
+
41
+ c.serveStaticAsset(fullAssetPath, w, r)
42
+ }
43
+ }
44
+
45
+ func (c StaticAssetController) serveStaticAsset(asset string, w http.ResponseWriter, r *http.Request) {
46
+ file, err := os.Open(asset)
47
+ if err != nil {
48
+ httpserver.ErrHttpResourceNotFound.Report(w)
49
+ return
50
+ }
51
+ stat, err := file.Stat()
52
+ if err != nil {
53
+ httpserver.ErrHttpInternalError.Report(w)
54
+ return
55
+ }
56
+ http.ServeContent(w, r, asset, stat.ModTime(), file)
57
+ }
@@ -2,30 +2,72 @@ package main
2
2
 
3
3
  import (
4
4
  "flag"
5
+ "github.com/jasondelponte/golib/net/conn/httpserver"
6
+ "github.com/jasondelponte/golib/text/html/template"
5
7
  "log"
8
+ "server/config"
9
+ "server/controller"
10
+ "server/view"
6
11
  )
7
12
 
8
- var addr = flag.String("addr", "", "IP address the server is to run on")
9
- var port = flag.Uint("port", 0, "Port address to run the server on")
10
- var rootURLPath = flag.String("root", "", "URL Path root of the webapp")
11
- var tmplPath = flag.String("tmpl", "assets/templates", "Specifies the directory to use for template assets.")
12
- var wwwPath = flag.String("www", "assets/www-debug", "Specifies the directory to use for web assest.")
13
- var debug = flag.Bool("debug", false, "Enable debug mode where extra loging is produce, each request will reload templates, and all assets are served from non-concaticated files.")
14
- var static = flag.Bool("static", false, "Enables serving up static content from disk")
13
+ var cfgFile = flag.String("config", "", "Configuration JSON file")
15
14
 
16
15
  func main() {
17
16
  flag.Parse()
18
17
 
19
- httpHndlr := &HttpHandler{
20
- Addr: *addr,
21
- Port: *port,
22
- RootURLPath: *rootURLPath,
23
- TmplPath: *tmplPath,
24
- WwwPath: *wwwPath,
25
- Debug: *debug,
26
- ServStatic: *static,
18
+ var (
19
+ cfg *config.Config
20
+ server *httpserver.HTTPServer
21
+ tmpl *template.Template
22
+ err error
23
+ )
24
+
25
+ if cfg, err = config.LoadConfig(*cfgFile); err != nil {
26
+ log.Fatalln("Failed to load config, ", *cfgFile, ", because", err)
27
+ return
28
+ }
29
+
30
+ if err = initDatabase(cfg); err != nil {
31
+ log.Fatalln("Failed to initialize database, because", err)
32
+ return
33
+ }
34
+
35
+ if tmpl, err = loadTemplates(cfg); err != nil {
36
+ log.Fatalln("Failed to load HTML templates, because", err)
37
+ return
38
+ }
39
+
40
+ if server, err = initServer(cfg, tmpl); err != nil {
41
+ log.Fatalln("Failed to initialize HTTP(s) server configuration, because", err)
42
+ return
43
+ }
44
+
45
+ server.Run()
46
+ }
47
+
48
+ func initDatabase(cfg *config.Config) error {
49
+ return nil
50
+ }
51
+
52
+ func loadTemplates(cfg *config.Config) (*template.Template, error) {
53
+ tmpl := new(template.Template)
54
+
55
+ if err := tmpl.Load(cfg.Templates); err != nil {
56
+ return tmpl, err
27
57
  }
28
58
 
29
- httpHndlr.HandleHttpConnection()
30
- log.Println("")
59
+ return tmpl, nil
60
+ }
61
+
62
+ func initServer(cfg *config.Config, tmpl *template.Template) (*httpserver.HTTPServer, error) {
63
+
64
+ server := httpserver.NewHTTPServer(cfg.GetAddressWithPort(), cfg.Debug)
65
+
66
+ home := controller.NewHomeController(view.NewHomeView(tmpl, cfg), cfg)
67
+ server.AddHandlerFunc(cfg.URLRoot, home.HandlerFuncs.Welcome)
68
+
69
+ static := controller.NewStaticAssetController(cfg)
70
+ server.AddHandlerFunc(cfg.URLFullAssetsPath, static.HandlerFuncs.StaticAssets)
71
+
72
+ return server, nil
31
73
  }
@@ -0,0 +1,28 @@
1
+ package view
2
+
3
+ import (
4
+ "github.com/jasondelponte/golib/text/html/template"
5
+ "server/config"
6
+ )
7
+
8
+ type HomeView struct {
9
+ template *template.Template
10
+ config *config.Config
11
+ }
12
+
13
+ func NewHomeView(tmpl *template.Template, cfg *config.Config) *HomeView {
14
+ return &HomeView{
15
+ template: tmpl,
16
+ config: cfg,
17
+ }
18
+ }
19
+
20
+ func (v *HomeView) RenderWelcome() ([]byte, error) {
21
+ common := template.CommonProps{
22
+ Title: "GoKart",
23
+ Debug: v.config.Debug,
24
+ RootURL: v.config.URLRoot,
25
+ }
26
+
27
+ return v.template.Render("home", common, nil)
28
+ }
@@ -1,10 +1,12 @@
1
1
 
2
2
  module ErbHelper
3
3
  def ErbHelper.asset_link(asset, prefix)
4
+ src = "{{PathJoin .RootURL \"/assets/\" \"#{asset}\"}}"
4
5
  if (prefix == 'js')
5
- return "<script type=\"text/javascript\" src=\"{{.RootURL}}/assets/#{asset}\"></script>"
6
+ return "<script type=\"text/javascript\" src=\"#{src}\"></script>"
7
+
6
8
  elsif (prefix == 'css')
7
- return "<link rel=\"stylesheet\" type=\"text/css\" src=\"{{.RootURL}}/assets/#{asset}\">"
9
+ return "<link rel=\"stylesheet\" type=\"text/css\" src=\"#{src}\">"
8
10
  end
9
11
  end
10
12
 
@@ -1,5 +1,10 @@
1
1
  #= require utilities/require
2
- #= require utilities/deferred
3
- #= require utilities/logger
4
- #= require utilities/properties
5
- #= require main
2
+ #= require vendor_wrapper
3
+ #= require underscore
4
+ #= require backbone
5
+ #= require mustache
6
+ #= require_tree ./controllers
7
+ #= require_tree ./models
8
+ #= require_tree ./pages
9
+ #= require_tree ./utilities
10
+ #= require_tree ./views
@@ -0,0 +1,6 @@
1
+ define('pages/home', [
2
+ 'mustache',
3
+ 'utilities/logger'
4
+ ], function HomePageModule(Mustache, Logger) {
5
+
6
+ });
@@ -0,0 +1,11 @@
1
+ define('underscore', [], function UnderscoreModule() {
2
+ return _;
3
+ });
4
+
5
+ define('jquery', [], function jQueryModule() {
6
+ return jQuery;
7
+ });
8
+
9
+ define('backbone', [], function BackboneModule() {
10
+ return Backbone;
11
+ });
@@ -1,2 +1,5 @@
1
- //= require base
2
- //= require home
1
+ #= require base
2
+ #= require partials_start
3
+ #= require_tree ./partials
4
+ #= require partials_end
5
+ #= require_tree ./pages
@@ -6,6 +6,7 @@
6
6
  {{if .Debug}}{{template "base.header.assets.debug" .}}{{else}}{{template "base.header.assets" .}}{{end}}
7
7
  </head>
8
8
  <body>
9
+ {{template "html_templates"}}
9
10
  {{end}}
10
11
 
11
12
  {{define "base.header.assets"}}
@@ -22,7 +23,6 @@
22
23
  <% end %>
23
24
  {{end}}
24
25
 
25
-
26
26
  {{define "base.footer"}}
27
27
  </body>
28
28
  </html>
@@ -1,16 +1,17 @@
1
1
  {{define "home.contents"}}
2
2
  <div class="info-block">
3
3
  <h3>Go Kart</h3>
4
- <p>Combination of tooks which will make developing webapps using go easier. The gokart gem by default combines SASS, Coffee Script, Rake, and Sprokets with Go to provide a great development environment. This environment supports test driven development (TDD) with ruby guard and jasmine.</p>
5
- <p>There are no external dependancies other than Go and ruby (which I expect you already have...). All dependancies are installed via bundler after the project directory is created.</p>
4
+ <p>Combination of tools which will make developing webapps using go easier. The gokart gem by default combines SASS, Javascript, Coffee Script, Rake, and Sprokets with Go to provide a great development environment. This environment supports test driven development (TDD) with ruby guard and jasmine.</p>
5
+ <p>There are no external dependencies other than Go and ruby (which I expect you already have...). All dependencies are installed via bundler after the project directory is created.</p>
6
6
  </div>
7
7
  <div class="info-block">
8
8
  <h3>Usage</h3>
9
9
  <p>With ruby (1.9.1 min maybe?) all you need to do is:</p>
10
10
  <p class="code">gem install gokart</p>
11
- <p>Once installed to create a new go + sprockets enviornment:</p>
11
+ <p>Once installed to create a new go + sprockets environment:</p>
12
12
  <p class="code">gokart APP_NAME</p>
13
- <p>This will create a folder of APP_NAME, build the environment, and run bundle install to pull in the gem files that are needed for the environment if you don't already have them. There are also several rake commands you can use to build/run/deploy the environment. Checkout rake --tasks for a list of them. In general you'll probably only ever need the app:x commands not app:server:x or app:www:x.</p>
13
+ <p>This utility/framework depends on the github.com/jasondelponte/go-utils to run out of the box, but if you choose it would be simple to remove and place with utilities of your choice.<p>
14
+ <p>There are several rake commands you can use to build/run/deploy the environment. Checkout rake --tasks for a list of them. In general you'll probably only ever need the app:x commands not app:server:x or app:www:x.</p>
14
15
  </div>
15
16
  <div class="info-block">
16
17
  <h4>Environment Structure</h4>
@@ -20,18 +21,22 @@
20
21
  <li><strong>bin/server/</strong> - Compiled Go http server.</li>
21
22
  <li><strong>bin/assets/templates/</strong> - Compiled Go HTML templates</li>
22
23
  <li><strong>bin/assets/www-min/</strong> - Compiled and minified CSS/JS/images using sprockets</li>
23
- <li><strong>bin/assets/www-debug/</strong> - Compiled, but non-minified/concatonated CSS/JS source, images are here also</li>
24
+ <li><strong>bin/assets/www-debug/</strong> - Compiled, but non-minified/concatenated CSS/JS source, images are here also</li>
24
25
  <li><strong>lib/</strong> - Where any Go libraries your app depends on will be installed by running 'go get'. Note: You'll need to set the project into your GOPATH, I haven't figured out a good solution for this yet.</li>
25
26
  <li><strong>spec/javascripts/</strong> - Where all of the compiled JS specs will be written to, and run from. Note: Currently these do not go through sprockets, but only the coffee-script compiler. So no preprocessing is available</li>
26
- <li><strong>spec/javascripts/support</strong> - Configuration files for Jasmine. jasmin.yml contains the script specifiing the source files to load, and what order to load them in.</li>
27
+ <li><strong>spec/javascripts/support</strong> - Configuration files for Jasmine. jasmin.yml contains the script specifying the source files to load, and what order to load them in.</li>
27
28
  <li><strong>src/server/</strong> - All of your Go code will go here following the normal Go 1.0 directory pattern</li>
28
29
  <li><strong>src/www/app/</strong> - All of you Coffee script, sass, and Go Template are found here</li>
29
30
  <li><strong>src/www/spec/</strong> - All coffee script spec files which will be compiled into js and run using jasmine.</li>
30
31
  <li><strong>src/www/vendor/</strong> - js/css/image files for third party tools and libraries</li>
31
- <li><strong>tasks/</strong> - Rake tasks to build, run, and test the application. There are sevearl tasks which you can run do 'rake --tasks' to see a complete list of them. 'rake app:test' to run test units, 'rake app:start' to build and run, use startdebug for a debug build. Note: when you do 'rake jasmine' or rake 'app:guard' you probably want to start these tasks in different tabs because they run until killed.</li>
32
+ <li><strong>tasks/</strong> - Rake tasks to build, run, and test the application. There are several tasks which you can run do 'rake --tasks' to see a complete list of them. 'rake app:test' to run test units, 'rake app:start' to build and run, use startdebug for a debug build. Note: when you do 'rake jasmine' or rake 'app:guard' you probably want to start these tasks in different tabs because they run until killed.</li>
32
33
  </ul>
33
34
  </div>
34
35
 
36
+ <script type="text/javascript">
37
+ var HomePage = require('pages/home');
38
+ </script>
39
+
35
40
  {{end}}
36
41
 
37
42
  {{define "home"}}
@@ -0,0 +1,3 @@
1
+ <script id="TestTemplate" type="text/html">
2
+
3
+ </script>
@@ -0,0 +1,5 @@
1
+ <script id="test2Template" type="text/html">
2
+ <div>
3
+
4
+ </div>
5
+ </script>
@@ -0,0 +1 @@
1
+ {{end}}
@@ -0,0 +1 @@
1
+ {{define "html_templates"}}
@@ -1,5 +1,5 @@
1
1
  define('spec/all', [
2
- 'spec/mocks',
2
+ 'spec/helpers/mocks',
3
3
  'spec/utilities/test',
4
4
  'spec/utilities/deferred',
5
5
  'spec/utilities/properties'
@@ -1,4 +1,4 @@
1
- define 'spec/mocks', [], ->
1
+ define 'spec/helpers/mocks', [], ->
2
2
 
3
3
  Mocks = {}
4
4