Deployment of web app html file not found

It’s the first time trying to deploy to a VPS.

In the development machine, source files are in
$GOPATH/src/github.com/myuser/myapp
html are at $GOPATH/src/github.com/myuser/myapp/html
static are at $GOPATH/src/github.com/myuser/myapp/static

It all works fine, both using go run … as using the binary.

When deploying (to a FreeBSD VPS), what I did was:
copy the binary to $HOME/go/
copy recursively html/ and static/ to $HOME/go/ as well, so ls $HOME/go
binary
html/
static/

But it seems that the app in the VPS can’t find some html file(s).
What am I missing?
Do I have to set GOPATH although I’m not using sources at the VPS?
Place the html/ in another place?
Recreate the directory structure of the development machine under $GOPATH?
What else?

Hi. Which paths do you use inside the go program? And no GOPATH only matters while developing. Your binary don’t know anything about it.

I suggest you to follow this steps to avoid problems with paths in your application:

  • Build a path to your resources (project folder) available from entire system

      path, err := filepath.Abs(filepath.Dir(os.Args[0]))
      if err != nil {
      	log.Fatal(err)
      }
    
  • If you use templates also use above path

      if _, err := templ.ParseGlob(filepath.Join(path, "html", "*.html")); err != nil {
      	log.Fatalln(err)
      }
    
  • Most probably you will need a file server for static resources

      http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(filepath.Join(path, "static")))))
    
  • Optional, some people (including me) use a deploy tool to copy and run the application in the same conditions on VPS as on your machine. A good one from my toolbox is the cloud tool.

Adjust the above examples as you need. Hope this will help.

1 Like

At the production box, had a script that was running the app. What I had to do was to include a cd go before exectuting the application.

Thanks both of you.

Very good solution. Thank you.


From BOC Sciences

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.