Skip to content

Commit d8346f8

Browse files
committed
Merge pull request #2 from gopherjs/simplify
Simplify code, instructions in README.
2 parents 11f7eba + 0661673 commit d8346f8

File tree

6 files changed

+44
-53
lines changed

6 files changed

+44
-53
lines changed

README.md

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,33 @@
11
# Gopherpen
22

3-
This is a template project that will let you easily get started with GopherJS. It includes some simple HTML, CSS, and Go code for the frontend. Make some changes, and refresh in browser to see results. When there are errors in your frontend Go code, they will show up in the dev console.
3+
This is a project template that will let you easily get started with GopherJS for building a web app. It includes some simple HTML, CSS, and Go code for the frontend. Make some changes, and refresh in browser to see results. When there are errors in your frontend Go code, they will show up in the browser console.
44

5-
Once you're done making changes, you can create a static production binary that has all assets built in, and can be deployed to any server.
5+
Once you're done making changes, you can easily create a fully self-contained static production binary.
66

77
## Installation
88

9-
Run this to get gopherpen and all dependencies, both for development and production modes.
9+
Get the source code for gopherpen and all dependencies, both for production and development modes:
1010

11-
```
12-
go get -u github.com/gopherjs/gopherpen
13-
go get -u -tags=dev github.com/gopherjs/gopherpen
14-
```
15-
16-
To run `go generate`, you'll also need:
17-
18-
```
19-
go get -u github.com/shurcooL/vfsgen
11+
```bash
12+
go get -u -d github.com/gopherjs/gopherpen/...
13+
go get -u -d -tags=generate github.com/gopherjs/gopherpen/...
2014
```
2115

2216
## Building
2317

2418
### Development Build
2519

26-
Accesses assets from disk directly.
20+
Accesses assets from disk directly:
2721

28-
```
22+
```bash
2923
go build -tags=dev
3024
```
3125

3226
### Production Build
3327

34-
All assets are statically embedded in the binary, so it can run standalone in any folder.
28+
All assets are statically embedded in the binary, so it can run standalone in any folder:
3529

36-
```
30+
```bash
3731
go generate
3832
go build
3933
```

assets/index.html.tmpl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<html>
22
<head>
33
<link href="/assets/style.css" rel="stylesheet" type="text/css" />
4-
<script src="/assets/script/script.js" type="text/javascript"></script>
54
</head>
6-
<body>A beginning.</body>
5+
<body>
6+
<p>Hello, world of {{.Animals}}!</p>
7+
8+
<script src="/assets/script/script.js" type="text/javascript"></script>
9+
</body>
710
</html>

assets/script/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
package main
44

5+
import "fmt"
6+
57
func main() {
6-
println("Script is working!")
8+
fmt.Println("Script is working! Try making some changes to it.")
79
}

assets_gen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build ignore
1+
// +build generate
22

33
package main
44

assets_vfsdata.go

Lines changed: 10 additions & 10 deletions
Large diffs are not rendered by default.

main.go

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,34 @@ import (
77
"log"
88
"net/http"
99
"strings"
10-
"sync"
1110

1211
"github.com/shurcooL/go/gzip_file_server"
1312
"github.com/shurcooL/httpfs/html/vfstemplate"
1413
)
1514

1615
var httpFlag = flag.String("http", ":8080", "Listen for HTTP connections on this address.")
1716

18-
var t *template.Template
19-
20-
func loadTemplates() error {
21-
var err error
22-
t = template.New("").Funcs(template.FuncMap{})
23-
t, err = vfstemplate.ParseGlob(assets, t, "/assets/*.tmpl")
24-
return err
25-
}
26-
27-
var state struct {
28-
mu sync.Mutex
17+
func loadTemplates() (*template.Template, error) {
18+
t := template.New("").Funcs(template.FuncMap{})
19+
t, err := vfstemplate.ParseGlob(assets, t, "/assets/*.tmpl")
20+
return t, err
2921
}
3022

3123
func mainHandler(w http.ResponseWriter, req *http.Request) {
32-
if err := loadTemplates(); err != nil {
24+
t, err := loadTemplates()
25+
if err != nil {
3326
log.Println("loadTemplates:", err)
3427
http.Error(w, err.Error(), http.StatusInternalServerError)
3528
return
3629
}
3730

38-
state.mu.Lock()
39-
err := t.ExecuteTemplate(w, "index.html.tmpl", &state)
40-
state.mu.Unlock()
31+
var data = struct {
32+
Animals string
33+
}{
34+
Animals: "gophers",
35+
}
36+
37+
err = t.ExecuteTemplate(w, "index.html.tmpl", data)
4138
if err != nil {
4239
log.Println("t.Execute:", err)
4340
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -48,17 +45,12 @@ func mainHandler(w http.ResponseWriter, req *http.Request) {
4845
func main() {
4946
flag.Parse()
5047

51-
err := loadTemplates()
52-
if err != nil {
53-
log.Fatalln("loadTemplates:", err)
54-
}
55-
56-
http.Handle("/favicon.ico", http.NotFoundHandler())
5748
http.HandleFunc("/", mainHandler)
5849
http.Handle("/assets/", gzip_file_server.New(assets))
50+
http.Handle("/favicon.ico", http.NotFoundHandler())
5951

6052
printServingAt(*httpFlag)
61-
err = http.ListenAndServe(*httpFlag, nil)
53+
err := http.ListenAndServe(*httpFlag, nil)
6254
if err != nil {
6355
log.Fatalln("ListenAndServe:", err)
6456
}

0 commit comments

Comments
 (0)