The server you created on the Getting Started page is only a single file, it does not compile as part of your application (due to the at the top). The main reason for this is that it makes things simpler when you are just hacking away on your user interface. If you enter bad code into a .vugu file (which ends up in a .go file and breaks your build), go run devserver.go will still run.

    To make a proper server suitable for staging or production, or to start adding more server-side functionality to, you'll want to create a server.go file, and place separate web server code in here. It is suggested you leave devserver.go where it is and you can use it if/when needed, it won't conflict with server.go.

    This example includes a flag to enable automatically performing Vugu code generation and rebuilding your wasm (same as devserver.go), which you can turn on and off. Running without -dev, this can also serve as a viable production server. (Obviously you need to be aware of your specific prouction environment requirements, but this will get you pointed in the right direction.)

    Command line flags for the HTTP listener and the directory to look for files in are also included. server.go:

    Making dist.go

    Rather than introducing third party build tools, the suggested approach to distributing your application is to create a small file called dist.go (ignored by the rest of your application) that you run when you want to build your distribution. The package has some convient functions that make this less tedious than it might seem. This approach also has the advantage that it works equally well on Windows, Linux and Mac, and without having to install anything. The Go module system takes care of that for you.

    Various steps are possible and there is not a one-size-fits-all dist.go. That said, here's one that will get you started:

    • It creates a "dist" folder, copies the static files from your project into it
    • It finds and copies wasm_exec.js
    • It makes sure vugugen is installed
    • It runs go generate
    • It builds your main.wasm file
    • And if desired you can make it write out an index.html or build your server executable
    1. // +build ignore
    2.  
    3. package main
    4.  
    5. import (
    6. "flag"
    7. "fmt"
    8. "log"
    9. "os"
    10. "os/exec"
    11. "path/filepath"
    12. "time"
    13.  
    14. "github.com/vugu/vugu/distutil"
    15. )
    16. func main() {
    17.  
    18. clean := flag.Bool("clean", false, "Remove dist dir before starting")
    19. dist := flag.String("dist", "dist", "Directory to put distribution files in")
    20. flag.Parse()
    21.  
    22. start := time.Now()
    23.  
    24. if *clean {
    25. os.RemoveAll(*dist)
    26. }
    27.  
    28. os.MkdirAll(*dist, 0755) // create dist dir if not there
    29.  
    30. // copy static files
    31. distutil.MustCopyDirFiltered(".", *dist, nil)
    32.  
    33. // find and copy wasm_exec.js
    34. distutil.MustCopyFile(distutil.MustWasmExecJsPath(), filepath.Join(*dist, "wasm_exec.js"))
    35.  
    36. // check for vugugen and go get if not there
    37. if _, err := exec.LookPath("vugugen"); err != nil {
    38. }
    39.  
    40. // run go generate
    41. fmt.Sprint(distutil.MustExec("go", "generate", "."))
    42.  
    43. // run go build for wasm binary
    44. fmt.Sprint(distutil.MustEnvExec([]string{"GOOS=js", "GOARCH=wasm"}, "go", "build", "-o", filepath.Join(*dist, "main.wasm"), "."))
    45.  
    46. // STATIC INDEX FILE:
    47. // if you are hosting with a static file server or CDN, you can write out the default index.html from simplehttp
    48. // req, _ := http.NewRequest("GET", "/index.html", nil)
    49. // outf, err := os.OpenFile(filepath.Join(*dist, "index.html"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
    50. // distutil.Must(err)
    51. // defer outf.Close()
    52. // template.Must(template.New("_page_").Parse(simplehttp.DefaultPageTemplateSource)).Execute(outf, map[string]interface{}{"Request": req})
    53.  
    54. // BUILD GO SERVER:
    55. // or if you are deploying a Go server (yay!) you can build that binary here
    56. // fmt.Sprint(distutil.MustExec("go", "build", "-o", filepath.Join(*dist, "server"), "."))
    57.  
    58. log.Printf("dist.go complete in %v", time.Since(start))
    59. }