├── .travis.yml ├── README.md ├── assets.go ├── assets ├── index.html.tmpl ├── script │ └── main.go └── style.css ├── assets_gen.go ├── assets_vfsdata.go └── main.go /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: go 3 | go: 4 | - 1.8.x 5 | - master 6 | matrix: 7 | allow_failures: 8 | - go: master 9 | fast_finish: true 10 | install: 11 | - # Do nothing. This is needed to prevent default install action "go get -t -v ./..." from happening here (we want it to happen inside script step). 12 | script: 13 | - go get -t -v ./... 14 | - diff -u <(echo -n) <(gofmt -d -s .) 15 | - go tool vet . 16 | - go test -v -race ./... 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gopherpen [](https://travis-ci.org/gopherjs/gopherpen) [](https://godoc.org/github.com/gopherjs/gopherpen) 2 | 3 | Gopherpen is a project template that will let you easily get started with GopherJS 4 | for building a web app. It includes some simple HTML, CSS, and Go code for the frontend. 5 | Make some changes, and refresh in browser to see results. When there are errors in your 6 | frontend Go code, they will show up in the browser console. 7 | 8 | Once you're done making changes, you can easily create a fully self-contained static 9 | production binary. 10 | 11 | Installation 12 | ------------ 13 | 14 | Get the source code for gopherpen and all dependencies, both for production and development modes: 15 | 16 | ```bash 17 | go get -u -d github.com/gopherjs/gopherpen/... 18 | go get -u -d -tags=dev github.com/gopherjs/gopherpen/... 19 | go get -u -d -tags=generate github.com/gopherjs/gopherpen/... 20 | ``` 21 | 22 | Building 23 | -------- 24 | 25 | ### Development Build 26 | 27 | Accesses assets from disk directly: 28 | 29 | ```bash 30 | go build -tags=dev 31 | ``` 32 | 33 | ### Production Build 34 | 35 | All assets are statically embedded in the binary, so it can run standalone in any folder: 36 | 37 | ```bash 38 | go generate 39 | go build 40 | ``` 41 | 42 | License 43 | ------- 44 | 45 | - [MIT License](https://opensource.org/licenses/mit-license.php) 46 | -------------------------------------------------------------------------------- /assets.go: -------------------------------------------------------------------------------- 1 | // +build dev 2 | 3 | package main 4 | 5 | import ( 6 | "net/http" 7 | 8 | "github.com/shurcooL/go/gopherjs_http" 9 | "github.com/shurcooL/httpfs/union" 10 | ) 11 | 12 | var assets = union.New(map[string]http.FileSystem{ 13 | "/assets": gopherjs_http.NewFS(http.Dir("assets")), 14 | }) 15 | -------------------------------------------------------------------------------- /assets/index.html.tmpl: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 | 5 | 6 |Hello, world of {{.Animals}}!
7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /assets/script/main.go: -------------------------------------------------------------------------------- 1 | // +build js 2 | 3 | package main 4 | 5 | import "fmt" 6 | 7 | func main() { 8 | fmt.Println("Script is working! Try making some changes to it.") 9 | } 10 | -------------------------------------------------------------------------------- /assets/style.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 20px; 3 | } 4 | -------------------------------------------------------------------------------- /assets_gen.go: -------------------------------------------------------------------------------- 1 | // +build generate 2 | 3 | package main 4 | 5 | import ( 6 | "log" 7 | 8 | "github.com/shurcooL/vfsgen" 9 | ) 10 | 11 | func main() { 12 | err := vfsgen.Generate(assets, vfsgen.Options{ 13 | BuildTags: "!dev", 14 | }) 15 | if err != nil { 16 | log.Fatalln(err) 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | //go:generate go run assets_gen.go assets.go 2 | 3 | // Gopherpen is a project template that will let you easily get started with GopherJS 4 | // for building a web app. It includes some simple HTML, CSS, and Go code for the frontend. 5 | // Make some changes, and refresh in browser to see results. When there are errors in your 6 | // frontend Go code, they will show up in the browser console. 7 | // 8 | // Once you're done making changes, you can easily create a fully self-contained static 9 | // production binary. 10 | package main 11 | 12 | import ( 13 | "flag" 14 | "fmt" 15 | "html/template" 16 | "log" 17 | "net/http" 18 | "strings" 19 | 20 | "github.com/shurcooL/httpfs/html/vfstemplate" 21 | "github.com/shurcooL/httpgzip" 22 | ) 23 | 24 | var httpFlag = flag.String("http", ":8080", "Listen for HTTP connections on this address.") 25 | 26 | func loadTemplates() (*template.Template, error) { 27 | t := template.New("").Funcs(template.FuncMap{}) 28 | t, err := vfstemplate.ParseGlob(assets, t, "/assets/*.tmpl") 29 | return t, err 30 | } 31 | 32 | func mainHandler(w http.ResponseWriter, req *http.Request) { 33 | t, err := loadTemplates() 34 | if err != nil { 35 | log.Println("loadTemplates:", err) 36 | http.Error(w, err.Error(), http.StatusInternalServerError) 37 | return 38 | } 39 | 40 | var data = struct { 41 | Animals string 42 | }{ 43 | Animals: "gophers", 44 | } 45 | 46 | err = t.ExecuteTemplate(w, "index.html.tmpl", data) 47 | if err != nil { 48 | log.Println("t.Execute:", err) 49 | http.Error(w, err.Error(), http.StatusInternalServerError) 50 | return 51 | } 52 | } 53 | 54 | func main() { 55 | flag.Parse() 56 | 57 | http.HandleFunc("/", mainHandler) 58 | http.Handle("/assets/", httpgzip.FileServer(assets, httpgzip.FileServerOptions{ServeError: httpgzip.Detailed})) 59 | http.Handle("/favicon.ico", http.NotFoundHandler()) 60 | 61 | printServingAt(*httpFlag) 62 | err := http.ListenAndServe(*httpFlag, nil) 63 | if err != nil { 64 | log.Fatalln("ListenAndServe:", err) 65 | } 66 | } 67 | 68 | func printServingAt(addr string) { 69 | hostPort := addr 70 | if strings.HasPrefix(hostPort, ":") { 71 | hostPort = "localhost" + hostPort 72 | } 73 | fmt.Printf("serving at http://%s/\n", hostPort) 74 | } 75 | --------------------------------------------------------------------------------