gokart 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/assets/config/config-debug.json +22 -0
- data/assets/config/config-release.json +22 -0
- data/assets/config.rb +1 -1
- data/assets/src/server/config/config.go +46 -0
- data/assets/src/server/controller/home.go +43 -0
- data/assets/src/server/controller/static_assets.go +57 -0
- data/assets/src/server/main.go +59 -17
- data/assets/src/server/view/home.go +28 -0
- data/assets/src/www/app/erb_helpers/erb_helper.rb +4 -2
- data/assets/src/www/app/scripts/application.js.coffee +9 -4
- data/assets/src/www/app/scripts/pages/home.js +6 -0
- data/assets/src/www/app/scripts/vendor_wrapper.js +11 -0
- data/assets/src/www/app/templates/application.gotmpl +5 -2
- data/assets/src/www/app/templates/base.gotmpl.erb +1 -1
- data/assets/src/www/app/templates/{home.gotmpl → pages/home.gotmpl} +12 -7
- data/assets/src/www/app/templates/partials/test.gotmpl +3 -0
- data/assets/src/www/app/templates/partials/test2.gotmpl +5 -0
- data/assets/src/www/app/templates/partials_end.gotmpl +1 -0
- data/assets/src/www/app/templates/partials_start.gotmpl +1 -0
- data/assets/src/www/spec/all-spec.js +1 -1
- data/assets/src/www/spec/helpers/mocks.coffee +1 -1
- data/assets/src/www/vendor/js/backbone.js +1478 -0
- data/assets/src/www/vendor/js/mustache.js +625 -0
- data/assets/src/www/vendor/js/underscore-min.js +5 -0
- data/assets/src/www/vendor/js/underscore.js +1204 -0
- data/assets/tasks/server.rake +9 -9
- data/assets/tasks/www.rake +2 -18
- data/bin/gokart +7 -0
- data/lib/gokart/base.rb +1 -1
- data/lib/gokart/environment.rb +27 -1
- data/lib/gokart/version.rb +1 -1
- metadata +49 -38
- data/assets/src/server/http_handler.go +0 -139
- data/assets/src/server/my_test.go +0 -13
- data/assets/src/server/templates.go +0 -42
- data/assets/src/www/app/partials/index.html +0 -92
- data/assets/src/www/app/scripts/main.js.coffee +0 -9
data/assets/tasks/server.rake
CHANGED
@@ -17,7 +17,7 @@ namespace :app do
|
|
17
17
|
|
18
18
|
desc 'Builds the existing source'
|
19
19
|
task :build => [:init] do
|
20
|
-
`GOPATH="#{ROOT}" go install #{GO_APP_NAME} 1>&2`
|
20
|
+
`GOPATH="#{ROOT}:$GOPATH" go install #{GO_APP_NAME} 1>&2`
|
21
21
|
end
|
22
22
|
|
23
23
|
desc 'Cleans the build path'
|
@@ -36,19 +36,19 @@ namespace :app do
|
|
36
36
|
|
37
37
|
desc 'Runs the test units for the source files'
|
38
38
|
task :test => [:build] do
|
39
|
-
`GOPATH="#{ROOT}" go test #{GO_APP_NAME} 1>&2`
|
39
|
+
`GOPATH="#{ROOT}:$GOPATH" go test #{GO_APP_NAME} 1>&2`
|
40
40
|
end
|
41
41
|
|
42
|
-
desc 'Runs the server
|
43
|
-
task :start, [:
|
44
|
-
args.with_defaults(:
|
45
|
-
`./bin/#{GO_APP_NAME} -
|
42
|
+
desc 'Runs the server'
|
43
|
+
task :start, [:config] => [:stop] do |t, args|
|
44
|
+
args.with_defaults(:config => "./config/config-release.json")
|
45
|
+
`./bin/#{GO_APP_NAME} -config="#{args[:config]}"`
|
46
46
|
end
|
47
47
|
|
48
|
-
desc 'Runs the server
|
48
|
+
desc 'Runs the server in debug mode'
|
49
49
|
task :startdebug, [:port] => [:stop] do |t, args|
|
50
|
-
args.with_defaults(:
|
51
|
-
`./bin/#{GO_APP_NAME} -
|
50
|
+
args.with_defaults(:config => "./config/config-debug.json")
|
51
|
+
`./bin/#{GO_APP_NAME} -config="#{args[:config]}"`
|
52
52
|
end
|
53
53
|
|
54
54
|
desc 'Stops the server if it was running'
|
data/assets/tasks/www.rake
CHANGED
@@ -31,11 +31,9 @@ namespace :app do
|
|
31
31
|
paths << MIN_WWW_PATH.join('js')
|
32
32
|
paths << MIN_WWW_PATH.join('css')
|
33
33
|
paths << MIN_WWW_PATH.join('images')
|
34
|
-
paths << MIN_WWW_PATH.join('partials')
|
35
34
|
paths << DEBUG_WWW_PATH.join('js')
|
36
35
|
paths << DEBUG_WWW_PATH.join('css')
|
37
36
|
paths << DEBUG_WWW_PATH.join('images')
|
38
|
-
paths << DEBUG_WWW_PATH.join('partials')
|
39
37
|
paths << TMPL_BUILD_PATH
|
40
38
|
|
41
39
|
paths.each do |path|
|
@@ -53,12 +51,8 @@ namespace :app do
|
|
53
51
|
|
54
52
|
Dir.glob(in_dir.join('**', '*')).each do |file|
|
55
53
|
out_file = File.join(out_dir, file.partition(in_dir.to_s)[2])
|
56
|
-
next if (out_file.empty?)
|
57
|
-
|
58
|
-
if FileTest::directory?(file)
|
59
|
-
next
|
60
|
-
end
|
61
54
|
|
55
|
+
next if out_file.empty? || FileTest::directory?(file)
|
62
56
|
FileUtils::mkdir_p(File.dirname(out_file)) if (!FileTest::directory?(File.dirname(out_file)))
|
63
57
|
|
64
58
|
begin
|
@@ -88,16 +82,6 @@ namespace :app do
|
|
88
82
|
end
|
89
83
|
end
|
90
84
|
|
91
|
-
task :partials do
|
92
|
-
from = WWW_SRC_APP_PATH.join('partials')
|
93
|
-
to = DEBUG_WWW_PATH
|
94
|
-
begin
|
95
|
-
FileUtils::cp_r from, to
|
96
|
-
rescue
|
97
|
-
puts "Failed to copy directory #{from} to #{to}"
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
85
|
task :vendor do
|
102
86
|
from = "#{WWW_SRC_VENDOR_PATH}/images"
|
103
87
|
to = DEBUG_WWW_PATH.join('vendor', 'images')
|
@@ -152,7 +136,7 @@ namespace :app do
|
|
152
136
|
end
|
153
137
|
|
154
138
|
desc 'Builds the existing source'
|
155
|
-
task :build => [:init,:compile,:compile_templates,:spec,:images
|
139
|
+
task :build => [:init,:compile,:compile_templates,:spec,:images]
|
156
140
|
|
157
141
|
desc 'Cleans the build path'
|
158
142
|
task :clean do
|
data/bin/gokart
CHANGED
@@ -26,3 +26,10 @@ Open3.popen3('bundle install') do | i, o, e |
|
|
26
26
|
$stderr.puts err unless err.nil?
|
27
27
|
end
|
28
28
|
end
|
29
|
+
|
30
|
+
Open3.popen3('GOPATH=./:$GOPATH go get github.com/jasondelponte/golib') do | i, o, e |
|
31
|
+
while (out = o.gets || err = e.gets)
|
32
|
+
$stdout.puts out unless out.nil?
|
33
|
+
$stderr.puts err unless err.nil?
|
34
|
+
end
|
35
|
+
end
|
data/lib/gokart/base.rb
CHANGED
@@ -42,7 +42,7 @@ module Gokart
|
|
42
42
|
end
|
43
43
|
|
44
44
|
def copy_files
|
45
|
-
Dir.glob(@assets_path.join("**","*")).each() do | inFile |
|
45
|
+
Dir.glob(@assets_path.join("**","*"), File::FNM_DOTMATCH).each() do | inFile |
|
46
46
|
begin
|
47
47
|
outFile = File.join(@app_base_path.to_s(), inFile.partition(@assets_path.to_s)[2])
|
48
48
|
|
data/lib/gokart/environment.rb
CHANGED
@@ -16,7 +16,33 @@ module Gokart
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def directories
|
19
|
-
paths = [
|
19
|
+
paths = [
|
20
|
+
File.join(@app_name,'bin'),
|
21
|
+
File.join(@app_name,'config'),
|
22
|
+
File.join(@app_name,'lib'),
|
23
|
+
File.join(@app_name,'pkg'),
|
24
|
+
File.join(@app_name,'spec'),
|
25
|
+
File.join(@app_name,'src','server','config'),
|
26
|
+
File.join(@app_name,'src','server','controller'),
|
27
|
+
File.join(@app_name,'src','server','view'),
|
28
|
+
File.join(@app_name,'src','www','app','erb_helpers'),
|
29
|
+
File.join(@app_name,'src','www','app','images'),
|
30
|
+
File.join(@app_name,'src','www','app','sass'),
|
31
|
+
File.join(@app_name,'src','www','app','scripts','controllers'),
|
32
|
+
File.join(@app_name,'src','www','app','scripts','models'),
|
33
|
+
File.join(@app_name,'src','www','app','scripts','pages'),
|
34
|
+
File.join(@app_name,'src','www','app','scripts','utilities'),
|
35
|
+
File.join(@app_name,'src','www','app','scripts','views'),
|
36
|
+
File.join(@app_name,'src','www','app','templates','pages'),
|
37
|
+
File.join(@app_name,'src','www','app','templates','partials'),
|
38
|
+
File.join(@app_name,'src','www','spec','helpers'),
|
39
|
+
File.join(@app_name,'src','www','spec','support'),
|
40
|
+
File.join(@app_name,'src','www','spec','utilities'),
|
41
|
+
File.join(@app_name,'src','www','vendor','css'),
|
42
|
+
File.join(@app_name,'src','www','vendor','images'),
|
43
|
+
File.join(@app_name,'src','www','vendor','js'),
|
44
|
+
File.join(@app_name,'tasks'),
|
45
|
+
]
|
20
46
|
end
|
21
47
|
end
|
22
48
|
end
|
data/lib/gokart/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gokart
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-12-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sass
|
16
|
-
requirement: &
|
16
|
+
requirement: &16254360 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *16254360
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: coffee-script
|
27
|
-
requirement: &
|
27
|
+
requirement: &16253320 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *16253320
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: therubyracer
|
38
|
-
requirement: &
|
38
|
+
requirement: &16251740 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *16251740
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: sprockets
|
49
|
-
requirement: &
|
49
|
+
requirement: &16250780 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *16250780
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: guard
|
60
|
-
requirement: &
|
60
|
+
requirement: &16249200 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *16249200
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: guard-coffeescript
|
71
|
-
requirement: &
|
71
|
+
requirement: &16262620 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *16262620
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: guard-jasmine
|
82
|
-
requirement: &
|
82
|
+
requirement: &16261560 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ! '>='
|
@@ -87,10 +87,10 @@ dependencies:
|
|
87
87
|
version: '0'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *16261560
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: guard-sass
|
93
|
-
requirement: &
|
93
|
+
requirement: &16260020 !ruby/object:Gem::Requirement
|
94
94
|
none: false
|
95
95
|
requirements:
|
96
96
|
- - ! '>='
|
@@ -98,10 +98,10 @@ dependencies:
|
|
98
98
|
version: '0'
|
99
99
|
type: :development
|
100
100
|
prerelease: false
|
101
|
-
version_requirements: *
|
101
|
+
version_requirements: *16260020
|
102
102
|
- !ruby/object:Gem::Dependency
|
103
103
|
name: guard-rake
|
104
|
-
requirement: &
|
104
|
+
requirement: &16258640 !ruby/object:Gem::Requirement
|
105
105
|
none: false
|
106
106
|
requirements:
|
107
107
|
- - ! '>='
|
@@ -109,10 +109,10 @@ dependencies:
|
|
109
109
|
version: '0'
|
110
110
|
type: :development
|
111
111
|
prerelease: false
|
112
|
-
version_requirements: *
|
112
|
+
version_requirements: *16258640
|
113
113
|
- !ruby/object:Gem::Dependency
|
114
114
|
name: guard-livereload
|
115
|
-
requirement: &
|
115
|
+
requirement: &16257540 !ruby/object:Gem::Requirement
|
116
116
|
none: false
|
117
117
|
requirements:
|
118
118
|
- - ! '>='
|
@@ -120,10 +120,10 @@ dependencies:
|
|
120
120
|
version: '0'
|
121
121
|
type: :development
|
122
122
|
prerelease: false
|
123
|
-
version_requirements: *
|
123
|
+
version_requirements: *16257540
|
124
124
|
- !ruby/object:Gem::Dependency
|
125
125
|
name: guard-shell
|
126
|
-
requirement: &
|
126
|
+
requirement: &16255780 !ruby/object:Gem::Requirement
|
127
127
|
none: false
|
128
128
|
requirements:
|
129
129
|
- - ! '>='
|
@@ -131,10 +131,10 @@ dependencies:
|
|
131
131
|
version: '0'
|
132
132
|
type: :development
|
133
133
|
prerelease: false
|
134
|
-
version_requirements: *
|
134
|
+
version_requirements: *16255780
|
135
135
|
- !ruby/object:Gem::Dependency
|
136
136
|
name: jasmine
|
137
|
-
requirement: &
|
137
|
+
requirement: &16284840 !ruby/object:Gem::Requirement
|
138
138
|
none: false
|
139
139
|
requirements:
|
140
140
|
- - ! '>='
|
@@ -142,10 +142,10 @@ dependencies:
|
|
142
142
|
version: '0'
|
143
143
|
type: :development
|
144
144
|
prerelease: false
|
145
|
-
version_requirements: *
|
145
|
+
version_requirements: *16284840
|
146
146
|
- !ruby/object:Gem::Dependency
|
147
147
|
name: yui-compressor
|
148
|
-
requirement: &
|
148
|
+
requirement: &16281600 !ruby/object:Gem::Requirement
|
149
149
|
none: false
|
150
150
|
requirements:
|
151
151
|
- - ! '>='
|
@@ -153,10 +153,10 @@ dependencies:
|
|
153
153
|
version: '0'
|
154
154
|
type: :development
|
155
155
|
prerelease: false
|
156
|
-
version_requirements: *
|
156
|
+
version_requirements: *16281600
|
157
157
|
- !ruby/object:Gem::Dependency
|
158
158
|
name: uglifier
|
159
|
-
requirement: &
|
159
|
+
requirement: &16280260 !ruby/object:Gem::Requirement
|
160
160
|
none: false
|
161
161
|
requirements:
|
162
162
|
- - ! '>='
|
@@ -164,10 +164,10 @@ dependencies:
|
|
164
164
|
version: '0'
|
165
165
|
type: :development
|
166
166
|
prerelease: false
|
167
|
-
version_requirements: *
|
167
|
+
version_requirements: *16280260
|
168
168
|
- !ruby/object:Gem::Dependency
|
169
169
|
name: bundler
|
170
|
-
requirement: &
|
170
|
+
requirement: &16278840 !ruby/object:Gem::Requirement
|
171
171
|
none: false
|
172
172
|
requirements:
|
173
173
|
- - ! '>='
|
@@ -175,7 +175,7 @@ dependencies:
|
|
175
175
|
version: 1.0.0
|
176
176
|
type: :runtime
|
177
177
|
prerelease: false
|
178
|
-
version_requirements: *
|
178
|
+
version_requirements: *16278840
|
179
179
|
description: Combination of tooks which will make developing webapps using go easier. The
|
180
180
|
gokart gem by default combines SASS, Coffee Script, Rake, and Sprokets with Go to
|
181
181
|
provide a great development environment. This environment supports test driven
|
@@ -194,12 +194,19 @@ files:
|
|
194
194
|
- lib/gokart/environment.rb
|
195
195
|
- lib/gokart/version.rb
|
196
196
|
- assets/Guardfile
|
197
|
+
- assets/config/config-debug.json
|
198
|
+
- assets/config/config-release.json
|
197
199
|
- assets/config.rb
|
198
|
-
- assets/src/server/
|
199
|
-
- assets/src/server/
|
200
|
-
- assets/src/server/
|
200
|
+
- assets/src/server/controller/static_assets.go
|
201
|
+
- assets/src/server/controller/home.go
|
202
|
+
- assets/src/server/config/config.go
|
201
203
|
- assets/src/server/main.go
|
204
|
+
- assets/src/server/view/home.go
|
202
205
|
- assets/src/www/vendor/js/backbone-min.js
|
206
|
+
- assets/src/www/vendor/js/underscore-min.js
|
207
|
+
- assets/src/www/vendor/js/backbone.js
|
208
|
+
- assets/src/www/vendor/js/mustache.js
|
209
|
+
- assets/src/www/vendor/js/underscore.js
|
203
210
|
- assets/src/www/spec/helpers/mocks.coffee
|
204
211
|
- assets/src/www/spec/all-spec.js
|
205
212
|
- assets/src/www/spec/utilities/test-spec.js
|
@@ -209,16 +216,20 @@ files:
|
|
209
216
|
- assets/src/www/spec/support/jasmine.yml
|
210
217
|
- assets/src/www/spec/support/jasmine_config.rb
|
211
218
|
- assets/src/www/app/erb_helpers/erb_helper.rb
|
212
|
-
- assets/src/www/app/scripts/
|
219
|
+
- assets/src/www/app/scripts/vendor_wrapper.js
|
213
220
|
- assets/src/www/app/scripts/application.js.coffee
|
214
221
|
- assets/src/www/app/scripts/utilities/properties.js.coffee
|
215
222
|
- assets/src/www/app/scripts/utilities/logger.js.coffee
|
216
223
|
- assets/src/www/app/scripts/utilities/deferred.js.coffee
|
217
224
|
- assets/src/www/app/scripts/utilities/require.js
|
218
|
-
- assets/src/www/app/
|
219
|
-
- assets/src/www/app/templates/home.gotmpl
|
225
|
+
- assets/src/www/app/scripts/pages/home.js
|
220
226
|
- assets/src/www/app/templates/base.gotmpl.erb
|
227
|
+
- assets/src/www/app/templates/partials/test.gotmpl
|
228
|
+
- assets/src/www/app/templates/partials/test2.gotmpl
|
229
|
+
- assets/src/www/app/templates/partials_start.gotmpl
|
230
|
+
- assets/src/www/app/templates/partials_end.gotmpl
|
221
231
|
- assets/src/www/app/templates/application.gotmpl
|
232
|
+
- assets/src/www/app/templates/pages/home.gotmpl
|
222
233
|
- assets/src/www/app/sass/application.css.scss
|
223
234
|
- assets/tasks/app.rake
|
224
235
|
- assets/tasks/server.rake
|
@@ -1,139 +0,0 @@
|
|
1
|
-
package main
|
2
|
-
|
3
|
-
import (
|
4
|
-
"fmt"
|
5
|
-
"log"
|
6
|
-
"net/http"
|
7
|
-
"os"
|
8
|
-
"regexp"
|
9
|
-
"strings"
|
10
|
-
)
|
11
|
-
|
12
|
-
// HTTP Error Enumerables
|
13
|
-
type HttpError struct {
|
14
|
-
ErrorString string
|
15
|
-
CodeNum int
|
16
|
-
}
|
17
|
-
|
18
|
-
func (h HttpError) Error() string { return h.ErrorString }
|
19
|
-
func (h HttpError) Code() int { return h.CodeNum }
|
20
|
-
func (h HttpError) Report(w http.ResponseWriter) { http.Error(w, h.ErrorString, h.CodeNum) }
|
21
|
-
|
22
|
-
var (
|
23
|
-
ErrHttpResourceNotFound = &HttpError{ErrorString: "Not found", CodeNum: 404}
|
24
|
-
ErrHttpMethodNotAllowed = &HttpError{ErrorString: "Method not allowed", CodeNum: 405}
|
25
|
-
ErrHttpBadRequeset = &HttpError{ErrorString: "Bad request", CodeNum: 400}
|
26
|
-
ErrHttpInternalError = &HttpError{ErrorString: "Internal failure", CodeNum: 500}
|
27
|
-
)
|
28
|
-
|
29
|
-
type HttpHandler struct {
|
30
|
-
RootURLPath string
|
31
|
-
TmplPath string
|
32
|
-
WwwPath string
|
33
|
-
Addr string
|
34
|
-
Port uint
|
35
|
-
Debug bool
|
36
|
-
ServStatic bool
|
37
|
-
templates *Templates
|
38
|
-
rootURLPathLen int
|
39
|
-
wwwURLPathLen int
|
40
|
-
}
|
41
|
-
|
42
|
-
// Load all the temmplates into memeory
|
43
|
-
func (h *HttpHandler) loadTemplates() {
|
44
|
-
h.templates = &Templates{}
|
45
|
-
h.templates.LoadTemplates(h.TmplPath)
|
46
|
-
}
|
47
|
-
|
48
|
-
// Configures the http connection and starts the listender
|
49
|
-
func (h *HttpHandler) HandleHttpConnection() {
|
50
|
-
h.rootURLPathLen = len(h.RootURLPath + "/")
|
51
|
-
h.wwwURLPathLen = len(h.RootURLPath + "/assets/")
|
52
|
-
|
53
|
-
h.loadTemplates()
|
54
|
-
|
55
|
-
h.initServeHomeHndlr(h.RootURLPath + "/")
|
56
|
-
if h.Debug || h.ServStatic {
|
57
|
-
h.initServeStaticHndlr(h.RootURLPath + "/assets/")
|
58
|
-
}
|
59
|
-
|
60
|
-
// Build the address with port if it's provided
|
61
|
-
address := h.Addr
|
62
|
-
if h.Port != 0 {
|
63
|
-
address = fmt.Sprintf("%s:%d", h.Addr, h.Port)
|
64
|
-
}
|
65
|
-
|
66
|
-
err := http.ListenAndServe(address, nil)
|
67
|
-
if err != nil {
|
68
|
-
log.Fatal("ListenAndServe: ", err)
|
69
|
-
}
|
70
|
-
}
|
71
|
-
|
72
|
-
// Network event handler for HTTP trafic. Serves up the
|
73
|
-
// home.html file which will allow connection to the websocket
|
74
|
-
func (h *HttpHandler) initServeHomeHndlr(path string) {
|
75
|
-
regProps := &CommonProps{
|
76
|
-
Title: "Go + WWW + Rake test app",
|
77
|
-
Debug: h.Debug,
|
78
|
-
RootURL: h.RootURLPath,
|
79
|
-
Host: "",
|
80
|
-
}
|
81
|
-
|
82
|
-
hostPortRep := regexp.MustCompile(":\\d+$")
|
83
|
-
|
84
|
-
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
85
|
-
if r.URL.Path != h.RootURLPath+"/" {
|
86
|
-
ErrHttpResourceNotFound.Report(w)
|
87
|
-
return
|
88
|
-
}
|
89
|
-
if r.Method != "GET" {
|
90
|
-
ErrHttpMethodNotAllowed.Report(w)
|
91
|
-
return
|
92
|
-
}
|
93
|
-
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
94
|
-
|
95
|
-
// Normalalize http host
|
96
|
-
if len(regProps.Host) == 0 {
|
97
|
-
regProps.Host = r.Host
|
98
|
-
if (h.Debug || h.ServStatic) && h.Port != 0 {
|
99
|
-
if strings.Contains(r.Host, ":") {
|
100
|
-
regProps.Host = hostPortRep.ReplaceAllString(r.Host, fmt.Sprintf(":%d", h.Port))
|
101
|
-
} else {
|
102
|
-
regProps.Host = fmt.Sprintf("%s:%d", r.Host, h.Port)
|
103
|
-
}
|
104
|
-
}
|
105
|
-
}
|
106
|
-
|
107
|
-
if h.Debug { // Force reloading of the template for each request in debug mode
|
108
|
-
h.loadTemplates()
|
109
|
-
}
|
110
|
-
|
111
|
-
b, err := h.templates.Render("home", regProps, nil)
|
112
|
-
if err != nil {
|
113
|
-
log.Println("Failed to render template, home")
|
114
|
-
return
|
115
|
-
}
|
116
|
-
|
117
|
-
w.Write(b)
|
118
|
-
})
|
119
|
-
}
|
120
|
-
|
121
|
-
// Simple handler for serving static files
|
122
|
-
func (h *HttpHandler) initServeStaticHndlr(path string) {
|
123
|
-
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
124
|
-
asset := r.URL.Path[h.wwwURLPathLen:]
|
125
|
-
fullAssetPath := h.WwwPath + "/" + asset
|
126
|
-
|
127
|
-
file, err := os.Open(fullAssetPath)
|
128
|
-
if err != nil {
|
129
|
-
ErrHttpResourceNotFound.Report(w)
|
130
|
-
return
|
131
|
-
}
|
132
|
-
stat, err := file.Stat()
|
133
|
-
if err != nil {
|
134
|
-
ErrHttpInternalError.Report(w)
|
135
|
-
return
|
136
|
-
}
|
137
|
-
http.ServeContent(w, r, asset, stat.ModTime(), file)
|
138
|
-
})
|
139
|
-
}
|
@@ -1,42 +0,0 @@
|
|
1
|
-
package main
|
2
|
-
|
3
|
-
import (
|
4
|
-
"bytes"
|
5
|
-
"html/template"
|
6
|
-
"log"
|
7
|
-
)
|
8
|
-
|
9
|
-
type Templates struct {
|
10
|
-
tmpls *template.Template
|
11
|
-
}
|
12
|
-
|
13
|
-
// Object defining what all template data will use
|
14
|
-
type TmplProps struct {
|
15
|
-
Common *CommonProps
|
16
|
-
Contents interface{}
|
17
|
-
}
|
18
|
-
|
19
|
-
// Generic properties shared by all templates
|
20
|
-
type CommonProps struct {
|
21
|
-
Title string
|
22
|
-
Debug bool
|
23
|
-
RootURL string
|
24
|
-
Host string
|
25
|
-
}
|
26
|
-
|
27
|
-
// Loads the templates from disk and returns them loaded
|
28
|
-
// into memory.
|
29
|
-
func (t *Templates) LoadTemplates(path string) {
|
30
|
-
t.tmpls = template.Must(template.ParseGlob(path + "/*.gotmpl"))
|
31
|
-
}
|
32
|
-
|
33
|
-
// Renders a template and returns the byte array for it
|
34
|
-
func (t *Templates) Render(tmplName string, common *CommonProps, contents interface{}) ([]byte, error) {
|
35
|
-
buf := bytes.NewBuffer(nil)
|
36
|
-
err := t.tmpls.ExecuteTemplate(buf, tmplName, &TmplProps{Common: common, Contents: contents})
|
37
|
-
if err != nil {
|
38
|
-
log.Println("Error: failed to execute template", tmplName, ", because", err)
|
39
|
-
}
|
40
|
-
|
41
|
-
return buf.Bytes(), err
|
42
|
-
}
|